aspose file tools
The moose likes JSF and the fly likes retrieve current authenticated user name Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » JSF
Reply Bookmark "retrieve current authenticated user name" Watch "retrieve current authenticated user name" New topic
Author

retrieve current authenticated user name

Vidal Sassoon
Greenhorn

Joined: Oct 10, 2006
Posts: 2
I am trying to retrieve the current authenticated user with the following code

public String getCurrentUser() {
Principal principal = getUserPrincipal();
if (principal == null){
setUser("");
} else {
setUser(principal.getName());
}
return user;
}

public Principal getUserPrincipal() {
FacesContext facesContext = FacesContext.getCurrentInstance();

if ( facesContext != null )
{
return facesContext.getExternalContext().getUserPrincipal();
}

return null;
}

Problem is the principal is returning null. The facesContext does not return null but getUserPrincipal() does. Any ideas why this is? Thanks.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14456
    
    7

Try

String userName = facesContext.getExternalContext().getRemoteUser();

I think that's what I use. Along with things like

boolean isAdmin = facesContext.getExternalContext().isUserInRole("sysadmin"));

Unless you actually need the Principal object, this is a little cleaner and probably keeps you from having to worry about what your container is.


Customer surveys are for companies who didn't pay proper attention to begin with.
Vidal Sassoon
Greenhorn

Joined: Oct 10, 2006
Posts: 2
Thank you very much for replying. I probably should have mentioned before that I tried getRemoteUser as well.

FacesContext facesContext = FacesContext.getCurrentInstance();
String name = facesContext.getExternalContext().getRemoteUser();

This also returned a null value. Am I missing something. Do I need anything else besides these two lines? I am just trying to retrieve the Windows authenticated user. Currently testing this on Windows XP. Thanks for any help.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14456
    
    7

OK, there's your problem. Windows authentication (LAN authentication) and web browser authentication are 2 different things, unless you specifically set up the Windows box to turn on the IE single-signon option (amazing isn't it, a case where Windows was actually secure by default!). In which case, you have enabled a protocol that causes your windows credentials to be passed as piggyback data to the web server.

You can really only use this feature on a LAN, since not all of us out here are logged into your Windows domain, or even running under Windows.

The getRemoteUser and getUserPrincipal methods get their data from the webserver's J2EE authentication and authorization service (Tomcat calls it a security Realm). Only if you have setup a realm that ties into the LAN authentication and authorization services will it be able to supply Windows user identity info.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: retrieve current authenticated user name
 
Similar Threads
difference between HttpServletRequest's getUserPrincipal and getRemoteUser
LDAP authentication in a web application.
Modifying a request using a Servlet Filter
Get Request
Windows UID