• Post Reply Bookmark Topic Watch Topic
  • New Topic

retrieve current authenticated user name

 
Vidal Sassoon
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 18412
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vidal Sassoon
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 18412
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!