Working with the JSF Security project

JSF Security is a set of security extensions for JavaServer Faces to solve common access control problems. JSF Security acts like a security layer by extending the JSF EL (Expression Language). Basically, it works in a separate scope, named securityScope, and accesses the security artifacts through EL language. In this recipe, you will see how to use the EL extensions provided by the JSF Security project.

Getting ready

We have developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library. In addition, we have used JSF Security 1.0, which provides support for JSF 2.0. You can download this distribution from http://sourceforge.net/projects/jsf-security/files/jsf-security/. The jsf-security libraries (including necessary dependencies) are in the book code bundle, under the /JSF_libs/jsf-security JSF 2.0 folder. The JSF Security project is available in ZIP format. All you have to do is to add the jsf-security.jar archive to your JSF projects.

How to do it...

Before developing an effective application let's see the available EL expressions:

Expression

Effect

#{securityScope.authType}

The authentication type being used; with container security this will be BASIC, FORM, DIGEST, or JAAS may return custom strings.

#{securityScope.remoteUser}

The user name of the authenticated user.

#{securityScope.securityEnabled}

If security is currently enabled this EL returns true. It returns false if no security is installed or the user is not yet authenticated.

#{securityScope.userInRole['role_1, role_2, … role_n']}

This returns true if the user is in at least one of the roles. It returns false if the user is not in any of the roles or if the user is not currently authenticated.

#{securityScope.userInAllRoles['role_1, role_2, … role_n']}

This returns true if the user is in all of the roles. It returns false if the user is not in all of the roles, or if the user is not currently authenticated.

Next, we will write a JSF page that will put the previous expressions in a single example. Assuming that we already have a role named, JSP-ROLE, our page looks as shown next:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8"/>
<title>JSF-SECURITY</title>
</head>
<body>
<h:form>
<h:panelGroup rendered="#{!securityScope.securityEnabled}">
<h:outputText value="Security is not enabled..."/>
</h:panelGroup>
<h:panelGrid columns="2"
rendered="#{securityScope.securityEnabled}">
<h:outputText value="Remote User"/>
<h:outputText value="#{securityScope.remoteUser}"/>
<h:outputText value="Auth Type"/>
<h:outputText value="#{securityScope.authType}"/>
<h:outputText value="User in JSP-ROLE "/>
<h:outputText value="#{securityScope.userInRole['JSP-ROLE']}"/>
<h:outputText value="User in all of JSP-ROLE "/>
<h:outputText value="#{securityScope.userInAllRoles['JSP
-ROLE']}"/>
</h:panelGrid>
</h:form>
</body>
</html>
</f:view>

The jsf_security.jar contains a faces-config.xml file in its META-INF directory. This defines custom <variable-resolver> and <property-resolver> values, as shown next:

<application>
<property-resolver>
com.groundside.jsf.securityresolver.SecurityPropertyResolver
</property-resolver>
<variable-resolver>
com.groundside.jsf.securityresolver.SecurityVariableResolver
</variable-resolver>
</application>

The JSP-ROLE was configured under Sun GlassFish Enterprise Server V3 Prelude container, but you can set it on any other container using the right knowledge. For more details of how to configure the JSP-ROLE under GlassFish you can try http://www.informit.com/authors/bio.aspx?a=3064cf95-43af-48f6-9303-8d2fdd7f3706.

The output of this example is in the following screenshot (we set the BASIC authentication type in the web.xml descriptor):

How to do it...

How it works...

The JSF Security layer interacts with the default security layers and provides EL extensions for managing common access control problems. The extensions are completely pluggable and can adapt to more or less any mechanism that is used for authentication and authorization that the programmer can reach from the FacesContext/Request/Session.

Notice that, by default JSF Security hooks into J2EE container-managed security using the J2EEContainerSecurityAttributeResolver. It is possible to plug in an alternative implementation here by a simple configuration change.

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and it is named: Working_with_jsf_security_project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset