Description
Myfaces provides an expression language extension that specializes on the application security. By the help of SecurityContextVariableResolver and the SecurityContextPropertyResolver it's easy to retrieve information from the underlying authentication/authorization mechanism that is used in the application.
API
variable-resolver | org.apache.myfaces.custom.security.SecurityContextVariableResolver |
property-resolver | org.apache.myfaces.custom.security.SecurityContextPropertyResolver |
Usage
<h:outputText value="#{securityContext.remoteUser}" /> <h:commandButton action="#{someBean.edit}" value="Edit" rendered="#{securityContext.ifGranted['admin']}" /> <h:commandButton action="#{someBean.edit}" value="Edit" disabled="#{securityContext.ifAllGranted['admin,manager']}" /> <h:commandButton action="#{someBean.edit}" value="Edit" rendered="#{securityContext.ifAnyGranted['admin,manager,editor']}" /> <h:commandButton action="#{someBean.edit}" value="Edit" disabled="#{securityContext.ifNotGranted['guest']}" />
Syntax
#{securityContext.authType}
Gives the name of authentication mechanism used like BASIC, FORM or etc.
#{securityContext.ifGranted['rolename']}
If the user is in the role "rolename", returns true or vice versa
#{securityContext.ifAllGranted['rolename1,rolename2']}
Returns true if user is in all of the roles given in the roles list, vice versa
Additional Information
SecurityContext is an abstract class that is used when the expressions above are resolved, J2EE container security is used by the default implementation called SecurityContextImpl meaning; ifGranted #{securityContext.ifGranted['rolename']} will yield to FacesContext.getCurrentInstance().getExternalContext().isUserInRole("rolename").
Plugin Your Own SecurityContext
It's possible to provide your own implementation of the SecurityContext if you're using another mechanism to manage security other than J2EE container like JAAS or ACEGI. In order to plugin your implementation org.apache.myfaces.SECURITY_CONTEXT context parameter needs to be configured with the class name of your implementation as the param value.
<context-param> <param-name>org.apache.myfaces.SECURITY_CONTEXT</param-name> <param-value>com.mycompany.MySecurityContextImpl</param-value> </context-param>