您的位置:首页 > 编程语言 > Java开发

Spring Security集成CAS实现单点登录

2015-03-22 13:47 393 查看


1.1. https协议改成http


1.1.1. cas服务端改以下文件支持http协议

主要改进以下配置文件:

1) ticketGrantingTicketCookieGenerator.xml 配置文件

<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"

p:cookieSecure="false"

p:cookieMaxAge="-1"

p:rememberMeMaxAge="120960"

p:cookieName="CASTGC"

p:cookiePath="/cas" />

将p:cookieSecure="true"改为p:cookieSecure="false",这里我们已经改过了。

2) warnCookieGenerator.xml 配置文件

<bean id="warnCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"

p:cookieSecure="false"

p:cookieMaxAge="-1"

p:cookieName="CASPRIVACY"

p:cookiePath="/cas" />

同样将p:cookieSecure="true"改为p:cookieSecure="false"

3) deployerConfigContext.xml 配置文件:

<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler" p:httpClient-ref="httpClient" />

将以上的文件改为

<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler" p:httpClient-ref="httpClient" p:requireSecure="false"/>

加入p:requireSecure="false"

完成以上的配置,cas服务器就能支持http协议访问了


1.1.2. 增加ticket的有效性

ticketExpirationPolicies.xml配置文件

<bean id="grantingTicketExpirationPolicy" class="org.jasig.cas.ticket.support.RememberMeDelegatingExpirationPolicy">

<property name="sessionExpirationPolicy">

<bean class="org.jasig.cas.ticket.support.TimeoutExpirationPolicy">

<constructor-arg index="0" value="7200000" />

</bean>

</property>

<property name="rememberMeExpirationPolicy">

<bean class="org.jasig.cas.ticket.support.TimeoutExpirationPolicy">

<constructor-arg index="0" value="120960000" />

</bean>

</property>

</bean>

在ticketExpirationPolicies.xml配置文件中增加上面所述的代码,可以使得ticket有效性增长。


1.2. 配置cas客户端


1.2.1. 利用spring security使用cas

这里我们已经假设会使用spring security的基本用法,所以下面没有涉及到spring security的内容。

首先我们需要添加ServiceProperties的bean到我们的application context里面去

<beans:bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">

<beans:property name="service" value="http://172.16.3.151:8888/ms/j_spring_cas_security_check" />

<beans:property name="sendRenew" value="false" />

</beans:bean>

这里的service 必须是一个由CasAuthenticationFilter 监控的URL。这个sendRenew默认是false,但如果你的程序特别敏感就应该设置成true。这个参数作用是,告诉CAS登录服务,一个单点登录没有到达。否则,用户需要重新输入他们的用户名和密码,来获得访问服务的权限。Value值为应用的完整路径 + j_spring_cas_security_check ,当登录成功后会跳转到value指定的应用。

下面配置的bean 就是展开CAS 认证的过程:

<beans:bean id="casAuthenticationEntryPoint" class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">

<!-- loginUrl : cas完整的登录地址 -->

<beans:property name="loginUrl" value="http://172.16.2.176:8080/cas/login" />

<beans:property name="serviceProperties" ref="serviceProperties" />

</beans:bean>

<beans:bean id="casAuthenticationFilter" class="org.springframework.security.cas.web.CasAuthenticationFilter">

<beans:property name="authenticationManager" ref="casAuthenticationManager" />

</beans:bean>

<authentication-manager alias="casAuthenticationManager">

<authentication-provider ref="casAuthenticationProvider"/>

</authentication-manager>

<!-- 登录验证成功后获得用户权限信息的途径 -->

<beans:bean id="casAuthenticationProvider" class="org.springframework.security.cas.authentication.CasAuthenticationProvider">

<beans:property name="authenticationUserDetailsService" ref="authenticationUserDetailsService"/>

<beans:property name="serviceProperties" ref="serviceProperties"/>

<beans:property name="ticketValidator">

<beans:bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">

<!-- casServerUrlPrefix : CAS服务URL前缀 -->

<beans:constructor-arg index="0" value="http://172.16.2.176:8080/cas"/>

</beans:bean>

</beans:property>

<!-- key : 用于帮助CasAuthenticationProvider标识出先前认证的令牌 -->

<beans:property name="key" value="an_id_for_this_auth_provider_only"></beans:property>

</beans:bean>

<beans:bean id="authenticationUserDetailsService" class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">

<beans:property name="userDetailsService" ref="userDetailServiceImpl"/>

</beans:bean>

<context:annotation-config/>

<context:component-scan base-package="com.wtkj.pseccs.security"/>

上面的casAuthenticationFilter是我们自定义的过滤器,我们需要在代码中配置<custom-filter position="CAS_FILTER"ref="casAuthenticationFilter" />一旦通过了CAS 的认证,CasAuthenticationProvider 使用一个UserDetailsService

实例来加载用户的认证信息,这里我们使用的是自定义的UserDetailsService。


1.2.2. Session的管理

在配置cas的时候,出现的最大的问题就是session不起作用,原因就是我们没有使session-management真正起作用。

在application context里增加下面代码:

<beans:bean id="sessionAuthenticationStrategy"

class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">

<beans:constructor-arg name="sessionRegistry"

ref="sessionRegistry" />

<beans:property name="maximumSessions" value="1" />

</beans:bean>

<beans:bean id="sessionRegistry"

class="org.springframework.security.core.session.SessionRegistryImpl" />

<beans:bean id="concurrencyFilter"

class="org.springframework.security.web.session.ConcurrentSessionFilter">

<beans:property name="sessionRegistry" ref="sessionRegistry" />

<beans:property name="expiredUrl" value="/login.jsp" />

</beans:bean>

加入上面代码可以确保session-management能真正起作用。

上面的sessionAuthenticationStrategy和concurrencyFilter也是我们自定义的过滤器,同样也需要加入

<session-management

session-authentication-strategy-ref="sessionAuthenticationStrategy" />

<custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />

通过以上的配置我们就可以实现spring security结合cas实现单点登录机制了。

下面是session管理的时序图:




1.2.3. Cas登录退出配置

注:Cas退出后边完善


1.2.4. Maven需要增加的依赖

<dependency>

<groupId>cas</groupId>

<artifactId>casclient</artifactId>

<version>2.1.1</version>

</dependency>

<dependency>

<groupId>org.jasig.cas</groupId>

<artifactId>cas-client-core</artifactId>

<version>3.1.10</version>

</dependency>

<dependency>

<groupId>org.springframework.security</groupId>

<artifactId>spring-security-cas-client</artifactId>

<version>3.0.8.RELEASE</version>

</dependency>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: