---
title: "OpenJWeb平台Spring Security+CAS SSO的配置"
author: "cjc"
date: "2009-05-15T14:47:00Z"
tags: []
slug: "openjweb-spring-security-cas-sso"
draft: false
lastmod: "2009-05-15T14:47:00Z"
id: 446844
url: "https://cjc.iteye.com/blog/446844"
---

CAS Server的搭建就不用介绍了,这里介绍一下OpenJWeb平台中Spring Security如何与CAS集成.Spring security集成CAS的官方例子可从[https://src.springframework.org/svn/spring-security/trunk/samples/cas/client/src/main/webapp](https://src.springframework.org/svn/spring-security/trunk/samples/cas/client/src/main/webapp)下载,但是这个例子过于简单,权限ID是配置在xml中,而本文介绍的配置,权限ID是存储在数据库中的.下面是配置的applicationContext-security.xml(这个配置已测通):

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"  
xmlns:sec="[http://www.springframework.org/schema/security](http://www.springframework.org/schema/security)"  
xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
xsi:schemaLocation="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans-2.0.xsd](http://www.springframework.org/schema/beans/spring-beans-2.0.xsd)  
[http://www.springframework.org/schema/security](http://www.springframework.org/schema/security) [http://www.springframework.org/schema/security/spring-security-2.0.xsd](http://www.springframework.org/schema/security/spring-security-2.0.xsd)"&gt;

&lt;sec:http entry-point-ref="casProcessingFilterEntryPoint"&gt;  
&lt;sec:intercept-url pattern="/secure/extreme/\*\*" access="ROLE\_SUPERVISOR" requires-channel="https"/&gt;  
&lt;sec:intercept-url pattern="/secure/\*\*" access="ROLE\_USER" /&gt;  
&lt;sec:logout logout-success-url="/index.jsp"/&gt;  
&lt;/sec:http&gt;

&lt;sec:authentication-manager alias="authenticationManager"/&gt;

&lt;bean id="casProcessingFilter" class="org.springframework.security.ui.cas.CasProcessingFilter"&gt;  
&lt;sec:custom-filter after="CAS\_PROCESSING\_FILTER"/&gt;  
&lt;property name="authenticationManager" ref="authenticationManager"/&gt;  
&lt;property name="authenticationFailureUrl" value="/casfailed.jsp"/&gt;  
&lt;property name="defaultTargetUrl" value="/comm/index.action?operate=selectPageList"/&gt;  
&lt;property name="proxyGrantingTicketStorage" ref="proxyGrantingTicketStorage" /&gt;  
&lt;property name="proxyReceptorUrl" value="/secure/receptor" /&gt;

&lt;/bean&gt;

&lt;bean id="casProcessingFilterEntryPoint" class="org.springframework.security.ui.cas.CasProcessingFilterEntryPoint"&gt;  
&lt;property name="loginUrl" value="[https://casserver.haoyisheng.com:8443/cas/login"/](https://casserver.haoyisheng.com:8443/cas/login%22/)&gt;  
&lt;property name="serviceProperties" ref="serviceProperties"/&gt;  
&lt;/bean&gt;

&lt;bean id="casAuthenticationProvider" class="org.springframework.security.providers.cas.CasAuthenticationProvider"&gt;  
&lt;sec:custom-authentication-provider /&gt;  
&lt;property name="userDetailsService" ref="userDetailsService"/&gt;  
&lt;property name="serviceProperties" ref="serviceProperties" /&gt;  
&lt;property name="ticketValidator"&gt;  
&lt;bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator"&gt;  
&lt;constructor-arg index="0" value="[https://casserver.haoyisheng.com:8443/cas](https://casserver.haoyisheng.com:8443/cas)" /&gt;  
&lt;property name="proxyGrantingTicketStorage" ref="proxyGrantingTicketStorage" /&gt;  
&lt;property name="proxyCallbackUrl" value="[https://bzwang.haoyisheng.com:8443/crm/secure/receptor](https://bzwang.haoyisheng.com:8443/crm/secure/receptor)" /&gt;

&lt;/bean&gt;  
&lt;/property&gt;  
&lt;property name="key" value="an\_id\_for\_this\_auth\_provider\_only"/&gt;  
&lt;/bean&gt;

&lt;bean id="proxyGrantingTicketStorage" class="org.jasig.cas.client.proxy.ProxyGrantingTicketStorageImpl" /&gt;

&lt;bean id="serviceProperties" class="org.springframework.security.ui.cas.ServiceProperties"&gt;  
&lt;property name="service" value="[https://bzwang.haoyisheng.com:8443/crm/j\_spring\_cas\_security\_check"/](https://bzwang.haoyisheng.com:8443/crm/j_spring_cas_security_check%22/)&gt;  
&lt;property name="sendRenew" value="false"/&gt;  
&lt;/bean&gt;

&lt;bean id="daoAuthenticationProvider"  
class="org.springframework.security.providers.dao.DaoAuthenticationProvider"&gt;  
&lt;property name="userDetailsService" ref="userDetailsService" /&gt;  
&lt;property name="userCache" ref="userCache" /&gt;  
&lt;property name="passwordEncoder" ref="passwordEncoder" /&gt;  
&lt;/bean&gt;  
&lt;bean id="passwordEncoder"  
class="org.springframework.security.providers.encoding.Md5PasswordEncoder" /&gt;  
&lt;bean id="userDetailsService"  
class="org.openjweb.core.springsecurity.UserDetailsServiceImpl"&gt;  
&lt;constructor-arg&gt;  
&lt;ref bean="IBaseDao3" /&gt;  
&lt;/constructor-arg&gt;  
&lt;/bean&gt;

&lt;bean id="userCache"  
class="org.springframework.security.providers.dao.cache.EhCacheBasedUserCache"&gt;  
&lt;property name="cache" ref="userCacheBacked" /&gt;  
&lt;/bean&gt;

&lt;bean id="userCacheBacked"  
class="org.springframework.cache.ehcache.EhCacheFactoryBean"&gt;  
&lt;property name="cacheManager" ref="cacheManager" /&gt;  
&lt;property name="cacheName" value="userCache" /&gt;  
&lt;/bean&gt;

&lt;bean id="cacheManager"  
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"&gt;  
&lt;property name="configLocation"  
value="classpath:ehcache-security.xml" /&gt;  
&lt;/bean&gt;  
&lt;bean id="filterSecurityInterceptor"  
class="org.springframework.security.intercept.web.FilterSecurityInterceptor"&gt;  
&lt;sec:custom-filter before="FILTER\_SECURITY\_INTERCEPTOR" /&gt;  
&lt;property name="authenticationManager"  
ref="authenticationManager" /&gt;  
&lt;property name="accessDecisionManager"  
ref="accessDecisionManager" /&gt;  
&lt;property name="alwaysReauthenticate" value="true" /&gt;  
&lt;property name="objectDefinitionSource"  
ref="databaseFilterInvocationDefinitionSource" /&gt;  
&lt;/bean&gt;  
&lt;bean id="accessDecisionManager"  
class="org.springframework.security.vote.AffirmativeBased"&gt;  
&lt;property name="decisionVoters"&gt;  
&lt;list&gt;  
&lt;bean  
class="org.springframework.security.vote.RoleVoter"&gt;  
&lt;property name="rolePrefix" value="" /&gt;  
&lt;/bean&gt;  
&lt;/list&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;bean id="databaseFilterInvocationDefinitionSource"  
class="org.springframework.security.intercept.web.DefaultFilterInvocationDefinitionSource"&gt;  
&lt;constructor-arg  
type="org.springframework.security.util.UrlMatcher"  
ref="antUrlPathMatcher" /&gt;  
&lt;constructor-arg type="java.util.LinkedHashMap" ref="requestMap" /&gt;  
&lt;/bean&gt;

&lt;bean id="antUrlPathMatcher"  
class="org.springframework.security.util.AntUrlPathMatcher" /&gt;

&lt;bean id="requestMap"  
class="org.openjweb.core.springsecurity.RequestMapFactoryBean"  
init-method="init"&gt;

&lt;/bean&gt;

&lt;/beans&gt;

说明:(1)SSO认证入口为/secure/index.jsp,这个文件有个重定向语句,作用是当SSO认证通过后跳转到系统主页面.在测试过程中发现只有访问/secure目录下jsp才自动到cas server认证,sec:intercept-url 配置其他的目录不跳转到cas server进行认证,不知道是什么原因.

(2) cas server采用3.3.2版本

(3)client端为cas-client-core-3.1.3.jar

作者QQ:29803446

Msn:baozhengw999@hotmail.com