View Source

h1. Spring Security 3.1.2 and ICEfaces 3.1 Tutorial


h3. About Spring Security

Spring Security 3 provides an API for configuring authentication and authorization. Authentication is possible against any number of repositories and databases. Authorization is applied at either the web resource level using Servlet Filters and/or at the business/service method level using aspects and annotations.

h3. About This Tutorial

The purpose of this tutorial is to demonstrate how application developers can use both Spring Security 3.1.2 and ICEfaces 3.1 in the same application. Both technologies leverage the Servlet API so it is essential one understands how the various parts of the web.xml file are organized to accommodate both frameworks. Its also important to be mindful that Spring Security is highly configurable. The example below shows only one extension (Session Management). There are several others filters and configuration options your requirements may warrant. Note that the official Spring-Webflow release has not been updated to fix a problem with overloading methods in the FacesContext that throw UnsupportedOperationExceptions, so the maven Pom included in the tutorial refers to a temporary build location for Spring-Webflow.

h3. Tutorial Use Case

The simple business case for this tutorial is a "product documentation" web application. Some of the documentation is publicly available and some of it requires authorization. To satisfy this requirement all content requiring authorization is accessible behind the spring intercept pattern "/secure/*"

h3. Technologies/Libraries Used

This tutorial uses Spring Security 3.1.2, Spring Framework 3.0.6, JSF 2.1.6 and ICEfaces 3.1. The project has been modified to use Maven as the build environment.

h3. Porting from Spring Security 3.0.6 and ICEFaces 3.0.1

In changing from the previous tutorial, users will note the following changes:

1) The *security-config.xml* file will need to name a new up-to-date Spring Security schema as in the following example:
{code:xml|title=Schema changes}<beans:beans xmlns=""
2) The *SessionManagementFilter* no longer supports an *invalidSessionURL* property. Instead, if so desired, there is a new *SessionManagement* element within the *<http>* element.

{code:xml|title=Session Management} <http>
<session-management invalid-session-url="/invalidSession.htm" />
With those changes in place, the tutorial operates as it did before.


{panel}Configuration Areas:

# [Configure Your Web.xml For Spring Security|#step1]
# [Configure Your applicationContext.xml For Spring Security|#step2]
# [Configure Your Web.xml For JSF|#step3]
# [Advanced: Configure Your Spring Security redirectStrategy|#step4]


h3. Part 1: Configure Web.xml For Spring Security

The web.xml below contains additional parameters showing where the spring configuration is located, the Spring Security filter chain, the context loader listener, and the SessionEventPublisher. These should be added to any existing ICEfaces web.xml configuration parameters show below in step 3.

{code:xml|title=web.xml}<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""


</listener> </web-app>

h3. Part 2: Configure Your security-config.xml For Spring Security

In this step we configure Spring Security in the file security-config.xml.
{code:xml|title=security-config.xml}<?xml version="1.0" encoding="UTF-8"?>

<beans:beans xmlns=""

<!-- Configure Spring Security -->
<http auto-config="false"

<form-login login-page="/spring/login"
default-target-url="/spring/main" always-use-default-target="true"
authentication-failure-url="/spring/login?login_error=1" />

<!-- When using custom filters, please make sure the positions do not conflict with default filters.
Alternatively you can disable the default filters by removing the corresponding child elements from
http and avoiding the use of http auto-config='true'. -->

<custom-filter ref="exceptionTranslationFilter" before="FILTER_SECURITY_INTERCEPTOR" />

<!--<session-management invalid-session-url="/spring/sessionExpired.jsf"/>-->

<logout logout-url="/spring/logout" logout-success-url="/spring/logoutSuccess"/>
<intercept-url pattern="/secure" method="POST" access="hasRole('ROLE_SUPERVISOR')"/>

Define local authentication provider, a real app would use an external provider (JDBC, LDAP, CAS, etc)

usernames/passwords are:
<password-encoder hash="md5"/>
<user name="keith" password="417c7382b16c395bc25b5da1398cf076"
<user name="erwin" password="12430911a8af075c6f41c6976af22b09"
<user name="jeremy" password="57c6cbff0d421449be820763f03139eb" authorities="ROLE_USER"/>
<user name="scott" password="942f2339bf50796de535a384f0d1af3e" authorities="ROLE_USER"/>

<beans:bean id="sessionManagementFilter"
<beans:constructor-arg name="securityContextRepository" ref="httpSessionSecurityContextRepository" />

<!-- -->
<beans:bean id="exceptionTranslationFilter" class="">
<beans:property name="accessDeniedHandler" ref="jsfAccessDeniedHandler"/>
<beans:property name="authenticationEntryPoint" ref="authenticationEntryPoint"/>

<beans:bean id="jsfAccessDeniedHandler" class="">
<beans:property name="loginPath" value="/spring/login" />
<beans:property name="contextRelative" value="true" />

<beans:bean id="authenticationEntryPoint" class="">
<beans:property name="loginFormUrl" value="/spring/login"/>
<beans:property name="redirectStrategy" ref="jsfRedirectStrategy" />
</beans:bean >

<beans:bean id="jsfRedirectStrategy" class=""/>
<beans:bean id="httpSessionSecurityContextRepository" class=""/>



h3. Part 3: Configure Web.xml For JSF

In this step we extend the web.xml to support JSF like we would any other ICEfaces application.
<servlet-name>Faces Servlet</servlet-name>

<servlet-name>Resource Servlet</servlet-name>

<servlet-name>Faces Servlet</servlet-name>

<servlet-name>Faces Servlet</servlet-name>

<servlet-name>Resource Servlet</servlet-name>

h3. Resources

For more information on Spring Security, please check out the content below:
* [Spring Security Tutorials||Spring Security Tutorials]
* [Spring Security Documentation||Spring Security Documentation]