飞道的博客

springboot + shiro + cas 实现 登录 + 授权 + sso单点登录 (六)

667人阅读  评论(0)

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文件


  
  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion> 4.0.0</modelVersion>
  5. <parent>
  6. <groupId>com.gane.maple</groupId>
  7. <artifactId>sso_login</artifactId>
  8. <version> 1.0</version>
  9. </parent>
  10. <groupId>com.gane.maple.order</groupId>
  11. <artifactId>order</artifactId>
  12. <version> 1.0.0</version>
  13. <name>order</name>
  14. <description>order system</description>
  15. <dependencies>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-web</artifactId>
  19. </dependency>
  20. <!-- thymeleaf -->
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  24. </dependency>
  25. <!--cas的客户端 -->
  26. <dependency>
  27. <groupId>net.unicon.cas</groupId>
  28. <artifactId>cas-client-autoconfig-support</artifactId>
  29. <version> 1.4.0-GA</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.jasig.cas.client</groupId>
  33. <artifactId>cas-client-core</artifactId>
  34. <version> 3.2.2</version>
  35. </dependency>
  36. </dependencies>
  37. </project>

三、application.properties


  
  1. server.port= 8083
  2. cas.server-url-prefix=https: //sso.maple.com:8443/cas
  3. cas.server-login-url=https: //sso.maple.com:8443/cas/login
  4. cas.client-host-url=http: //localhost:8083
  5. cas.validation-type=cas
  6. casClientLogoutUrl=https: //sso.maple.com:8443/cas/logout?service=http://localhost:8083

四、application里加上注解

@EnableCasClient//启用cas client

五、CASController


  
  1. package com.gane.maple.order.controller;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.ui.ModelMap;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. /**
  7. * @Description TODO
  8. * @Date 2020/4/18 17:40
  9. * @Created by 王弘博
  10. */
  11. @Controller
  12. public class CASController {
  13. @Value( "${casClientLogoutUrl}")
  14. private String clientLogoutUrl;
  15. @RequestMapping( "index")
  16. public String index(ModelMap map) {
  17. map.addAttribute( "name", "clien B");
  18. return "index";
  19. }
  20. @RequestMapping( "hello")
  21. public String hello() {
  22. return "hello";
  23. }
  24. @RequestMapping( "logout")
  25. public String logout() {
  26. return "redirect:" + clientLogoutUrl;
  27. }
  28. }

六、CASAutoConfig


  
  1. package com.gane.maple.order.config;
  2. import org.jasig.cas.client.authentication.AuthenticationFilter;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. /**
  10. * @Description TODO
  11. * @Date 2020/4/18 17:15
  12. * @Created by 王弘博
  13. */
  14. @Configuration
  15. public class CASAutoConfig {
  16. @Value( "${cas.server-url-prefix}")
  17. private String serverUrlPrefix;
  18. @Value( "${cas.server-login-url}")
  19. private String serverLoginUrl;
  20. @Value( "${cas.client-host-url}")
  21. private String clientHostUrl;
  22. /**
  23. * 授权过滤器
  24. * @return
  25. */
  26. @Bean
  27. public FilterRegistrationBean filterAuthenticationRegistration() {
  28. FilterRegistrationBean registration = new FilterRegistrationBean();
  29. registration.setFilter( new AuthenticationFilter());
  30. // 设定匹配的路径
  31. registration.addUrlPatterns( "/*");
  32. Map<String,String> initParameters = new HashMap<String, String>();
  33. initParameters.put( "casServerLoginUrl", serverUrlPrefix);
  34. initParameters.put( "serverName", clientHostUrl);
  35. //忽略的url,"|"分隔多个url
  36. initParameters.put( "ignorePattern", "/logout/success|/index");
  37. registration.setInitParameters(initParameters);
  38. // 设定加载的顺序
  39. registration.setOrder( 1);
  40. return registration;
  41. }
  42. }

测试:

启动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
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场