springboot + shiro + cas 实现 登录 + 授权 + sso单点登录 (一)
springboot + shiro + cas 实现 登录 + 授权 + sso单点登录 (二)
springboot + shiro + cas 实现 登录 + 授权 + sso单点登录 (三)
springboot + shiro + cas 实现 登录 + 授权 + sso单点登录 (四)
springboot + shiro + cas 实现 登录 + 授权 + sso单点登录 (五)
之前的几篇文章里,我们已经完成了 cas-client(member系统)与 原始的 cas-server 进行 SSO单点登录的测试,也完成了 cas-server 从原始到集成我们数据库的改造,现在我们就来做一下 member 与 改造之后的 cas-server 的SSO测试。
一、允许client以 Http 的格式过来
修改target/war/work/org.jasig.cas/cas-server-webapp/WEB-INF/classes/services/HTTPSandIMAPS-10000001.json,添加http

二、打war包,扔tomcat,启动 cas-server
三、启动member系统,输入 http://localhost:8081/index,跳到 cas-server 的登录页面

1、输入我们DB里的账号密码,我的是 maple/123456,登录

2、输入http://localhost:8081/logout登出

至此,基本工作做完
-------------------------------------------------优雅的分割线------------------------------------------------------------------------------------------------------
但是我们这里只有一个cas-client,就是member,实际的项目中,都会有一个统一认证中心cas-server,和多个子系统,子系统之间可以相互单点,那我们这里再多加一个子系统 order。
一、工程结构:

二、pom文件
-
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
<modelVersion>
4.0.0</modelVersion>
-
-
<parent>
-
<groupId>com.gane.maple</groupId>
-
<artifactId>sso_login</artifactId>
-
<version>
1.0</version>
-
</parent>
-
-
<groupId>com.gane.maple.order</groupId>
-
<artifactId>order</artifactId>
-
<version>
1.0.0</version>
-
<name>order</name>
-
<description>order system</description>
-
-
<dependencies>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-web</artifactId>
-
</dependency>
-
-
<!-- thymeleaf -->
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-thymeleaf</artifactId>
-
</dependency>
-
-
<!--cas的客户端 -->
-
<dependency>
-
<groupId>net.unicon.cas</groupId>
-
<artifactId>cas-client-autoconfig-support</artifactId>
-
<version>
1.4.0-GA</version>
-
</dependency>
-
<dependency>
-
<groupId>org.jasig.cas.client</groupId>
-
<artifactId>cas-client-core</artifactId>
-
<version>
3.2.2</version>
-
</dependency>
-
</dependencies>
-
-
</project>
三、application.properties
-
server.port=
8083
-
-
cas.server-url-prefix=https:
//sso.maple.com:8443/cas
-
cas.server-login-url=https:
//sso.maple.com:8443/cas/login
-
cas.client-host-url=http:
//localhost:8083
-
cas.validation-type=cas
-
casClientLogoutUrl=https:
//sso.maple.com:8443/cas/logout?service=http://localhost:8083
四、application里加上注解
@EnableCasClient//启用cas client
五、CASController
-
package com.gane.maple.order.controller;
-
-
import org.springframework.beans.factory.annotation.Value;
-
import org.springframework.stereotype.Controller;
-
import org.springframework.ui.ModelMap;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
-
/**
-
* @Description TODO
-
* @Date 2020/4/18 17:40
-
* @Created by 王弘博
-
*/
-
@Controller
-
public
class CASController {
-
-
@Value(
"${casClientLogoutUrl}")
-
private String clientLogoutUrl;
-
-
@RequestMapping(
"index")
-
public String index(ModelMap map) {
-
map.addAttribute(
"name",
"clien B");
-
return
"index";
-
}
-
-
@RequestMapping(
"hello")
-
public String hello() {
-
return
"hello";
-
}
-
-
@RequestMapping(
"logout")
-
public String logout() {
-
return
"redirect:" + clientLogoutUrl;
-
}
-
-
}
六、CASAutoConfig
-
package com.gane.maple.order.config;
-
-
import org.jasig.cas.client.authentication.AuthenticationFilter;
-
import org.springframework.beans.factory.annotation.Value;
-
import org.springframework.boot.web.servlet.FilterRegistrationBean;
-
import org.springframework.context.annotation.Bean;
-
import org.springframework.context.annotation.Configuration;
-
-
import java.util.HashMap;
-
import java.util.Map;
-
-
/**
-
* @Description TODO
-
* @Date 2020/4/18 17:15
-
* @Created by 王弘博
-
*/
-
@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;
-
}
-
}
测试:
启动cas-server
启动cas-client(member)
启动cas-client(order)
1、先访问:http://localhost:8081/index member的首页,由于没有登录过,需要去统一认证中心里登录

登录完成,成功访问 member 系统的 index页面
2、输入:http://localhost:8083/index ,访问 order 系统的index页面,发现不用登录也可以访问

3、点击order系统的登出logout接口


可以看到调用统一认证中心的登出接口,会把所有的子系统给全部登出
4、再次访问 member 系统的 http://localhost:8081/index 首页,发现需要再次登录了

至此,我们的SSO单点登录模块,全部结束。
springboot + shiro + cas 实现 登录 + 授权 + sso单点登录 (一)
springboot + shiro + cas 实现 登录 + 授权 + sso单点登录 (二)
springboot + shiro + cas 实现 登录 + 授权 + sso单点登录 (三)
springboot + shiro + cas 实现 登录 + 授权 + sso单点登录 (四)
springboot + shiro + cas 实现 登录 + 授权 + sso单点登录 (五)
转载:https://blog.csdn.net/qq_33101675/article/details/105601169