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);