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=99
cas.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;
@Bean
public 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 客户端就搭建成功了