2012-04-02 88 views
0

我想将安全上下文传递给我的休息服务。Rest - Jersey.Client将@SecurityContext传递给服务器

在服务器端我试图用得到:

public Response postObject(@Context SecurityContext security, JAXBElement<Object> object) { 
    System.out.println("Security Context: " + security.getUserPrincipal()); 
..... 

但实际上Syso为空。

在客户端IM只是在做:

ClientConfig config = new DefaultClientConfig(); 
    Client client = Client.create(config); 
    client.addFilter(new HTTPBasicAuthFilter("user", "password")); 

所以,我有另外的东西来改变我的web.xml中得到它的工作?

我希望它的工作不需要在tomcat用户xml中设置静态用户。因此,我可以将来自安全上下文的用户/密码与位于服务器端的“持久”用户/密码散列映射进行比较。但是,如果没有tomcat用户xml的工作,那么如何才能将用户动态地添加到该用户xml?当我是静态用户时,我无法注册一个新用户。我不想使用这种尝试:http://objecthunter.congrace.de/tinybo/blog/articles/89因为我只想使用像用户/密码的HashMap一样的半持久性。

除了另一个问题:为什么每个人都提到Apache HttpClient是关于泽西岛的安全问题,当它像我写的一样工作时?

我的尝试是指这个帖子:

Jersey Client API - authentication

回答

2

你需要设置你的应用程序服务器上,以便它需要基本身份验证。即在应用程序的war文件的web.xml中包含如下内容 - 否则Tomcat不会执行身份验证,也不会填充安全上下文。

<security-constraint> 
    <display-name>Authentication Constraint</display-name> 
    <web-resource-collection> 
     <web-resource-name>all</web-resource-name> 
     <description/> 
     <url-pattern>/*</url-pattern> 
    </web-resource-collection> 
    <auth-constraint> 
     <description>authentication required</description> 
    </auth-constraint> 
</security-constraint> 
<login-config> 
    <auth-method>BASIC</auth-method> 
    <realm-name>realm_name</realm-name> 
</login-config> 
+0

是的,这就是web.xml部分。我读了很多关于我的问题。看来我无法使用用户的半持久性地图。一方面,我可以使用在tomcat-user.xml中声明的静态用户,另一方面我可以使用我的数据库中的动态用户来完成。 – 2012-04-04 09:38:17