CAS(四)基於Springboot搭建CAS-client,Springboot搭建CAS客戶端
阿新 • • 發佈:2018-11-22
環境要求
- JDK 8+
- CAS 5.2
- tomcat 8+
步驟
一、搭建CAS伺服器 --> CAS(一)搭建CAS - server伺服器
二、配置hosts,加入如下配置
127.0.0.1 cas.server.com
127.0.0.1 cas.client1.com
三、搭建Springboot專案
專案名為cas-clientB,專案結構如下:
- 包含三個頁面:主頁--index.jsp(無需登入),hello頁面--hello.jsp(需要登入),退出成功提示頁--logoutsuccess.jsp(無需登入)
- pom.xml檔案配置如下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.oumuv</groupId> <artifactId>cas-clientB</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>cas-clientB</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>net.unicon.cas</groupId> <artifactId>cas-client-autoconfig-support</artifactId> <version>1.4.0-GA</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project>
- application.properties配置:
cas.server-url-prefix=http://cas.server.com:8443/cas cas.server-login-url=http://cas.server.com:8443/cas/login cas.client-host-url=http://cas.client1.com:9002 cas.use-session=true cas.validation-type=cas server.port=9002 #自定義的退出url,退出成功後跳轉到 http://cas.client1.com:9002/logout/success casClientLogoutUrl=http://cas.server.com:8443/cas/logout?service=http://cas.client1.com:9002/logout/success
其中http://cas.server.com:8443是服務端的地址,http://cas.client1.com:9002是客戶端地址
-
CASAutoConfig配置類,配置需要忽略授權的url:
@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;
/**
* 授權過濾器
* @return
*/
@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", "/logout/success|/index");
registration.setInitParameters(initParameters);
// 設定載入的順序
registration.setOrder(1);
return registration;
}
}
- CASController.java有兩個業務如下:
@Controller
public class CASController {
@RequestMapping("index")
public String index(ModelMap map) {
map.addAttribute("name", "clien B");
return "index";
}
@RequestMapping("hello")
public String hello() {
return "hello";
}
}
四、啟動專案、測試
依次啟動CAS-server服務端、CAS-clientB客戶端
訪問http://cas.client1.com:9002/index進入CAS-clientB主頁:
點選client B(http://cas.client1.com:9002/hello),會跳轉到cas登入認證頁面:
輸入賬號和密碼完成登入後跳轉到hello頁面:
到此專案搭建完成
程式碼託管在https://gitee.com/oumuv/cas-Demo
(cas-clientA基於SpringMVC,cas-clientB、cas-clientC基於springboot)
上一篇:CAS(三)基於SpringMVC搭建CAS-client,SpringMVC搭建CAS客戶端
下一篇:CAS(五)CAS客戶端單點退出實現
希望可以幫助到有需要的人