spring security 3整合cas client用于实现各Application之间的单点登录。

1. 需要准备的jar

  1. spring-security-core-3.0.8.RELEASE.jar
  2. spring-security-web-3.0.8.RELEASE.jar
  3. spring-security-config-3.0.8.RELEASE.jar
  4. spring-security-cas-client-3.0.8.RELEASE.jar
  5. cas-client-core-3.2.1.jar (jasig)

  

2. web.xml

  web.xml中加入context param 和 filter

  1. <!--context configuration-->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>classpath:springSecurity.xml</param-value>
  5. </context-param>
  6. <!-- Spring security filter -->
  7. <filter>
  8. <filter-name>springSecurityFilterChain</filter-name>
  9. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  10. </filter>
  11. <filter-mapping>
  12. <filter-name>springSecurityFilterChain</filter-name>
  13. <url-pattern>/*</url-pattern>
  14. </filter-mapping>

3. spring security cas-client配置

  springSecurity.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:security="http://www.springframework.org/schema/security"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:beans="http://www.springframework.org/schema/beans"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/security
  9. http://www.springframework.org/schema/security/spring-security-3.0.xsd">
  10.  
  11. <!-- cas拦截规则 -->
  12. <security:http auto-config="true" entry-point-ref="casAuthEntryPoint" servlet-api-provision="true">
  13. <security:intercept-url pattern="/login.do" access="ROLE_USER,ROLE_ADMIN"/>
  14. <security:intercept-url pattern="/report/*.do" access="ROLE_USER,ROLE_ADMIN"/>
  15. <security:intercept-url pattern="/packages/*add.do" access="ROLE_USER,ROLE_ADMIN"/>
  16. <security:intercept-url pattern="/packages/*delete.do" access="ROLE_USER,ROLE_ADMIN"/>
  17. <security:intercept-url pattern="/packages/*edit.do" access="ROLE_USER,ROLE_ADMIN"/>
  18. <security:intercept-url pattern="/resource/*.do" access="ROLE_USER,ROLE_ADMIN"/>
  19. <security:intercept-url pattern="/equip/*.do" access="ROLE_ADMIN"/>
  20. <security:custom-filter ref="casAuthenticationFilter" position="CAS_FILTER"/>
  21. <security:custom-filter ref="requestSingleLogoutFilter" before="LOGOUT_FILTER" />
  22. <security:custom-filter ref="singleLogoutFilter" before="CAS_FILTER" />
  23. </security:http>
  24.  
  25. <!-- CAS认证切入点,声明cas服务器端登录的地址 -->
  26. <bean id="casAuthEntryPoint" class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">
         <!-- 此处loginUrl必须和浏览器中访问的地址一致 -->
  27. <property name="loginUrl" value="http://10.10.144.51:8081/cas"/>
  28. <property name="serviceProperties" ref="casService"/>
  29. </bean>
  30.  
  31. <!-- 在认证管理器中注册cas认证提供器 -->
  32. <security:authentication-manager alias="authenticationManager">
  33. <security:authentication-provider ref="casAuthenticationProvider" />
  34. </security:authentication-manager>
  35.  
  36. <!-- 登录成功后的返回地址 -->
  37. <bean id="casService" class="org.springframework.security.cas.ServiceProperties">
  38. <property name="service" value="http://10.10.144.130:8080/provisioningApp/j_spring_cas_security_check"/>
  39. <property name="sendRenew" value="false" />
  40. </bean>
  41.  
  42. <!-- cas 认证过滤器 -->
  43. <bean id="casAuthenticationFilter" class="org.springframework.security.cas.web.CasAuthenticationFilter">
  44. <property name="authenticationManager" ref="authenticationManager"/>
  45. <property name="authenticationFailureHandler" ref="authenticationFailureHandler" />
  46. <property name="authenticationSuccessHandler" ref="authenticationSuccessHandler" />
  47. <property name="filterProcessesUrl" value="/j_spring_cas_security_check" />
  48. </bean>
  49.  
  50. <!-- cas 认证失败控制器 -->
  51. <bean id="authenticationFailureHandler"
  52. class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
  53. <property name="defaultFailureUrl" value="/error.jsp" />
  54. </bean>
  55. <!-- cas 认证成功控制器 -->
  56. <bean id="authenticationSuccessHandler"
  57. class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">
  58. <property name="alwaysUseDefaultTargetUrl" value="true" />
  59. <property name="defaultTargetUrl" value="/sim/init.do" />
  60. </bean>
  61.  
  62. <!-- 注销客户端 -->
  63. <bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter" />
  64.  
  65. <!-- 注销服务器端 -->
  66. <bean id="requestSingleLogoutFilter"
  67. class="org.springframework.security.web.authentication.logout.LogoutFilter">
  68. <constructor-arg value="http://10.10.144.51:8081/cas/logout" />
  69. <constructor-arg>
  70. <bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" />
  71. </constructor-arg>
  72. <property name="filterProcessesUrl" value="/j_spring_cas_security_logout" />
  73. </bean>
  74.  
  75. <!-- cas认证提供器,定义客户端的验证方式 -->
  76. <bean id="casAuthenticationProvider" class="org.springframework.security.cas.authentication.CasAuthenticationProvider">
  77. <property name="ticketValidator" ref="casTicketValidator"></property>
  78. <property name="serviceProperties" ref="casService"></property>
  79. <property name="key" value="an_id_for_this_auth_provider_only"></property>
  80. <property name="authenticationUserDetailsService" ref="authenticationUserDetailsService"></property>
  81. </bean>
  82. <!-- 配置ticket validator -->
  83. <bean id="casTicketValidator" class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
  84. <constructor-arg value="http://10.10.144.51:8081/cas/"/>
  85. </bean>
  86.  
  87. <bean id="authenticationUserDetailsService" class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
  88. <property name="userDetailsService" ref="jdbcUserService"/>
  89. </bean>

  90.   <!-- 这里没有实现custom service就直接采用security自带的jdbcdao实现来进行数据库操作,这里我们重写了sql -->
  91. <bean id="jdbcUserService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
  92. <property name="dataSource" ref="dataSource"/>
  93. <property name="enableGroups" value="true"/>
  94. <property name="enableAuthorities" value="false"/>
         <!-- sql查询字段的个数必须与源码中的字段数对应起来,否则会报错 -->
  95. <property name="usersByUsernameQuery">
  96. <value>
  97. select username, password,1 from user where username = ?
  98. </value>
  99. </property>
  100. <property name="groupAuthoritiesByUsernameQuery">
  101. <value>
  102. select username, department, authority from user where username = ?
  103. </value>
  104. </property>
  105. </bean>

  106.   <!-- 定义数据源 -->
  107. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  108. <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
  109. <property name="jdbcUrl" value="jdbc:mysql://10.10.144.51:3306/prvn"></property>
  110. <property name="minPoolSize" value="1"></property>
  111. <property name="maxPoolSize" value="25"></property>
  112. <property name="initialPoolSize" value="3"></property>
  113. <property name="acquireIncrement" value="1"></property>
  114. <property name="maxIdleTime" value="600"></property>
  115. <property name="idleConnectionTestPeriod" value="0"></property>
  116. <property name="acquireRetryAttempts" value="30"></property>
  117. <property name="acquireRetryDelay" value="1000"></property>
  118. <property name="breakAfterAcquireFailure" value="true"></property>
  119. <property name="checkoutTimeout" value="0"></property>
  120. <property name="testConnectionOnCheckout" value="false"></property>
  121. <property name="properties">
  122. <props>
  123. <prop key="user">pvnUser</prop>
  124. <prop key="password">123456</prop>
  125. </props>
  126. </property>
  127. </bean>
  128. </beans>

Spring Security 3整合CAS 实现SSO的更多相关文章

  1. CAS Spring Security 3 整合配置(转)

    一般来说, Web 应用的安全性包括用户认证( Authentication )和用户授权( Authorization )两个部分.用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否 ...

  2. SpringCloud微服务实战——搭建企业级开发框架(四十):使用Spring Security OAuth2实现单点登录(SSO)系统

    一.单点登录SSO介绍   目前每家企业或者平台都存在不止一套系统,由于历史原因每套系统采购于不同厂商,所以系统间都是相互独立的,都有自己的用户鉴权认证体系,当用户进行登录系统时,不得不记住每套系统的 ...

  3. Spring Security(20)——整合Cas

    整合Cas 目录 1.1           配置登录认证 1.1.1     配置AuthenticationEntryPoint 1.1.2     配置CasAuthenticationFilt ...

  4. Spring Security 集成CAS实现单点登录

    参考:http://elim.iteye.com/blog/2270446 众所周知,Cas是对单点登录的一种实现.本文假设读者已经了解了Cas的原理及其使用,这些内容在本文将不会讨论.Cas有Ser ...

  5. Spring Boot:整合Spring Security

    综合概述 Spring Security 是 Spring 社区的一个顶级项目,也是 Spring Boot 官方推荐使用的安全框架.除了常规的认证(Authentication)和授权(Author ...

  6. Spring Security 整合JWT(四)

    一.前言 本篇文章将讲述Spring Security 简单整合JWT 处理认证授权 基本环境 spring-boot 2.1.8 mybatis-plus 2.2.0 mysql 数据库 maven ...

  7. 使用Spring MVC测试Spring Security Oauth2 API

    不是因为看到希望了才去坚持,而坚持了才知道没有希望. 前言 在Spring Security源码分析十一:Spring Security OAuth2整合JWT和Spring Boot 2.0 整合 ...

  8. Spring Boot2.0使用Spring Security

     一.Spring Secutity简介     Spring 是一个非常流行和成功的 Java 应用开发框架.Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性 ...

  9. Spring Security 认证执行流程

    本文基于 Spring Security 5.x 推荐阅读: 项目集成Spring Security SpringSecurity 整合 JWT 一.外层-正常登陆调用 项目启动后会自动寻找 User ...

随机推荐

  1. Linux内核分析——进程描述与创建

    20135125陈智威 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验内容 ...

  2. LeetCode 7 -- String to Integer (atoi)

    Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...

  3. 【转】C#调用DLL

    C#中如何调用动态链接库DLL(转)     每种编程语言调用DLL的方法都不尽相同,在此只对用C#调用DLL的方法进行介绍.首先,您需要了解什么是托管,什么是非托管.一般可以认为:非托管代码主要是基 ...

  4. c++构造函数 对象初始化

    最近查看了关于c++构造函数的博客,为了防止关键知识的遗忘,特此记录一些要点,以便于今后的查阅. 如果不主动书写构造函数,c++或默认提供一般构造函数,拷贝构造函数以及复制运算符的操作.一般的构造函数 ...

  5. (zhuan)Python 虚拟环境:Virtualenv

    Python 虚拟环境:Virtualenv zhuanzi: http://liuzhijun.iteye.com/blog/1872241 virtualenv virtualenv用于创建独立的 ...

  6. MySql数据库忘记root密码

    以windows为例: 1. 关闭正在运行的MySQL服务.(services.msc运行停止服务) 2. 打开DOS窗口,转到mysql\bin目录.(输入cd..返回到c盘根目录下,一般MySQL ...

  7. mtk开发

    mtk套接字所有的声明放在soc_api.h 条件编译命令最常见的形式为: ? 1 2 3 4 5 #ifdef标识符 //程序段1 #else //程序段2 #endif 它的作用是:当标识符已经被 ...

  8. SQL --Chapter02 查询基础

    SELECT 语句基础 SELECT <列名>,….. FROM <表名>; 查询全部列: SELECT * FROM <表名>; 使用AS关键字为列设置别名,设定 ...

  9. Android SDK代理服务器解决国内Android SDK不能更新下载问题

  10. java编程经验积累

    1.java批量删除checkbox中选中的对象-CSDN论坛-CSDN.NET-中国最大的IT技术社区  http://bbs.csdn.net/topics/360223125 2.重定向与转发路 ...