开发者

get user's name regardless of authentication-realm

开发者 https://www.devze.com 2023-03-22 17:05 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消