CAS Spring Security Example

Example Spring Security Configuration for Applications

The example below is a stripped-down web.xml and Spring Application context that is used to demonstrate configuring Spring Security for Java. This client is significantly more advanced than the basic JASIG CAS Client for Java.

For more advanced usage of this CAS client, please see the official documentation.

Example web.xml

  1. <?xml version=“1.0” encoding=“ISO-8859-1”?>
  2. <web-app xmlns=“http://java.sun.com/xml/ns/j2ee” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=“http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd” version=“2.4”>
  3.     <display-name>Sample Application</display-name>
  4.     <context-param>
  5.         <param-name>contextConfigLocation</param-name>
  6.         <param-value>/WEB-INF/securityContext.xml</param-value>
  7.     </context-param>
  8.     <filter>
  9.         <filter-name>springSecurityFilterChain</filter-name>
  10.         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  11.     </filter>
  12.     <filter-mapping>
  13.         <filter-name>springSecurityFilterChain</filter-name>
  14.         <url-pattern>/services/*</url-pattern>
  15.     </filter-mapping>
  16.     <listener>
  17.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  18.     </listener>
  19.     <servlet>
  20.         <servlet-name>myservlet</servlet-name>
  21.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  22.         <init-param>
  23.             <param-name>publishContext</param-name>
  24.             <param-value>false</param-value>
  25.         </init-param>
  26.         <load-on-startup>1</load-on-startup>
  27.     </servlet>
  28.     <servlet-mapping>
  29.         <servlet-name>myservlet</servlet-name>
  30.         <url-pattern>/*</url-pattern>
  31.     </servlet-mapping>
  32.     <session-config>
  33.         <!– Default to 5 minute session timeouts –>
  34.         <session-timeout>5</session-timeout>
  35.     </session-config>
  36.     <welcome-file-list>
  37.         <welcome-file>index.jsp</welcome-file>
  38.     </welcome-file-list>
  39. </web-app>

Example securityContext.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.        xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4.        xmlns:p=“http://www.springframework.org/schema/p”
  5.        xmlns:tx=“http://www.springframework.org/schema/tx”
  6.        xmlns:sec=“http://www.springframework.org/schema/security”
  7.        xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  8.        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
  9.        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.1.xsd”>
  10.     <description>
  11.         This is the configuration file for the Spring Security configuration used with the sample application.
  12.     </description>
  13.     <sec:http entry-point-ref=“casProcessingFilterEntryPoint”>
  14.         <sec:intercept-url pattern=“/**” access=“ROLE_ADMIN” />
  15.         <sec:logout logout-url=“/logout.html” logout-success-url=“/loggedOut.html” />
  16.     </sec:http>
  17.     <sec:authentication-manager alias=“casAuthenticationManager”/>
  18.     <bean id=“serviceProperties” class=“org.springframework.security.ui.cas.ServiceProperties”
  19.         p:service=“https://myservice.rutgers.edu/j_acegi_cas_security_check”
  20.         p:sendRenew=“false” />
  21.     <bean id=“casProcessingFilter” class=“org.springframework.security.ui.cas.CasProcessingFilter”
  22.         p:authenticationManager-ref=“casAuthenticationManager”
  23.         p:authenticationFailureUrl=“/authorizationFailure.jsp”
  24.         p:alwaysUseDefaultTargetUrl=“true”
  25.         p:filterProcessesUrl=“/j_acegi_cas_security_check”
  26.         p:defaultTargetUrl=“/manage.html”>
  27.         <sec:custom-filter after=“CAS_PROCESSING_FILTER” />
  28.     </bean>
  29.     <bean id=“casProcessingFilterEntryPoint” class=“org.springframework.security.ui.cas.CasProcessingFilterEntryPoint”
  30.         p:loginUrl=“https://test-cas.rutgers.edu/login”
  31.         p:serviceProperties-ref=“serviceProperties” />
  32.     <bean id=“casAuthenticationProvider” class=“org.springframework.security.providers.cas.CasAuthenticationProvider”
  33.         p:key=“my_password_for_this_auth_provider_only”
  34.         p:serviceProperties-ref=“serviceProperties”
  35.         p:userDetailsService-ref=“userDetailsService”>
  36.         <sec:custom-authentication-provider />
  37.         <property name=“ticketValidator”>
  38.           <bean class=“org.jasig.cas.client.validation.Cas20ServiceTicketValidator”>
  39.             <constructor-arg index=“0” value=“https://test-cas.rutgers.edu” />
  40.             </bean>
  41.         </property>
  42.     </bean>
  43. </beans>

Note: the example securityContext.xml doesn’t include a userDetailsService bean. You’ll need to add one that points to your data store.

Related Articles