Duncan Mills

Subscribe to Duncan Mills: eMailAlertsEmail Alerts
Get Duncan Mills: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Related Topics: Java EE Journal, Apache Web Server Journal

J2EE Journal: Article

Effective Page Authorization In JavaServer Faces

Application security - the art of applications defending themselves - represents an important line of defence

Listing 9 The J2EESecurityPhaseListener listens to any event but responds to RestoreView and InvokeApplication only

public void afterPhase(PhaseEvent phaseEvent)
    {
      PhaseId phaseid = phaseEvent.getPhaseId();

      if ( phaseid == PhaseId.RESTORE_VIEW||
        phaseid == PhaseId.INVOKE_APPLICATION )
      { ... }
}

The J2EESecurityPhaseListener uses an extra XML configuration file, faces-security-config.xml, to define JSF pages that require authentication, authorization, or secure communication with SSL. The faces-security-config.xml file is located in the WEB-INF directory like the JavaServer Faces faces-config.xml file.

The security PhaseListener parses the faces-security-config.xml file with the Apache Digester [DIGESTER] to create a Java security object that is subsequently cached in the application scope. The security object contains security information about the configured HTTP and HTTPS ports, whether or not to keep SSL after it's used the first time, and individual authentication, authorization, and SSL requirements for each page.

The PhaseListener determines page authorization only once per session for each JSF page (viewId) that a user requests. At successful authorization, a reference to the JSF page is cached in the session to improve performance on subsequent requests for the same resource.

A user who requests a protected page without first having been authenticated is redirected to an authentication servlet. The authentication servlet is configured in the web.xml deployment descriptor and protected by a J2EE security role that all users are members of. All JSF pages configured in the faces-security-comfig.xml file to require authorization implicitly also require authentication. If an unauthenticated user tries to access a page that requires SSL, a container-managed logon form is launched over HTTPS.

The faces-security-config.xml file consists of two parts:

  • A global configuration section to provide HTTP and HTTPS port information and whether SSL communication should be kept once established. The keep-SSL-mode information overrides the individual page configuration for SSL.
  • Multiple jsf-page elements to configure page and directory authorization. The page is identified by its viewId, which starts with a leading slash followed by the relative URI, not excluding the faces virtual mapping (for example, /protected/main.jsp). Directory names are indicated by an appended wildcard character '*' (for example, /protected/*).
For each protected page or directory, the developer defines the authentication, authorization, and SSL requirements. If a page requires SSL, but the current request protocol is HTTP, the J2EESecurityPhaseListener redirects the request to a configured HTTPS port. Similarly, if the protocol is currently HTTPS but the page doesn't require a secure channel and the keep-ssl-mode is set to false, the PhaseListener redirects the request to the HTTP port.

JSF pages and directories that require authorization have to reference the name of one or more J2EE security roles defined in the web.xml file. The role-concatenation attribute lets developers specify whether a user has to be a member of all the configured roles (AND) or only a single role (OR).

Because the security configurations are stored in an XML file, the page authentication strategy and the authorization definitions can be changed at any time without recompiling or redeploying the application.

To use the J2EESecurityPhaseListener in custom JSF applications, developers do the following

  • Deploy the jsfj2ee-security-util.jar file as a library with the application.
  • Create and configure the faces-security-config.xml file in the application WEB-INF directory.
  • For protocol switching, configure the HTTP and HTTPS ports and ensure that the application server is set up to share a session between the two ports.
  • Configure the authentication servlet in the web.xml file.
  • Configure container-managed authentication and J2EE security roles in the web.xml file.
Once set up, the J2EESecurityPhaseListener lets developers apply authorization to any JSF page navigation - a forward or a redirect - while still using standard container mechanisms to handle authentication and role definition.

To improve default container-managed security, the J2EESecurityPhaseListener also lets developers configure page authorizations that use multiple J2EE roles, rather than having to define multiple roles to achieve fine-grained authorization in conventional container-managed security.

Completing the Page Authorization Picture with a Custom Property Resolver
The "limited view" security design pattern defines what users can view and access and lets them access accordingly. Protecting a page from unauthorized access alone isn't enough. UI components that initiate page navigation should be hidden if the user is not allowed access the navigation target. To set a component's rendered property to false, expression language (EL) can be used. The EL can be sourced from handcrafted managed beans defined as part of the application or more generically through a security-aware custom variable resolver. A sample resolver of this type is available for download at jsf-security.sourceforge.net/.

Summary
Without a doubt, JavaServer Faces is a great step forward for J2EE application development. Application security is, however, an important component in the development lifecycle and, unfortunately, this is where the current JavaServer Faces specification falls short. In the future, we would hope and expect that security integration, perhaps of a nature similar to that discussed in this article and the associated code, will be added to the specification.

The custom J2EESecurityPhase-Listener developed for this article uses container-managed security for the sake of simplicity, but it can be adapted to use either JAAS or a custom security provider. The source code for this PhaseListener based solution can be downloaded with this article.

References

More Stories By Duncan Mills

Duncan Mills is senior director of product management for Oracle's Application Development Tools - including the JDeveloper IDE, and the Oracle Application Development Framework. He has been in the IT industry for the past 19 years working with Oracle, Java, and a variety of more obscure programming languages and frameworks along the way. Duncan is the co-author of the Oracle Press book: Oracle JDeveloper 10g for Forms and PL/SQL Developers - a Guide to Web Development with Oracle ADF.

More Stories By Frank Nimphius

Frank Nimphius is a principal product manager for application development tools at Oracle Corporation. As a conference speaker, Frank represents the Oracle J2EE development team at J2EE conferences world wide, including various Oracle user groups and the Oracle Open World conference.

Comments (6) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
y_alomoush 09/01/08 04:29:19 PM EDT

Hi Duncan and Frank,

Thanks for your great efforts.

It is a job well done. In fact,I have installed the module and plugged it to my own application for evaluation purposes.However, I have found a scenario where a user can access into a unauthorized page, I don't know if I can call it as a bug or not.

The scenario is as the following:
1-start the application by calling a page that requires authentication and authorization.
2-the system shall direct you to the login page.

3-enter a correct username/password but not authorized to access that page.

3-system shall redirect you to error page using the sendError method.

4-call the same page again (using the same session). the system strangely forward you to the page that you are not authorized to access.(because the page is already cached as an authorized page)

I think after the page is redirected to the error page the handleSecurity method must return, in this case, false instead of retuning true, this in order not to cache an unauthorized page.

Thank yo once again.

keerthi 09/21/06 10:22:56 AM EDT

Hi Duncan and Frank,

This article is really an interesting one. I found it at a right moment of time as I was trying to implement Page level security in a Project based on JSF.
I was wondering the article is based on Container-Managed Security or reading roles from web.xml. I have a requirement where I need to read the roles from database and not from web.xml, can I achieve this security feature by implementing the points mentioned in this article.
Awaiting for your response.
Thanks and Regards,
Keerthi.

SYS-CON Italy News Desk 08/10/06 05:42:31 PM EDT

Application security - the art of applications defending themselves - represents an important line of defence in an overall in-depth security strategy. Web applications that follow the Model-View-Controller (MVC) architecture can, and should, have security implemented on all three layers. Normally it's the controller component that handles page authorization in MVC, the view layer that hides controls and information based on user authorization, and the model that enforces the business rules and input validation. However, it's up to the developer, based on an individual security policy and the programming technology used, to decide where to put security. Using pluggable validator components in JavaServer Faces (JSF), for example, developers may decide to verify user input on the view layer as well as on the model layer.

AJAXWorld News Desk 08/10/06 05:26:41 PM EDT

Application security - the art of applications defending themselves - represents an important line of defence in an overall in-depth security strategy. Web applications that follow the Model-View-Controller (MVC) architecture can, and should, have security implemented on all three layers. Normally it's the controller component that handles page authorization in MVC, the view layer that hides controls and information based on user authorization, and the model that enforces the business rules and input validation. However, it's up to the developer, based on an individual security policy and the programming technology used, to decide where to put security. Using pluggable validator components in JavaServer Faces (JSF), for example, developers may decide to verify user input on the view layer as well as on the model layer.

JDJ News Desk 07/26/06 05:18:34 PM EDT

Application security - the art of applications defending themselves - represents an important line of defence in an overall in-depth security strategy. Web applications that follow the Model-View-Controller (MVC) architecture can, and should, have security implemented on all three layers. Normally it's the controller component that handles page authorization in MVC, the view layer that hides controls and information based on user authorization, and the model that enforces the business rules and input validation. However, it's up to the developer, based on an individual security policy and the programming technology used, to decide where to put security. Using pluggable validator components in JavaServer Faces (JSF), for example, developers may decide to verify user input on the view layer as well as on the model layer.

JDJ News Desk 07/26/06 04:40:02 PM EDT

Application security - the art of applications defending themselves - represents an important line of defence in an overall in-depth security strategy. Web applications that follow the Model-View-Controller (MVC) architecture can, and should, have security implemented on all three layers. Normally it's the controller component that handles page authorization in MVC, the view layer that hides controls and information based on user authorization, and the model that enforces the business rules and input validation. However, it's up to the developer, based on an individual security policy and the programming technology used, to decide where to put security. Using pluggable validator components in JavaServer Faces (JSF), for example, developers may decide to verify user input on the view layer as well as on the model layer.