Spring Security整合CAS的示例代码

 更新时间:2018年07月06日 16:25:42   作者:乱世浮生  
本篇文章主要介绍了Spring Security整合CAS的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

这里使用的是spring-security和原生的jasig cas包来进行整合,为什么没有直接使用spring提供的spring-security-cas,后面会进行解释。

配置

web.xml

<filter>
 <filter-name>casFilterChain</filter-name>
 <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
 <filter-name>casFilterChain</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
 <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
</listener>

applicationContext-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:security="http://www.springframework.org/schema/security"
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/security
  http://www.springframework.org/schema/security/spring-security-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

 <bean id="casFilterChain" class="org.springframework.security.web.FilterChainProxy">
  <constructor-arg>
   <util:list>
    <security:filter-chain pattern="/**" filters="singleSignOutFilter, cas20ProxyReceivingTicketValidationFilter, authenticationFilter, httpServletRequestWrapperFilter, assertionThreadLocalFilter"/>
   </util:list>
  </constructor-arg>
 </bean>

 <bean id="singleSignOutFilter" class="org.jasig.cas.client.session.SingleSignOutFilter"/>

 <bean id="cas20ProxyReceivingTicketValidationFilter"
   class="org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter">
  <property name="serverName" value="${client.url}"/>
  <property name="ticketValidator" ref="cas20ServiceTicketValidator"/>
 </bean>

 <bean id="cas20ServiceTicketValidator" class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
  <constructor-arg value="${cas.url}"/>
  <property name="renew" value="false"/>
 </bean>

 <bean id="authenticationFilter" class="org.jasig.cas.client.authentication.AuthenticationFilter">
  <property name="renew" value="false"/>
  <property name="casServerLoginUrl" value="${cas.url}"/>
  <property name="serverName" value="${client.url}"/>
 </bean>

 <bean id="httpServletRequestWrapperFilter" class="org.jasig.cas.client.util.HttpServletRequestWrapperFilter"/>

 <bean id="assertionThreadLocalFilter" class="org.jasig.cas.client.util.AssertionThreadLocalFilter"/>

</beans>

properties

#CAS服务地址
cas.url=https://cas.example.com:8443
#CAS客户端地址,就是本应用的地址
client.url=http://localhost:8080

分析

在applicationContext-security.xml中的security filter chain中,我们使用了5个filter,分别是:singleSignOutFilter、cas20ProxyReceivingTicketValidationFilter、authenticationFilter、httpServletRequestWrapperFilter、assertionThreadLocalFilter。

为什么不用spring-security-cas

spring-security-cas

在spring-security-cas中负责ticket validator filter使用的是org.springframework.security.cas.authentication.CasAuthenticationProvider。

private CasAuthenticationToken authenticateNow(final Authentication authentication) throws AuthenticationException {
 try {
  final Assertion assertion = this.ticketValidator.validate(authentication.getCredentials().toString(), getServiceUrl(authentication));
  ...

在构建validator的validator方法的第二个参数时

private String getServiceUrl(Authentication authentication) {
 String serviceUrl;
 if(authentication.getDetails() instanceof ServiceAuthenticationDetails) {
  serviceUrl = ((ServiceAuthenticationDetails)authentication.getDetails()).getServiceUrl();
 }else if(serviceProperties == null){
  throw new IllegalStateException("serviceProperties cannot be null unless Authentication.getDetails() implements ServiceAuthenticationDetails.");
 }else if(serviceProperties.getService() == null){
  throw new IllegalStateException("serviceProperties.getService() cannot be null unless Authentication.getDetails() implements ServiceAuthenticationDetails.");
 }else {
  serviceUrl = serviceProperties.getService();
 }
 if(logger.isDebugEnabled()) {
  logger.debug("serviceUrl = "+serviceUrl);
 }
 return serviceUrl;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • HelloSpringMVC配置版实现步骤解析

    HelloSpringMVC配置版实现步骤解析

    这篇文章主要介绍了HelloSpringMVC配置版实现步骤解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • 举例讲解Java编程中this关键字与super关键字的用法

    举例讲解Java编程中this关键字与super关键字的用法

    这篇文章主要介绍了Java编程中this关键字与super关键字的用法示例,super是this的父辈,在继承过程中两个关键字经常被用到,需要的朋友可以参考下
    2016-03-03
  • SpringBoot自定义注解及AOP的开发和使用详解

    SpringBoot自定义注解及AOP的开发和使用详解

    在公司项目中,如果需要做一些公共的功能,如日志等,最好的方式是使用自定义注解,自定义注解可以实现我们对想要添加日志的方法上添加,这篇文章基于日志功能来讲讲自定义注解应该如何开发和使用,需要的朋友可以参考下
    2023-08-08
  • Java Applet查找素数小程序代码实例

    Java Applet查找素数小程序代码实例

    这篇文章主要介绍了Java Applet查找素数小程序代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • java实现压缩字符串和java字符串过滤

    java实现压缩字符串和java字符串过滤

    这篇文章主要介绍了java实现压缩字符串和java字符串过滤,需要的朋友可以参考下
    2014-04-04
  • Java实现按权重随机数

    Java实现按权重随机数

    这篇文章主要介绍了Java实现按权重随机数,本文给出了提出问题、分析问题、解决问题三个步骤,需要的朋友可以参考下
    2015-04-04
  • 一文梳理Java超大型文件读取的18种方法和性能

    一文梳理Java超大型文件读取的18种方法和性能

    这篇文章主要为大家详细介绍了Java中超大型文件读取的18种方法和性能对比,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下
    2025-02-02
  • SpringCloud实现基于RabbitMQ消息队列的详细步骤

    SpringCloud实现基于RabbitMQ消息队列的详细步骤

    在Spring Cloud框架中,我们可以利用RabbitMQ实现强大而可靠的消息队列系统,本篇将详细介绍如何在Spring Cloud项目中集成RabbitMQ,并创建一个简单的消息队列,感兴趣的朋友一起看看吧
    2024-03-03
  • Java多线程的同步优化的6种方案

    Java多线程的同步优化的6种方案

    大家使用多线程无非是为了提高性能,在Java中,有多线程并发时,我们可以使用多线程同步的方式来解决内存一致性的问题。本文就详细的介绍了Java多线程同步优化,感兴趣的可以了解一下
    2021-05-05
  • Java中的迭代和递归详解

    Java中的迭代和递归详解

    这篇文章主要给大家介绍了关于Java中的迭代和递归,文章显示分别介绍了Java中的迭代和递归,而后又介绍了迭代和递归的区别以及数形递归的相关内容,文中介绍的很详细,相信会对大家学习具有一定的参考借鉴价值,有需要的朋友们可以参考借鉴。
    2016-11-11

最新评论