我在实现JAX-WS Web服务的授权方面遇到了麻烦。我正在开发一个可以通过JAX-WS Web服务连接到Java EE应用程序的Swing应用程序。用户可以使用Swing应用程序登录服务器,并可以从服务器下载用户特定数据。记录的用户无法下载属于其他用户的数据。
我的问题是jaxwsContext.getUserPrincipal().getName()
以“ANONYMOUS”返回。我在这个门户网站上阅读了类似的问题,但遗憾的是它并没有帮助。
其实我有这个:
服务器端:
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@SOAPBinding(style = SOAPBinding.Style.RPC)
@WebService
public class SampleWSEJB extends AbstractSampleEJB implements ISampleWSLocal, ISampleWSRemote {
@Resource
private WebServiceContext jaxwsContext;
public String getUsername() {
return username = jaxwsContext.getUserPrincipal().getName();
}
@Override
@WebMethod
public UserDataVO logInUser() {
return SampleServerServices.getInstance().logInUser(getEm(), this.getUsername());
}
...
...
}
客户方:
我使用wsimport工具生成的一些类(ImportedSampleWSEJB,UserDataVO等...)
相关客户代码:
private static ImportedSampleWSEJB importedEJB;
public UserDataVO logInUser(String username, String password) {
Map<String, Object> requestContext = ((BindingProvider)ImportedSampleWSEJB.importedEJB).getRequestContext();
requestContext.put(BindingProvider.USERNAME_PROPERTY, username);
requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);
return importedEJB.logInUser();
}
我使用“file”作为安全领域,我在glassfish 3.1中创建了一些测试用户。
任何人都知道如何解决它?
答案 0 :(得分:0)
您可以使用基本身份验证(http标头),也可以使用UserName Token(WS-Security的一部分)。用户名令牌将在SOAP Header中而不是http头中。