Wednesday, May 23, 2007

J2EE Container Managed Security: How to reference the current user


When J2EE container managed security is used the User Principal can be referenced in a number of ways:


Expression Language
       <af:outputText value="#{facesContext.externalContext.userPrincipal.name}"/>

Managed / Backing Bean
       ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
String userName = ectx.getUserPrincipal().getName();
System.out.println("Current user: " + userName);

ADF BC Application Module
       String userName = getUserPrincipalName();
System.out.println("Current user: " + userName);


1 comment:

Anonymous said...

Thanks a lot Brenden!

af:outputText value="#{facesContext.externalContext.userPrincipal.name}"

was EXACTLY what I was looking for, to include in a web application I'm working on. This has been driving me nuts for the past few days.

I wonder why such an obvious information has to be obtained witch such an obfuscated way, though

Anyway, thanks again for this useful post!