SpringSecurity3整合CAS实现单点登录

简介:

SpringSecurity本身已经做好了与CAS的集成工作,只需要我们做简单配置就可以了

步骤1 spring-cas.xml配置文件内容如下(完整版)

 

 
 
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans:beans xmlns="http://www.springframework.org/schema/security" 
  3.     xmlns:context="http://www.springframework.org/schema/context" 
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
  5.     xmlns:beans="http://www.springframework.org/schema/beans" 
  6.     xsi:schemaLocation="  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  7.            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  8.            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"  
  9.     default-lazy-init="true"> 
  10.     <context:component-scan base-package="com.itec.core" /> 
  11. <!--SSO --> 
  12.     <http auto-config="false" entry-point-ref="casEntryPoint" servlet-api-provision="true">    
  13.         <intercept-url pattern="/login.do" filters="none" /> 
  14.         <intercept-url pattern="/image.do" filters="none" /> 
  15.         <intercept-url pattern="/admin/*.do*" access="ROLE_LOGIN" />   
  16.         <!-- logout-success-url="/login.html" -->    
  17. <!--        <logout logout-url="/login.do" success-handler-ref="casLogoutSuccessHandler"/>   --> 
  18.         <custom-filter ref="requestSingleLogoutFilter" before="LOGOUT_FILTER" />   
  19.         <custom-filter position="FORM_LOGIN_FILTER" ref="casFilter"/>    
  20.         <custom-filter ref="singleLogoutFilter" before="CAS_FILTER" /> 
  21.     </http>   
  22.  
  23.     <beans:bean id="casEntryPoint"  class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">    
  24.         <beans:property name="loginUrl" value="http://172.19.50.21:9083/HASLSSO/login"/>    
  25.         <beans:property name="serviceProperties" ref="serviceProperties"/>    
  26.     </beans:bean> 
  27.     <beans:bean id="serviceProperties"  class="org.springframework.security.cas.ServiceProperties">    
  28.         <beans:property name="service"  value="http://172.19.4.225:8080/HACMS/j_spring_cas_security_check"/>    
  29.         <beans:property name="sendRenew" value="false"/>    
  30.     </beans:bean> 
  31.  
  32.     <beans:bean id="casFilter"  class="org.springframework.security.cas.web.CasAuthenticationFilter">    
  33.         <beans:property name="authenticationManager" ref="authenticationManager"/>    
  34.     </beans:bean>    
  35.         
  36.     <authentication-manager alias="authenticationManager">    
  37.         <authentication-provider ref="casAuthenticationProvider"/>   
  38.     </authentication-manager>    
  39.         
  40.     <beans:bean id="casAuthenticationUserDetailsService" class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">    
  41.         <beans:property name="userDetailsService" >    
  42.             <beans:ref bean="userDetailsManager" />    
  43.         </beans:property>    
  44.     </beans:bean>    
  45.        
  46.     <beans:bean id="casAuthenticationProvider"    
  47.             class="org.springframework.security.cas.authentication.CasAuthenticationProvider">    
  48.         <beans:property name="authenticationUserDetailsService" ref="casAuthenticationUserDetailsService"/>    
  49.         <beans:property name="serviceProperties" ref="serviceProperties" />    
  50.         <beans:property name="ticketValidator">    
  51.             <beans:bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">    
  52.                 <beans:constructor-arg index="0" value="http://172.19.50.21:9083/HASLSSO" />    
  53.             </beans:bean>    
  54.         </beans:property>    
  55.         <beans:property name="key" value="an_id_for_this_auth_provider_only"/>    
  56.     </beans:bean>    
  57.  
  58.     <!-- 注销客户端 --> 
  59.     <beans:bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter" /> 
  60.  
  61.     <!-- 注销服务器端 --> 
  62.     <beans:bean id="requestSingleLogoutFilter" 
  63.     class="org.springframework.security.web.authentication.logout.LogoutFilter"> 
  64.     <beans:constructor-arg 
  65.     value="http://172.19.50.21:9083/HASLSSO/logout" /> 
  66.     <beans:constructor-arg> 
  67.     <beans:bean 
  68.     class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"/> 
  69.     </beans:constructor-arg> 
  70.     <beans:property name="filterProcessesUrl" value="/j_spring_cas_security_logout" /> 
  71.     </beans:bean> 
  72.  
  73. </beans:beans>    

 

步骤2 之前的UserDetailsManager不需要改任何代码

 

 
 
  1. @Service 
  2. public class UserDetailsManager implements UserDetailsService { 

步骤3 web.xml需要修改一点东西,不加载Security的配置文件就行了

 

 
 
  1. <context-param> 
  2.         <param-name>contextConfigLocation</param-name> 
  3.         <!-- 使用工程本身验证 --> 
  4.         <param-value>/WEB-INF/spring-config.xml,/WEB-INF/spring-freemarker.xml,/WEB-INF/spring-jpa.xml,/WEB-INF/spring-security.xml</param-value> 
  5.         <!-- 使用 SSO 验证 --> 
  6. <!--        <param-value>/WEB-INF/spring-config.xml,/WEB-INF/spring-freemarker.xml,/WEB-INF/spring-jpa.xml,/WEB-INF/spring-cas.xml</param-value> --> 
  7.     </context-param> 

大功告成~!










本文转自 tony_action 51CTO博客,原文链接:http://blog.51cto.com/tonyaction/898173,如需转载请自行联系原作者
目录
相关文章
|
5天前
|
安全 Java API
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(上)
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)
24 0
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(上)
|
1月前
|
存储 安全 Java
Spring Boot整合Spring Security--学习笔记
Spring Boot整合Spring Security--学习笔记
53 0
|
16天前
|
安全 数据安全/隐私保护
Springboot+Spring security +jwt认证+动态授权
Springboot+Spring security +jwt认证+动态授权
|
2天前
|
安全 Java 数据安全/隐私保护
|
5天前
|
存储 安全 Java
第10章 Spring Security 的未来趋势与高级话题(2024 最新版)(下)
第10章 Spring Security 的未来趋势与高级话题(2024 最新版)
17 2
|
5天前
|
安全 Cloud Native Java
第10章 Spring Security 的未来趋势与高级话题(2024 最新版)(上)
第10章 Spring Security 的未来趋势与高级话题(2024 最新版)
22 2
|
5天前
|
安全 Java API
第5章 Spring Security 的高级认证技术(2024 最新版)(上)
第5章 Spring Security 的高级认证技术(2024 最新版)
29 0
|
5天前
|
存储 安全 Java
第3章 Spring Security 的用户认证机制(2024 最新版)(下)
第3章 Spring Security 的用户认证机制(2024 最新版)
29 0
|
5天前
|
存储 安全 Java
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
15 0
|
5天前
|
安全 Java 数据库
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(上)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)
28 0