當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
CAS Server(二):基于SpringBoot搭建客户端
生活随笔
收集整理的這篇文章主要介紹了
CAS Server(二):基于SpringBoot搭建客户端
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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/logoutsuccess6. 創建過濾器
@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搭建客户端的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CAS Server(一):搭建服务端
- 下一篇: Elasticsearch 如何把SQL