开发者

How to obtain logged in user name in JSF when using container managed authentication

I have configured a form based log in in my JSF 2.0 Application on GlassFish 3 server, using a custom Realm. What is the simpliest way of getting information about a logged in user, i.e. the users name.

Is this possible? Or is the current session just associated with a security role? If so, is there so开发者_StackOverflow社区me way of making this possible without changing the log in configuration?

Simply put, what I want is to display a simple message like:

Logged in as username

on my webpages.


The easiest way to get at the logged in user via EL (Expression Language) in JSF 2.0 would be:

#{request.remoteUser}

Tobbe's answer would work well to get at the remote user from within a backing bean.


The simple (maybe not the best) answer was:

FacesContext.getCurrentInstance().getExternalContext().getRemoteUser()

I'm shocked about how long it took me to figure that out.


in your loginmanagedbean class define a currentUSer and in getter this: i didnt use an external logging in system and this works for me.

    public Login getCurrentUser() {
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext externalContext = fc.getExternalContext();
    if (externalContext.getUserPrincipal() == null){
        logger.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!current principal is null"); 
    }
    else{ 
        Integer id = Integer.parseInt(externalContext.getUserPrincipal().getName());
        logger.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!LOGGED USER "+ id); 
        try {
            currentUser = getLoginService().getLoginById(id);
        }
        catch (Exception ex) {
        } 
    } 
    return currentUser;
}


Inside Facelets you can use #{request.userPrincipal.name}.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜