版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/oumuv/article/details/84317757
环境要求
- 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客户端单点退出实现