SpringBoot中Shiro使用Pac4j集成CAS认证
Pac4j 简介
Pac4j与Shiro,Spring Security一样都是权限框架,并且提供了OAuth - SAML - CAS - OpenID Connect - HTTP - OpenID - Google App Engine - Kerberos (SPNEGO) 的认证集成。且可以和shiro,security等权限框架集成。
 

Pac4j CAS认证流程
<ignore_js_op>
代码 关键部分

说明: pac4j-cas与shiro的集成是通过过滤器完成cas认证,提供相应的Pac4jRealm来与shiro集成。代码过多就不一一列出了,详细的请下载附件,附件中代码屏蔽了公司相关代码。自身项目需要保持CAS与非CAS并存所以把CAS登录固定到指定路径了。
POM
[XML] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
>[font=-apple-system, system-ui, Segoe UI, Helvetica, Arial, sans-serif][color=#24292e][b]<!--cas认证 -->[/b][/color][/font][/align][align=left][font=-apple-system, system-ui, Segoe UI, Helvetica, Arial, sans-serif][color=#24292e][b]        <dependency>[/b][/color][/font][/align][align=left][font=-apple-system, system-ui, Segoe UI, Helvetica, Arial, sans-serif][color=#24292e][b]            <groupId>org.pac4j</groupId>[/b][/color][/font][/align][align=left][font=-apple-system, system-ui, Segoe UI, Helvetica, Arial, sans-serif][color=#24292e][b]            <artifactId>pac4j-cas</artifactId>[/b][/color][/font][/align][align=left][font=-apple-system, system-ui, Segoe UI, Helvetica, Arial, sans-serif][color=#24292e][b]            <version>3.8.3</version>[/b][/color][/font][/align][align=left][font=-apple-system, system-ui, Segoe UI, Helvetica, Arial, sans-serif][color=#24292e][b]        </dependency>[/b][/color][/font][/align][align=left][font=-apple-system, system-ui, Segoe UI, Helvetica, Arial, sans-serif][color=#24292e][b]<!-- pac4j与shiro集成-->[/b][/color][/font][/align][align=left][font=-apple-system, system-ui, Segoe UI, Helvetica, Arial, sans-serif][color=#24292e][b]        <dependency>[/b][/color][/font][/align][align=left][font=-apple-system, system-ui, Segoe UI, Helvetica, Arial, sans-serif][color=#24292e][b]            <groupId>io.buji</groupId>[/b][/color][/font][/align][align=left][font=-apple-system, system-ui, Segoe UI, Helvetica, Arial, sans-serif][color=#24292e][b]            <artifactId>buji-pac4j</artifactId>[/b][/color][/font][/align][align=left][font=-apple-system, system-ui, Segoe UI, Helvetica, Arial, sans-serif][color=#24292e][b]            <version>4.1.1</version>[/b][/color][/font][/align][align=left][font=-apple-system, system-ui, Segoe UI, Helvetica, Arial, sans-serif][color=#24292e][b]        </dependency>[/b][/color][/font]/mw_shl_code][/align][align=left]JAVA配置[/align][align=left][mw_shl_code=java,true]//Pac4jConfig.java 配置中
 @Bean
 public CasConfiguration casConfig() {
  final CasConfiguration configuration = new CasConfiguration();
  //CAS server登录地址
  configuration.setLoginUrl(casServerUrl + "/login");
  configuration.setAcceptAnyProxy(true);
  configuration.setPrefixUrl(casServerUrl + "/");
  //监控CAS服务端登出,登出后销毁本地session实现双向登出
  DefaultLogoutHandler logoutHandler = new DefaultLogoutHandler();
  logoutHandler.setDestroySession(true);
  configuration.setLogoutHandler(logoutHandler);
  return configuration;
 }
//ShiroConfig.java 中
//shiro 过滤器配置中增加SecurityFilter,CallbackFilter ,LogoutFilter
 @Bean("shiroFilter")
 public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
  ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
  shiroFilterFactoryBean.setSecurityManager(securityManager);
  //获取filters
  Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();
  filters.put("authc", new MySystemFilter());
  // cas 资源认证拦截器
  SecurityFilter securityFilter = new SecurityFilter();
  securityFilter.setConfig(exPac4jConfig);
  securityFilter.setClients(clientName);
  filters.put("securityFilter", securityFilter);
  //cas 认证后回调拦截器
  CallbackFilter callbackFilter = new CallbackFilter();
  callbackFilter.setConfig(exPac4jConfig);
  filters.put("callbackFilter", callbackFilter);
  shiroFilterFactoryBean.setFilters(filters);
  // 本地登出同步登出CAS服务器
  LogoutFilter pac4jCentralLogout = new LogoutFilter();
  pac4jCentralLogout.setConfig(exPac4jConfig);
  pac4jCentralLogout.setCentralLogout(true);
  pac4jCentralLogout.setLocalLogout(true);
  filters.put("pac4jCentralLogout", pac4jCentralLogout);
  //拦截器.
  Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
  filterChainDefinitionMap.put("/logout", "logout");
  filterChainDefinitionMap.put("/pac4jCentralLogout", "pac4jCentralLogout");
  filterChainDefinitionMap.put("/cas", "securityFilter");
  filterChainDefinitionMap.put("/callback", "callbackFilter");
  filterChainDefinitionMap.put("/**", "authc");
  shiroFilterFactoryBean.setLoginUrl("/login");
  shiroFilterFactoryBean.setSuccessUrl("index");
  shiroFilterFactoryBean.setUnauthorizedUrl("/error/403");
  shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
  return shiroFilterFactoryBean;
 }
 @Bean
 public SecurityManager securityManager() {
  DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
  securityManager.setAuthenticator(exModularRealmAuthenticator());
  List<Realm> realms = new ArrayList<>();
  realms.add(exSystemRealm());
 // casRealm继承Pac4jRealm 与shiro的Realm使用方法相同
  realms.add(casRealm);
  securityManager.setRealms(realms);
  securityManager.setCacheManager(redisCacheManager());
 //增加pac4jSubjectFactory
  securityManager.setSubjectFactory(pac4jSubjectFactory);
  securityManager.setRememberMeManager(cookieRememberMeManager());
  securityManager.setSessionManager(sessionManager());
  return securityManager;
 }
问题
  • 默认配置不支持CAS登出本地项目退出
重写ShiroSessionStore见ExShiroSessionStore.java
附件:链接: https://pan.baidu.com/s/1E-6uTYpOFn2ldAxd_k0XvQ 提取码: 8nhx
更多技术资讯可关注:itheimaGZ获取

SpringBoot中Shiro使用Pac4j集成CAS认证的更多相关文章

  1. CAS学习笔记五:SpringBoot自动/手动配置方式集成CAS单点登出

    本文目标 基于SpringBoot + Maven 分别使用自动配置与手动配置过滤器方式实现CAS客户端登出及单点登出. 本文基于<CAS学习笔记三:SpringBoot自动/手动配置方式集成C ...

  2. SpringBoot中Shiro缓存使用Redis、Ehcache

    在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...

  3. SpringBoot整合Shiro+MD5+Salt+Redis实现认证和动态权限管理|前后端分离(下)----筑基后期

    写在前面 在上一篇文章<SpringBoot整合Shiro+MD5+Salt+Redis实现认证和动态权限管理(上)----筑基中期>当中,我们初步实现了SpringBoot整合Shiro ...

  4. SpringBoot整合Shiro+MD5+Salt+Redis实现认证和动态权限管理(上)----筑基中期

    写在前面 通过前几篇文章的学习,我们从大体上了解了shiro关于认证和授权方面的应用.在接下来的文章当中,我将通过一个demo,带领大家搭建一个SpringBoot整合Shiro的一个项目开发脚手架, ...

  5. (十二)springboot中shiro的使用

    一.引入maven配置 <dependency>     <groupId>org.apache.shiro</groupId>     <artifactI ...

  6. SpringBoot中整合Redis、Ehcache使用配置切换 并且整合到Shiro中

    在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...

  7. SpringBoot整合Shiro 四:认证+授权

    搭建环境见: SpringBoot整合Shiro 一:搭建环境 shiro配置类见: SpringBoot整合Shiro 二:Shiro配置类 shiro整合Mybatis见:SpringBoot整合 ...

  8. spring boot 2.0 集成 shiro 和 pac4j cas单点登录

    新开的项目,果断使用  spring boot  最新版本  2.0.3 ,免得后期升级坑太多,前期把雷先排了. 由于对 shiro 比较熟,故使用 shiro 来做权限控制.同时已经存在了 cas  ...

  9. springboot shiro和freemarker集成之权限控制完全参考手册(跳过认证,登录由三方验证,全网首发)

    本文主要考虑单点登录场景,登录由其他系统负责,业务子系统只使用shiro进行菜单和功能权限校验,登录信息通过token从redis取得,这样登录验证和授权就相互解耦了. 用户.角色.权限进行集中式管理 ...

随机推荐

  1. 可能对Flutter应用程序开发有用的代码/库/专有技术列表

    当我开始使用Flutter实施该应用程序时,我开始担心“如何最好地编写?”以及“如何使其更好地放置?”. 在这种情况下,您将需要参考GitHub上发布的代码和应用程​​序. 因此,我收集了似乎对Flu ...

  2. POJ-1015 Jury Compromise(dp|01背包)

    题目: In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting ...

  3. HTTP、MQTT、WebSocket有什么区别

    https://blog.csdn.net/linyunping/article/details/81950185 相同点:均为OSI 7层模型(应用层.表示层.会话层.传输层.网络层.数据链路层.物 ...

  4. android studio compile api implementation 区别

    compile与api 二者等同,无区别 implementation与compile或implementation与api implementation编译的依赖只作用于当前的module.即APP ...

  5. python语法基础-并发编程-进程-进程池以及回调函数

    ###############   进程池    ############## """ 进程池的概念 为什么会有进程池? 1,因为每次开启一个进程,都需要创建一个内存空间 ...

  6. vue执行期间的函数

    先放上vue官方给的函数图

  7. 关于maven的使用总结

    maven介绍 项目构建过程 eclipse只是开发工具,虽然提供了创建.编码.编译.测试.运行等功能,但并不是项目构建工具. 项目构建主要过程如下: 实际的项目构建过程要复杂繁琐的多.如果是一个独立 ...

  8. 887C. Slava and tanks#轰炸弹坦克游戏(分析)

    题目出处:http://codeforces.com/problemset/problem/877/C 题目大意:按照游戏规则,求最小炸弹使用次数 #include<iostream> u ...

  9. 【lca+输入】Attack on Alpha-Zet

    Attack on Alpha-Zet 题目描述 Space pirate Captain Krys has recently acquired a map of the artificial and ...

  10. MySQL报错解决:The MySQL server is running with the --read-only option so it cannot execute this statement

    MySQL报错:The MySQL server is running with the --skip-grant-tables option so it cannot execute this st ...