日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

CAS Server(二):基于SpringBoot搭建客户端

發(fā)布時(shí)間:2025/3/19 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CAS Server(二):基于SpringBoot搭建客户端 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 概述

本篇介紹SpringBoot項(xiàng)目接入CAS Server,先創(chuàng)建一個(gè)空的SpringBoot項(xiàng)目

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. 創(chuàng)建頁(yè)面

在templates目錄下創(chuàng)建2個(gè)頁(yè)面:

index.html 首頁(yè)

<!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 退出后跳轉(zhuǎn)的頁(yè)面

<!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. 創(chuàng)建 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. 創(chuàng)建過(guò)濾器

@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());// 設(shè)定匹配的路徑registration.addUrlPatterns("/*");Map<String,String> initParameters = new HashMap<String, String>();initParameters.put("casServerLoginUrl", serverUrlPrefix);initParameters.put("serverName", clientHostUrl);//忽略的url,"|"分隔多個(gè)url//initParameters.put("ignorePattern", "/logoutsuccess|/index");initParameters.put("ignorePattern", "/logoutsuccess");registration.setInitParameters(initParameters);// 設(shè)定加載的順序registration.setOrder(1);return registration;} }

7. 測(cè)試

打開(kāi)首頁(yè)地址:http://localhost:99/index,會(huì)跳轉(zhuǎn)到CAS Server登錄頁(yè)面。這時(shí)候注意看瀏覽器的地址:http://127.0.0.1:8080/cas/login?service=http%3A%2F%2F127.0.0.1%3A99%2Findex,自動(dòng)攜帶的service參數(shù)就是我們剛才訪問(wèn)的頁(yè)面

輸入賬號(hào)密碼,登錄后會(huì)跳轉(zhuǎn)會(huì)前面service的頁(yè)面地址


注意看前面的application.properties,lotout 鏈接我們同樣傳了service參數(shù),目的是CAS Server單點(diǎn)登出后能夠跳回我們指定的頁(yè)面。

CAS Server默認(rèn)不會(huì)開(kāi)啟登出跳轉(zhuǎn),需要修改E:\apache-tomcat-8.5.59\webapps\cas\WEB-INF\cas.properties文件,將cas.logout.followServiceRedirects屬性值改成true


成功退出后,CAS會(huì)跳回我們的頁(yè)面

到這里,簡(jiǎn)單的 SpringBoot CAS 客戶端就搭建成功了

總結(jié)

以上是生活随笔為你收集整理的CAS Server(二):基于SpringBoot搭建客户端的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。