Spring Security(20)——整合Cas

本文详细介绍了如何在Spring Security应用中整合Cas以实现单点登录和登出功能。主要内容包括配置登录认证(CasAuthenticationEntryPoint、CasAuthenticationFilter、AuthenticationManager)、单点登出设置、以及如何使用代理。配置完成后,Spring Security应用可以通过Cas Server进行单点登录和登出,同时也支持作为代理端或被代理端的身份切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

       众所周知,Cas是对单点登录的一种实现。本文假设读者已经了解了Cas的原理及其使用,这些内容在本文将不会讨论。Cas有Server端和Client端,Client端通常对应着我们自己的应用,Spring Security整合Cas指的就是在Spring Security应用中整合Cas Client,已达到使用Cas Server实现单点登录和登出的效果。本文旨在描述如何在Spring Security应用中使用Cas的单点登录。

       首先需要将Spring Security对Cas支持的jar包加入到应用的类路径中。如果我们的应用使用Maven构造的,则可以在应用的pom.xml文件中加上如下依赖。

   <dependency>

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

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

      <version>${spring.security.version}</version>

   </dependency>

 

1.1     配置登录认证

       加入了spring-security-cas-xxx.jar到Spring Security应用的classpath后,我们便可以开始配置我们的Spring Security应用使用Cas进行单点登录了。

 

1.1.1配置AuthenticationEntryPoint

       首先需要做的是将应用的登录认证入口改为使用CasAuthenticationEntryPoint。所以首先我们需要配置一个CasAuthenticationEntryPoint对应的bean,然后指定需要进行登录认证时使用该AuthenticationEntryPoint。配置CasAuthenticationEntryPoint时需要指定一个ServiceProperties,该对象主要用来描述service(Cas概念)相关的属性,主要是指定在Cas Server认证成功后将要跳转的地址。

   <!-- 指定登录入口为casEntryPoint -->

   <security:http  entry-point-ref="casEntryPoint">

      ...

   </security:http>

 

   <!-- 认证的入口 -->

   <bean id="casEntryPoint"

      class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">

      <!-- Cas Server的登录地址,elim是我的计算机名 -->

      <property name="loginUrl" value="https://elim:8443/cas/login" />

      <!-- service相关的属性 -->

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

   </bean>

 

   <!-- 指定service相关信息 -->

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

      <!-- Cas Server认证成功后的跳转地址,这里要跳转到我们的Spring Security应用,之后会由CasAuthenticationFilter处理,默认处理地址为/j_spring_cas_security_check -->

      <property name="service"

         value="http://elim:8080/app/j_spring_cas_security_check" />

   </bean>

 

1.1.2配置CasAuthenticationFilter

       之后我们需要配置一个CasAuthenticationFilter,并将其放置在Filter链表中CAS_FILTER的位置,以处理Cas Server认证成功后的页面跳转,用以在Spring Security中进行认证。该Filter会将Cas Server传递过来的ticket(Cas概念)封装成一个Authentication(对应UsernamePasswordAuthenticationToken,其中ticket作为该Authentication的password),然后传递给AuthenticationManager进行认证。

   <security:http entry-point-ref="casEntryPoint">

      ...

      <security:custom-filter ref="casFilter" position="CAS_FILTER"/>

      ...

   </security:http>

 

   <bean id="casFilter"

      class="org.springframework.security.cas.web.CasAuthenticationFilter">

      <property name="authenticationManager" ref="authenticationManager" />

      <!-- 指定处理地址,不指定时默认将会是“/j_spring_cas_security_check” -->

      <property name="filterProcessesUrl" value="/j_spring_cas_security_check"/>

   </bean>

 

1.1.3配置AuthenticationManager

       CasAuthenticationFilter会将封装好的包含Cas Server传递过来的ticket的Authentication对象传递给AuthenticationManager进行认证。我们知道默认的AuthenticationManager实现类为ProviderManager,而ProviderManager中真正进行认证的是AuthenticationProvider。所以接下来我们要在AuthenticationManager中配置一个能够处理CasAuthenticationFilter传递过来的Authentication对象的AuthenticationProvider实现,CasAuthenticationProvider。CasAuthenticationProvider首先会利用TicketValidator(Cas概念)对Authentication中包含的ticket信息进行认证。认证通过后将利用持有的AuthenticationUserDetailsService根据认证通过后回传的Assertion对象中拥有的username加载用户对应的UserDetails,即主要是加载用户的相关权限信息GrantedAuthority。然后构造一个CasAuthenticationToken进行返回。之后的逻辑就是正常的Spring Security的逻辑了。

   <security:authentication-manager alias="authenticationManager">

      <security:authentication-provider ref="casAuthenticationProvider"/>

   </security:authentication-manager>

 

   <bean id="casAuthenticationProvider"

   class="org.springframework.security.cas.authentication.CasAuthenticationProvider">

      <!-- 通过username来加载UserDetails -->

      <property name="authenticationUserDetailsService">

         <beanclass="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">

            <!-- 真正加载UserDetailsUserDetailsService实现 -->

            <constructor-arg ref="userDetailsService" />

         </bean>

      </property>

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

      <!-- 配置TicketValidator在登录认证成功后验证ticket -->

      <property name="ticketValidator">

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

            <!-- Cas Server访问地址的前缀,即根路径-->

            <constructor-arg index=

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值