本文目标

基于SpringBoot + Maven 分别使用自动配置与手动配置过滤器方式集成CAS客户端。

代码目录结构

代码实现

父工程 cas-integration-demo

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hellxz</groupId>
<artifactId>cas-integration-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath /> <!-- lookup parent from repository -->
</parent> <modules>
<!-- 自动配置demo A -->
<module>demo-a-auto-config</module>
<!-- 手动配置demo B -->
<module>demo-b-manual-config</module>
</modules> <properties>
<java.version>1.8</java.version>
<!-- 指定cas客户端版本 -->
<cas.client.version>3.6.3</cas.client.version>
</properties> <dependencies>
<!-- 引入web starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

自动配置工程 demo-a-auto-config

下文简称此工程为 服务A

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hellxz</groupId>
<artifactId>cas-integration-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>demo-a-auto-config</artifactId> <dependencies>
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-support-springboot</artifactId>
<version>${cas.client.version}</version>
</dependency>
</dependencies>
</project>

com.hellxz.cas.CasAutoConfigApp.java

package com.hellxz.cas;

import javax.servlet.http.HttpServletRequest;

import org.jasig.cas.client.boot.configuration.EnableCasClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@RestController
@EnableCasClient //<--自动配置只需要启用此注解
public class CasAutoConfigApp { public static void main(String[] args) {
SpringApplication.run(CasAutoConfigApp.class, args);
} @GetMapping("/test")
public String test(HttpServletRequest request) {
return "服务A测试通过";
} }

application.properties

server.port=8081

#CAS配置,更多参数见https://github.com/apereo/java-cas-client#spring-boot-autoconfiguration
#cas服务端地址
cas.server-url-prefix=http://192.168.56.104:8080/cas
#cas服务端登录地址
cas.server-login-url=http://192.168.56.104:8080/cas/login
#当前服务地址
cas.client-host-url=http://10.2.6.63:8081
#校验ticket使用的协议,可选: CAS(代表CAS2)、CAS3、SAML
cas.validation-type=CAS3

注意替换 CAS 服务端地址 及 客户端 IP

手动配置工程 demo-b-manual-config

下文简称此工程为 服务B

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hellxz</groupId>
<artifactId>cas-integration-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>demo-b-manual-config</artifactId> <dependencies>
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
<version>${cas.client.version}</version>
</dependency>
</dependencies>
</project>

com.hellxz.cas.CasManualConfigApp.java

package com.hellxz.cas;

import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@SpringBootApplication
public class CasManualConfigApp { public static void main(String[] args) {
SpringApplication.run(CasManualConfigApp.class, args);
} @GetMapping("/test")
public String test(HttpServletRequest request) {
return "服务B测试通过";
} }

com.hellxz.cas.Config.java

package com.hellxz.cas;

import java.util.HashMap;
import java.util.Map; import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.jasig.cas.client.util.HttpServletRequestWrapperFilter;
import org.jasig.cas.client.validation.Cas30ProxyReceivingTicketValidationFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order; @Configuration
public class Config {
/**
* 自定义cas服务地址
*/
@Value("${custom.cas.casServerUrlPrefix:}")
private String casServerUrlPrefix; /**
* 自定义服务标识,格式为{protocol}:{hostName}:{port}
*/
@Value("${custom.cas.serverName:}")
private String serverName; /**
* 拦截所有请求,将未携带票据与会话中无票据的请求都重定向到CAS登录地址
*/
@Bean
@Order(1)
public FilterRegistrationBean<AuthenticationFilter> casAuthenticationFilter() {
FilterRegistrationBean<AuthenticationFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new AuthenticationFilter());
registration.setName("CAS Authentication Filter");
Map<String, String> initParams = new HashMap<>();
initParams.put("casServerUrlPrefix", casServerUrlPrefix); // CAS服务端地址,会拼接为登录地址
initParams.put("serverName", serverName); // 服务地址
registration.setInitParameters(initParams);
registration.addUrlPatterns("/*");
return registration;
} /**
* 拦截所有请求,使用获取的票据向CAS服务端发起校验票据请求
*/
@Bean
@Order(2)
public FilterRegistrationBean<Cas30ProxyReceivingTicketValidationFilter> cas30TicketValidationFilter() {
FilterRegistrationBean<Cas30ProxyReceivingTicketValidationFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new Cas30ProxyReceivingTicketValidationFilter());
registration.setName("CAS30 Ticket Validation Filter");
Map<String, String> initParams = new HashMap<>();
initParams.put("casServerUrlPrefix", casServerUrlPrefix); // CAS服务端地址,会拼接为服务校验地址
initParams.put("serverName", serverName);
registration.setInitParameters(initParams);
registration.addUrlPatterns("/*");
return registration;
} /**
* 包装HttpServletRequest,使CAS登录成功的用户名等信息存入请求中<br>
* <br>
* 登录成功后以下两个方法将不再返回null: <br>
*
* <pre>
* HttpServletRequest#getUserPrincipal()
* HttpServletRequest#getRemoteUser()
* </pre>
*/
@Bean
@Order(3)
public FilterRegistrationBean<HttpServletRequestWrapperFilter> httpServletRequestWrapperFilter() {
FilterRegistrationBean<HttpServletRequestWrapperFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new HttpServletRequestWrapperFilter());
registration.setName("HttpServletRequest Wrapper Filter");
registration.addUrlPatterns("/*");
return registration;
} }

application.properties

server.port=8082
#cas服务端地址
custom.cas.casServerUrlPrefix=http://192.168.56.104:8080/cas
#当前服务地址
custom.cas.serverName=http://10.2.6.63:8082

注意替换 CAS 服务端地址 及 客户端 IP

验证单点登录流程

1、分别启动 demo-a-auto-config 与 demo-b-manual-config

2、打开浏览器输入 demo部署的IP:8081/test,访问服务A,访问后立即跳转CAS登录

3、输入默认用户名/密码 casuser/Mellon,登录成功返回服务A的字样,让我们看看登录请求后发生了什么:

登录成功后,响应头Set-Cookie回写了名为 TGC 的Cookie,并且还有 Location 重定向,URL上传了 ST (服务票据)

重定向到服务A,服务A验证ST(忽略此流程,它不在浏览器端体现),校验通过回写Cookie,重定向回服务A的 /test接口

接着访问服务A接口,完成响应,此时服务A单点登录已经成功了。

4、清网络记录,再访问服务B的接口 服务B地址:8081/test,可以看到立即跳转成功了,查看下请求,主要是 /cas/login 请求

后边校验ST重定向与访问服务A时过程一致,不再赘述。CAS单点登录验证通过。

代码解释

自动配置demo(服务A)

SpringBoot集成CAS客户端比较简单,此处用的是官方方式。首先引入自动配置依赖,并启用 @EnableCasClient 注解

        <dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-support-springboot</artifactId>
<version>${cas.client.version}</version>
</dependency>

此依赖引用了 cas-client-core.jar (核心包)与 cas-client-support-saml.jar (saml验签协议支持),另外 cas-client-support-springboot.jar 中还有配置类

其中:

1、EnableCasClient 注解类引入了配置类 CasClientConfiguration

2、CasClientConfiguration 配置类中使用 CasClientConfigurationProperties 读取配置文件,根据配置内容注册 Spring Bean

3、CasClientConfiguration 配置类中做了以下几件事:

  • casAuthenticationFilter() 创建了 认证过滤器
  • casValidationFilter() 创建了 验证票据过滤器
  • casHttpServletRequestWrapperFilter() 创建了请求对象的包装类
  • casAssertionThreadLocalFilter() 创建了将 Assertion 放到 ThreadLocal 的过滤器,对于获取不到HttpRequest 请求对象的情况这很有用
  • casSingleSignOutFilter() 创建了单点登出的过滤器
  • casSingleSignOutListener() 创建单点登出的Listener,用于监听登出事件,清理内存中单点登录会话缓存
  • SpringSecurityAssertionAutoConfiguration 兼容Spring Security的配置类

其中对于单点登录最重要的是 casAuthenticationFilter()、casValidationFilter() 这两个方法,另外以上几个方法创建的对象类都在 cas-client-core.jar ,也就是说可以只引这一个包,然后自行配置。

有了自动配置我们再来看手动配置就比较简单了。

手动配置demo (服务B)

刚才也说了 cas-client-core.jar 是CAS客户端的核心依赖,必须引用!

        <dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
<version>${cas.client.version}</version>
</dependency>

然后就是编写配置类 com.hellxz.cas.Config.java,使用 @Value 读取配置文件,生成最关键的2个过滤器 AuthenticationFilter 与 Cas30ProxyReceivingTicketValidationFilter,为便于获取用户名配置了 HttpServletRequestWrapperFilter 过滤器,相当于自动配置demo的配置类简化版,如此而已。

至于为什么用 Cas30ProxyReceivingTicketValidationFilter,AbstractTicketValidationFilter 是个抽象类,具体用哪个实现根据协议需要选择就可以了,更多配置参考官方客户端源码仓库 https://github.com/apereo/java-cas-client

总结

根据本次 集成 CAS 单点登录代码的编写与求证,验证了上篇文章 CAS 单点登录的流程,相信大家对CAS更加了解了。

由于行文紧促,可能出现错误与遗漏,还请大家多多包涵,评论指出,感激!

本文同步于本人博客园(hellxz.cnblogs.com) 与 CSDN(https://blog.csdn.net/u012586326),禁止转载。

CAS学习笔记三:SpringBoot自动配置与手动配置过滤器方式集成CAS客户端的更多相关文章

  1. 【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)

    http://blog.csdn.net/a67474506/article/details/61640548 Dubbo是什么东西我这里就不详细介绍了,自己可以去谷歌 SpringBoot整合Dub ...

  2. SpringBoot学习笔记(3)----SpringBoot的profile多环境配置

    在实际的应用开发中,可能会需要不同配置文件来支撑程序的运行,如开发,测试,预生产,生产环境等,程序在不同的环境下可能需要不同的配置,如不同数据源等,如果每次在不同的环境下都要去修改配置文件就会闲得不合 ...

  3. MyBatis学习笔记(三)——优化MyBatis配置文件中的配置

    转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4264301.html 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的 ...

  4. CAS学习笔记五:SpringBoot自动/手动配置方式集成CAS单点登出

    本文目标 基于SpringBoot + Maven 分别使用自动配置与手动配置过滤器方式实现CAS客户端登出及单点登出. 本文基于<CAS学习笔记三:SpringBoot自动/手动配置方式集成C ...

  5. 源码学习系列之SpringBoot自动配置(篇一)

    源码学习系列之SpringBoot自动配置源码学习(篇一) ok,本博客尝试跟一下Springboot的自动配置源码,做一下笔记记录,自动配置是Springboot的一个很关键的特性,也容易被忽略的属 ...

  6. 源码学习系列之SpringBoot自动配置(篇二)

    源码学习系列之SpringBoot自动配置(篇二)之HttpEncodingAutoConfiguration 源码分析 继上一篇博客源码学习系列之SpringBoot自动配置(篇一)之后,本博客继续 ...

  7. ZooKeeper学习笔记三:使用ZooKeeper实现一个简单的配置中心

    作者:Grey 原文地址:ZooKeeper学习笔记三:使用ZooKeeper实现一个简单的配置中心 前置知识 完成ZooKeeper集群搭建以及熟悉ZooKeeperAPI基本使用 需求 很多程序往 ...

  8. VSTO学习笔记(三) 开发Office 2010 64位COM加载项

    原文:VSTO学习笔记(三) 开发Office 2010 64位COM加载项 一.加载项简介 Office提供了多种用于扩展Office应用程序功能的模式,常见的有: 1.Office 自动化程序(A ...

  9. [Firefly引擎][学习笔记三][已完结]所需模块封装

    原地址:http://www.9miao.com/question-15-54671.html 学习笔记一传送门学习笔记二传送门 学习笔记三导读:        笔记三主要就是各个模块的封装了,这里贴 ...

随机推荐

  1. 【Java 与数据库】How to Timeout JDBC Queries

    How to Timeout JDBC Queries JDBC queries by default do not have any timeout, which means that a quer ...

  2. 团队协作项目——SVN的使用

    参考文献:https://www.cnblogs.com/rwh871212/p/6955489.html 老师接了一个新项目,需要团队共同完成开发任务,因此需要SVN.SVN是C/S架构: 1.服务 ...

  3. C#.NET编程小考30题错题纠错

    1)以下关于序列化和反序列化的描述错误的是( C). a) 序列化是将对象的状态存储到特定存储介质中的过程 b) 二进制格式化器的Serialize()和Deserialize()方法可以分别用来实现 ...

  4. Mysql配置文件 binlog和慢日志

    目录 binlog binlog_format log_slave_updates log-bin|log-bin-index expire_logs_days relay-log|relay-log ...

  5. java多线程6:ReentrantLock

    下面看下JUC包下的一大并发神器ReentrantLock,是一个可重入的互斥锁,具有比synchronized更为强大的功能. ReentrantLock基本用法 先来看一下ReentrantLoc ...

  6. [BUUCTF]PWN——[V&N2020 公开赛]simpleHeap

    [V&N2020 公开赛]simpleHeap 附件 步骤: 例行检查,64位,保护全开 根据题目可知是一道堆,直接用64位ida打开 我修改了这些函数的名称,这样方便看程序 add,我们可以 ...

  7. [BUUCTF]PWN——xdctf2015_pwn200

    xdctf2015_pwn200 附件 步骤 例行检查,32位程序,开启了nx保护 本地试运行一下程序,看看大概的情况 32位ida载入,习惯性的检索程序里的字符串,没有发现什么铭感的地方,直接看ma ...

  8. YonBuilder低代码开发实践:4行代码实现跨实体列表数据同步

    提到增.删.改.查等数据维护,后端开发者们再熟悉不过了.传统的数据维护通过操作数据库的方式实现,步骤比较繁琐,需要通过Java代码实现数据库链接,然后编写SQL语句.编写实体,将想要的数据存到相应的数 ...

  9. 在eclipse打开jsp文件变成文本的解决:

    在eclipse打开jsp文件变成文本的解决: ------原因:可能是不小心删除某些组件等等一些操作 1,考虑一下是否还有插件jsp 编辑器组件 选择内部编辑器[在下面选择 JSP Editor]- ...

  10. LuoguB2035 判断数正负 题解

    Content 给定一个数 \(n\),判断这个数的正负. 数据范围:\(-10^9\leqslant n\leqslant 10^9\). Solution 一个基础的分支结构题. if-else ...