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

1. 需要准备的jar

spring-security-core-3.0.8.RELEASE.jar
spring-security-web-3.0.8.RELEASE.jar
spring-security-config-3.0.8.RELEASE.jar
spring-security-cas-client-3.0.8.RELEASE.jar
cas-client-core-3.2.1.jar (jasig)

  

2. web.xml

  web.xml中加入context param 和 filter

<!--context configuration-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springSecurity.xml</param-value>
</context-param>
<!-- Spring security filter -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

3. spring security cas-client配置

  springSecurity.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd"> <!-- cas拦截规则 -->
<security:http auto-config="true" entry-point-ref="casAuthEntryPoint" servlet-api-provision="true">
<security:intercept-url pattern="/login.do" access="ROLE_USER,ROLE_ADMIN"/>
<security:intercept-url pattern="/report/*.do" access="ROLE_USER,ROLE_ADMIN"/>
<security:intercept-url pattern="/packages/*add.do" access="ROLE_USER,ROLE_ADMIN"/>
<security:intercept-url pattern="/packages/*delete.do" access="ROLE_USER,ROLE_ADMIN"/>
<security:intercept-url pattern="/packages/*edit.do" access="ROLE_USER,ROLE_ADMIN"/>
<security:intercept-url pattern="/resource/*.do" access="ROLE_USER,ROLE_ADMIN"/>
<security:intercept-url pattern="/equip/*.do" access="ROLE_ADMIN"/>
<security:custom-filter ref="casAuthenticationFilter" position="CAS_FILTER"/>
<security:custom-filter ref="requestSingleLogoutFilter" before="LOGOUT_FILTER" />
<security:custom-filter ref="singleLogoutFilter" before="CAS_FILTER" />
</security:http> <!-- CAS认证切入点,声明cas服务器端登录的地址 -->
<bean id="casAuthEntryPoint" class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">
     <!-- 此处loginUrl必须和浏览器中访问的地址一致 -->
<property name="loginUrl" value="http://10.10.144.51:8081/cas"/>
<property name="serviceProperties" ref="casService"/>
</bean> <!-- 在认证管理器中注册cas认证提供器 -->
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider ref="casAuthenticationProvider" />
</security:authentication-manager> <!-- 登录成功后的返回地址 -->
<bean id="casService" class="org.springframework.security.cas.ServiceProperties">
<property name="service" value="http://10.10.144.130:8080/provisioningApp/j_spring_cas_security_check"/>
<property name="sendRenew" value="false" />
</bean> <!-- cas 认证过滤器 -->
<bean id="casAuthenticationFilter" class="org.springframework.security.cas.web.CasAuthenticationFilter">
<property name="authenticationManager" ref="authenticationManager"/>
<property name="authenticationFailureHandler" ref="authenticationFailureHandler" />
<property name="authenticationSuccessHandler" ref="authenticationSuccessHandler" />
<property name="filterProcessesUrl" value="/j_spring_cas_security_check" />
</bean> <!-- cas 认证失败控制器 -->
<bean id="authenticationFailureHandler"
class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
<property name="defaultFailureUrl" value="/error.jsp" />
</bean>
<!-- cas 认证成功控制器 -->
<bean id="authenticationSuccessHandler"
class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">
<property name="alwaysUseDefaultTargetUrl" value="true" />
<property name="defaultTargetUrl" value="/sim/init.do" />
</bean> <!-- 注销客户端 -->
<bean id="singleLogoutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter" /> <!-- 注销服务器端 -->
<bean id="requestSingleLogoutFilter"
class="org.springframework.security.web.authentication.logout.LogoutFilter">
<constructor-arg value="http://10.10.144.51:8081/cas/logout" />
<constructor-arg>
<bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" />
</constructor-arg>
<property name="filterProcessesUrl" value="/j_spring_cas_security_logout" />
</bean> <!-- cas认证提供器,定义客户端的验证方式 -->
<bean id="casAuthenticationProvider" class="org.springframework.security.cas.authentication.CasAuthenticationProvider">
<property name="ticketValidator" ref="casTicketValidator"></property>
<property name="serviceProperties" ref="casService"></property>
<property name="key" value="an_id_for_this_auth_provider_only"></property>
<property name="authenticationUserDetailsService" ref="authenticationUserDetailsService"></property>
</bean>
<!-- 配置ticket validator -->
<bean id="casTicketValidator" class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
<constructor-arg value="http://10.10.144.51:8081/cas/"/>
</bean> <bean id="authenticationUserDetailsService" class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
<property name="userDetailsService" ref="jdbcUserService"/>
</bean>

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

  <!-- 定义数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://10.10.144.51:3306/prvn"></property>
<property name="minPoolSize" value="1"></property>
<property name="maxPoolSize" value="25"></property>
<property name="initialPoolSize" value="3"></property>
<property name="acquireIncrement" value="1"></property>
<property name="maxIdleTime" value="600"></property>
<property name="idleConnectionTestPeriod" value="0"></property>
<property name="acquireRetryAttempts" value="30"></property>
<property name="acquireRetryDelay" value="1000"></property>
<property name="breakAfterAcquireFailure" value="true"></property>
<property name="checkoutTimeout" value="0"></property>
<property name="testConnectionOnCheckout" value="false"></property>
<property name="properties">
<props>
<prop key="user">pvnUser</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</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. Arraylist<E>

    ArrayList 应该是大部分人接触JCF之后, 第一个熟悉和使用的类.它的特点主要有一下几个: 1. 基于数组 public ArrayList(int initialCapacity) { if ...

  2. MVC OF UEditor 图片上传- 额外参数 笔记

    最近正巧需要用到UEditor ,因为需求,.需要把上传的图片数据上传到阿里云的OSS与记录图片相关信息到MS SQL中 . 不得已只能翻UEditor的实现代码>_<痛苦. 备忘笔记: ...

  3. ZOJ2604-DP

    递推式并不知自己推出来的,用来纪念一下学会了java的函数用法... 最后5分钟的时候,大姐头直接告诉我打表,但是我当场就蒙了,我怎么会用java打表(其实自己脑中想的就是要输出到文件中然后生成数组打 ...

  4. PHP与Golang如何通信?

    PHP与Golang如何通信? 最近遇到的一个场景:php项目中需要使用一个第三方的功能(结巴分词),而github上面恰好有一个用Golang写好的类库.那么问题就来了,要如何实现不同语言之间的通信 ...

  5. JS控制的动态表格

    对应的js: function pccAddSignRow() { //读取最后一行的行号,存放在LearnTRLastIndex文本框中 var pccTRLastIndex = findObj(& ...

  6. Voreen (一) GPU Raycast主流程

    最近看了下Voreen这个开源体绘制快速原型开发包.Voreen里面集合了不少体绘制的基本算法,还是值得研究和试验图像显示效果的.今天第一篇以它自带工程为例,介绍下DICOM数据读取和GPU Rayc ...

  7. 外边距(padding)重叠的及解决办法

    两个或多个块级盒子的垂直相邻边界会重合.结果的边界宽度是相邻边界宽度中最大的值.如果出现负边界,则在最大的正边界中减去绝对值最大的负边界.如果没有正边界,则从零中减去绝对值最大的负边界.注意:相邻的盒 ...

  8. python 输出十六进制中文乱码

    代码中红色字体为解决方案: #-*-coding:utf-8-* import csv filename='C:\Users\yaru\Desktop\Senti_Value(1).csv' data ...

  9. CCI4.5/LintCode Validate Binary Search Tree

    Validate BST是指按中序遍历niorder后左<node<右: 第一种方法: 先按inoreder遍历, 再放进ArrayList里用循环看是不是从小到大排序: 注意: 设置成员 ...

  10. 解决href标签跳转到WEB-INF下的jsp页面的办法

    用的配置struts的action的方法. jsp页面这样写的. class里面的路径是下面的struts配置文件的路径 struts配置文件里这样写的. name里面的内容要和href里面的内用一致 ...