In JSF, you can retrieve the current Principal associated with the request and hence, the current session, using the ExternalContext
object, which can be retrieved from the FacesContext
. The Principal associated with the request is accessible from the ExternalContext using the getUserPrincipal()
method:
FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
The above invocation can be done in a JSF managed bean. You can invoke the getName()
method on the Principal object to retrieve the name of the user as a String.
Note that, it possible for you to obtain a Principal
instance that references the Anonymous user, if you are retrieving the Principal before authentication, or if the authentication scheme does not protect the entire site.