简体   繁体   English

验证JAX-WS Web服务中的凭证

[英]Validate credentials in a JAX-WS webservice

I'd like to validate user and password values in a Java webservice operation. 我想在Java网络服务操作中验证用户和密码值。 Credentials have to be in a database, but to avoid calling multiple times to this layer I have a cache. 凭据必须在数据库中,但是为了避免多次调用此层,我有一个缓存。 I've been thinking to use a Filter ( http://viralpatel.net/blogs/tutorial-java-servlet-filter-example-using-eclipse-apache-tomcat/ ) and map this filter to the servlet that acts as the endpoint, but this way all operations from my endpoint will have to use this filter. 我一直在考虑使用过滤器( http://viralpatel.net/blogs/tutorial-java-servlet-filter-example-using-eclipse-apache-tomcat/ ),然后将此过滤器映射到充当终结点,但是通过这种方式,来自终结点的所有操作都必须使用此过滤器。 What if one operation has not to be authenticated? 如果一项操作没有通过身份验证怎么办?

I've found that in the example at http://docs.oracle.com/cd/E19226-01/820-7627/bnccv/index.html the following is used in web.xml : 我发现在http://docs.oracle.com/cd/E19226-01/820-7627/bnccv/index.html的示例中,在web.xml中使用了以下内容:

<auth-constraint>
    <role-name>SomeName</role-name>
</auth-constraint>

I can authenticate using roles but users have to be added in application server and I don't want that. 我可以使用角色进行身份验证,但是必须将用户添加到应用程序服务器中,但我不希望这样。

What is the best option? 最好的选择是什么?

Thanks 谢谢

One option is using HTTP headers where you can send parameters like username and password. 一种选择是使用HTTP标头,您可以在其中发送诸如用户名和密码之类的参数。 Using the webservicecontext you can access to HTTP_REQUEST_HEADERS. 使用webservicecontext,您可以访问HTTP_REQUEST_HEADERS。

@Resource
WebServiceContext wsctx;

@Override
public String callOperation() {

MessageContext mctx = wsctx.getMessageContext();

//get detail from request headers
    Map http_headers = (Map) mctx.get(MessageContext.HTTP_REQUEST_HEADERS);
    List userList = (List) http_headers.get("Username");
    List passList = (List) http_headers.get("Password");


    String username = "";
    String password = "";
    // do whatever you want with user and password

Hope this helps to other people. 希望这对其他人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM