1

We are using JSF2 and using Navigation Handler to control the navigation between public and secured pages of our web application.

The logic has become so complicated as we need to check for which page is secure and which page is public.

Is there any framework or better way to handle Navigation Handler...

user684434
  • 1,165
  • 2
  • 19
  • 39

1 Answers1

2

Normally you put the secured pages in a common URL path, such as /app/*, /secured/*, /private/*, etc. This way you can use a single entry point to control the access. If you're using container managed security, it's then a matter of specifying the proper URL pattern:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Restricted pages</web-resource-name>
        <url-pattern>/secured/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>*</role-name>
    </auth-constraint>
</security-constraint>

If you are however using homegrown security, then you need to implement a Filter for that instead:

@WebFilter("/secured/*")
public class AuthorizationFilter implements Filter {
    // ...
}

or when you're still not on Servlet 3.0 yet, then register it as follows instead of using @WebFilter:

<filter>
    <filter-name>authorizationFilter</filter-name>
    <filter-class>com.example.AuthorizationFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>authorizationFilter</filter-name>
    <url-pattern>/secured/*</url-pattern>
</filter-mapping>

Inside the doFilter() method you need to write code which checks if the user is logged in or not and then continues the chain or redirects the response to the login page (which is by itself of course not covered by the same URL pattern).

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Can we use JSF Managed bean within a Filter ? – user684434 Dec 16 '11 at 15:55
  • Please check the "See also" link below my answer for a more concrete example of the filter and this related link: http://stackoverflow.com/questions/2633112/jsf-get-managed-bean-by-name/ – BalusC Dec 16 '11 at 15:55
  • does jsf2 supports servlets3.0 – user684434 Dec 16 '11 at 19:00
  • This support is not JSF specific. This support is container specific. JSF 2.0 requires a minimum of Servlet 2.5 and works therefore fine on Servlet 3.0 or newer. Servlet 3.0 containers are Tomcat 7, Glassfish 3, JBoss AS 6, etc. If you already have it, but can't seem to utilize new Servlet 3.0 features, then make sure that `web.xml` is declared conform Servlet 3.0 version. – BalusC Dec 16 '11 at 19:02