1. 概述

本篇介绍SpringBoot项目接入CAS Server,先创建一个空的SpringBoot项目

2. pom.xml

    <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.5.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>net.unicon.cas</groupId><artifactId>cas-client-autoconfig-support</artifactId><version>1.4.0-GA</version></dependency></dependencies>

3. 创建页面

在templates目录下创建2个页面:

index.html 首页

<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<html lang="en">
<head><meta charset="UTF-8"><title>cas-client</title>
</head>
<body>
<h2>Hello:</h2><h2 th:text="${name}"></h2>
<a href="/logout">logout</a>
</body>
</html>

logoutsuccess.html 退出后跳转的页面

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Logout success! </title>
</head>
<body>
<h1>Logout success!</h1>
<a href="/index">back to index</a>
</body>
</html>

4. 创建 Controller

@Controller
public class CASController {@Value("${casClientLogoutUrl}")private String clientLogoutUrl;@RequestMapping("index")public String index(ModelMap map, HttpServletRequest request) {//获取登录的用户名map.addAttribute("name", request.getUserPrincipal());return "index";}@RequestMapping("logout")public String logout(HttpSession session) {session.invalidate();return "redirect:" + clientLogoutUrl;}@RequestMapping("logoutsuccess")public String logoutsuccess(HttpSession session) {return "logoutsuccess";}
}

5. application.properties

server.port=99cas.server-url-prefix=http://127.0.0.1:8080/cas
cas.server-login-url=http://127.0.0.1:8080/cas/login
cas.client-host-url=http://127.0.0.1:99
cas.use-session=true
cas.validation-type=cas# 自定义的退出url
casClientLogoutUrl=http://127.0.0.1:8080/cas/logout?service=http://127.0.0.1:99/logoutsuccess

6. 创建过滤器

@Configuration
public class CASAutoConfig {@Value("${cas.server-url-prefix}")private String serverUrlPrefix;@Value("${cas.server-login-url}")private String serverLoginUrl;@Value("${cas.client-host-url}")private String clientHostUrl;@Beanpublic FilterRegistrationBean filterAuthenticationRegistration(){FilterRegistrationBean registration = new FilterRegistrationBean();registration.setFilter(new AuthenticationFilter());// 设定匹配的路径registration.addUrlPatterns("/*");Map<String,String> initParameters = new HashMap<String, String>();initParameters.put("casServerLoginUrl", serverUrlPrefix);initParameters.put("serverName", clientHostUrl);//忽略的url,"|"分隔多个url//initParameters.put("ignorePattern", "/logoutsuccess|/index");initParameters.put("ignorePattern", "/logoutsuccess");registration.setInitParameters(initParameters);// 设定加载的顺序registration.setOrder(1);return registration;}
}

7. 测试

打开首页地址:http://localhost:99/index,会跳转到CAS Server登录页面。这时候注意看浏览器的地址:http://127.0.0.1:8080/cas/login?service=http%3A%2F%2F127.0.0.1%3A99%2Findex,自动携带的service参数就是我们刚才访问的页面

输入账号密码,登录后会跳转会前面service的页面地址


注意看前面的application.properties,lotout 链接我们同样传了service参数,目的是CAS Server单点登出后能够跳回我们指定的页面。

CAS Server默认不会开启登出跳转,需要修改E:\apache-tomcat-8.5.59\webapps\cas\WEB-INF\cas.properties文件,将cas.logout.followServiceRedirects属性值改成true


成功退出后,CAS会跳回我们的页面

到这里,简单的 SpringBoot CAS 客户端就搭建成功了

CAS Server(二):基于SpringBoot搭建客户端相关推荐

  1. 基于SpringBoot搭建应用开发框架(一) —— 基础架构

    基于SpringBoot搭建应用开发框架(一) -- 基础架构 目录 Spring的简史 零.开发技术简介 一.创建项目 1.创建工程 2.创建Starter 3.启动项目 4.Spring Boot ...

  2. 基于springboot搭建的个人博客系统

    源码下载地址:blog blog是基于springboot搭建的个人博客,响应式 前端技术:html.css.js.jq.bootstrap 后台技术:springboot.thymeleaf.myb ...

  3. [转]2020年排名前20的基于SpringBoot搭建的开源项目,帮你快速进行项目搭建!

    △Hollis, 一个对Coding有着独特追求的人△ 这是Hollis的第 287 篇原创分享 作者 l Hollis 来源 l Hollis(ID:hollischuang) SpringBoot ...

  4. 2020年排名前20的基于SpringBoot搭建的开源项目,帮你快速进行项目搭建!

    △Hollis, 一个对Coding有着独特追求的人△ 这是Hollis的第 287 篇原创分享 作者 l Hollis 来源 l Hollis(ID:hollischuang) SpringBoot ...

  5. 基于Springboot搭建个人博客 (学习笔记)

    个人博客搭建 技术选型 主要框架 安装 RabbitMq,Elasticsearch 安装RabbitMq 安装环境 下载安装包 安装配置 erlang 安装配置 rabbitMQ 验证 安装Elas ...

  6. CAS Server 5.3 demo搭建

    1 项目下载地址: 下载5.3版本的 cas-overlay ,用idea打开 .然后打开idea 自带的命令行工具Terminal .window系统,初次运行命令可以是 build.cmd hel ...

  7. CAS(三)基于SpringMVC搭建CAS-client,SpringMVC搭建CAS客户端

    环境要求 JDK 8+ CAS 5.2 tomcat 8+ 步骤 一.搭建CAS服务器  --> CAS(一)搭建CAS - server服务器 二.配置hosts,加入如下配置 127.0.0 ...

  8. 【毕设精选】基于SpringBoot和Vue的相关系统

    SpringBoot+Vue相关系统 前言 springboot330付费问答系统的设计与实现含文档附源码 springboot325基于Java的企业OA管理系统的设计与实现含文档附源码 sprin ...

  9. SpringBoot 搭建的个人博客

    介绍 blog是基于SpringBoot 搭建的个人博客,响应式 前端技术:html.css.js.jq.bootstrap 后台技术:springboot.thymeleaf.mybatis.mys ...

最新文章

  1. Science:固氮(The nitrogen fix)
  2. fileoutputstream路径 android,Android编程中FileOutputStream与openFileOutput()的区别分析
  3. 计算机 专业英语 考试,《计算机专业英语》期末考试试卷
  4. cannot open file mfc42u.lib的问题解决
  5. 接收超时死信的死信交换机
  6. 关于 SAP Spartacus SSR 服务器返回的响应是否应该被缓存的问题
  7. mac显示无法连接adobe服务器,Mac安装Adobe软件,如遇Error提示解决方法
  8. debug和release的区别
  9. 【Keras】从两个实际任务掌握图像分类
  10. matlab循环遍历数组_MatLab简易教程 #8 循环
  11. 设置table的td宽度,不随文字变宽
  12. nginx connect() to (13: Permission denied) while connecting
  13. SpringBoot2 结合BeetlSQL开发
  14. Ubuntu 20.04桌面美化
  15. 蓝牙温度检测系统(基于BT08-B蓝牙模块)
  16. 二进制转八进制公式计算机,二进制转八进制算法(二进制与八进制的互相转换方法)...
  17. 如何查看Mac系统的位数
  18. spark GBT算法
  19. python爬取12306火车票信息_从零开始写Python爬虫 --- 爬虫应用: 12306火车票信息查询...
  20. 总结Python设置Excel单元格样式的一切,比官方文档还详细。

热门文章

  1. SAP Backgroud job
  2. 蓝图设计对SAP项目实施的重要性
  3. NUMERIC_CHECK函数解析
  4. 全国各省“光棍”排名,数据揭秘哪里脱单最难?
  5. 政府、学校、企业都需要的轻量级数据填报方案
  6. 澳优、伊利、君乐宝、贝因美等入局,羊奶能否迎来“牛市”?
  7. Python常用模块之hashlib模块
  8. 3种Python字典合并方法
  9. Python教程:文件路径/目录获取教程
  10. Python教程:多态与多态性