一、概述
CAS即Central Authentication Service,是耶魯大學發起的開源單點登錄解決方案。CAS在很多大學和公司被廣泛應用。
Spring Boot CAS 單點登錄結合了Spring Boot和CAS,可以輕鬆實現Web應用的單點登錄。
二、CAS客戶端的配置
在pom.xml文件中添加CAS客戶端的依賴。
<dependency> <groupId>org.jasig.cas.client</groupId> <artifactId>cas-client-core</artifactId> <version>3.5.1</version> </dependency>
在application.properties文件中添加CAS客戶端的配置。
# CAS Client cas.server.url=https://cas-server.com:8443/cas cas.client.host=localhost cas.client.loginUrl=https://localhost:8443/login cas.client.validateUrl=https://localhost:8443/serviceValidate cas.client.logoutUrl=https://localhost:8443/logout cas.client.protocol=https cas.client.singleSignOutFilter.enabled=true cas.client.useSession=true
其中,cas.server.url為CAS伺服器的URL地址,cas.client.host為本機的IP地址,cas.client.loginUrl為本應用的登錄URL地址,cas.client.validateUrl為本應用驗證票據的地址,cas.client.logoutUrl為本應用的登出URL地址。
三、Web應用的配置
在pom.xml文件中添加Web應用的依賴。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
在application.properties文件中添加Web應用的配置。
# Spring Boot server.port=8443 server.ssl.enabled=true server.ssl.key-store=file:/path/to/keystore server.ssl.key-store-password=changeit server.ssl.key-password=changeit server.servlet.context-path=/cas-client
其中,server.port為應用的埠號,server.ssl.enabled為是否啟用SSL,server.ssl.key-store為SSL的密鑰庫地址,server.ssl.key-store-password為SSL的密鑰庫密碼,server.ssl.key-password為SSL的私鑰密碼,server.servlet.context-path為應用的上下文路徑。
四、CAS登出
在application.properties文件中添加CAS登出的配置。
# Spring Security CAS security.logout.success.url=https://cas-server.com:8443/cas/logout?service=https://localhost:8443 security.cas.logout.url=https://cas-server.com:8443/cas/logout?service=https://localhost:8443
其中,security.logout.success.url為應用登出後跳轉的URL地址,security.cas.logout.url為CAS登出的URL地址。
五、小結
Spring Boot CAS 單點登錄提供了一種簡單而強大的解決方案,可以方便地實現Web應用的單點登錄和登出。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/156895.html