I'm developing a webservice in Java on a stack of Metro and Tomcat, and my problem is with the user authentication. I understand Tomcat can make use of several external authentication realms, such as JDBCRealm or JNDIRealm, and I want my endusers to configure their Tomcat installation for their respective realm.
Now, what I need in my webservice-methods is the user name (or i开发者_如何学编程d, something unique) the user has used to log in, regardless of the concrete realm. Where do I get it? Obviously I've searched for the wrong key words so far - is there a specific name for the concept I want to use?
Thanks in advance!
The logged-in user is available by WebServiceContext#getUserPrincipal()
which returns a Principal
instance which in turn has a getName()
method.
E.g.
@Resource
private WebServiceContext context;
@WebMethod
public String hello() {
Principal user = context.getUserPrincipal();
return (user != null) ? user.getName() : "(not logged in)";
}
This is under the covers obtained from HttpServletRequest#getUserPrincipal()
by the way.
精彩评论