Springboot多realm集成,无ini文件,无xml配置
来源:互联网 发布:手机淘宝怎么进咸鱼 编辑:程序博客网 时间:2024/06/07 03:19
网上shiro的集成案例在多realm这块基本都是基于ini配置文件的,或者部分基于xml配置。
本人使用springboot框架,这种技术本身出发点就是标榜少配置文件或0配置(pom除外)。所以这里我把所有配置相关都集中在代码中处理。现在列出部分代码段,完整代码可以从我前面文章中获取:Spring Boot集成无状态Shiro–内容详细介绍。
两个realm
realm设置:
@Bean public SecurityManager securityManager(){ DefaultWebSecurityManager securityManager=new DefaultWebSecurityManager(); //设置realm. securityManager.setAuthenticator(modularRealmAuthenticator());// securityManager.setAuthenticator(customizedModularRealmAuthenticator()); List<Realm> realms=new ArrayList<>(); realms.add(myShiroRealm()); realms.add(myShiroRealm2()); securityManager.setRealms(realms); return securityManager; } @Bean public MyShiroRealm myShiroRealm(){ MyShiroRealm myShiroRealm = new MyShiroRealm(); //我自己实现的加密判断,这里被备注起来,仅供参考// myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher()); return myShiroRealm; } @Bean public MyShiroRealm2 myShiroRealm2(){ MyShiroRealm2 myShiroRealm = new MyShiroRealm2(); return myShiroRealm; } /** * 系统自带的Realm管理,主要针对多realm * */ @Bean public ModularRealmAuthenticator modularRealmAuthenticator(){ ModularRealmAuthenticator modularRealmAuthenticator=new ModularRealmAuthenticator(); modularRealmAuthenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy()); return modularRealmAuthenticator; }
modularRealmAuthenticator是shiro提供的realm管理器,在这里可以设置realm的生效。
通过setAuthenticationStrategy来设置多realm的使用规则。我这里选用的是AtLeastOneSuccessfulStrategy,至少有一个生效。大家也可以选用AllSuccessfulStrategy()(所有realm都验证通过才能成功登陆)和FirstSuccessfulStrategy()(只要有一个生效就不会去其它realm验证)。
如果想自己进一步控制多realm,可以自己实现ModularRealmAuthenticator ,前面我备注起来的securityManager.setAuthenticator(customizedModularRealmAuthenticator());
就是我自己重写的,具体可以从我前面的那篇文章中看到。
有一点必须要特别注意:
securityManager.setAuthenticator(modularRealmAuthenticator());// securityManager.setAuthenticator(customizedModularRealmAuthenticator()); List<Realm> realms=new ArrayList<>(); realms.add(myShiroRealm()); realms.add(myShiroRealm2()); securityManager.setRealms(realms);
这段代码中modularRealmAuthenticator的设置必须要在realm设置之前,否则就会报找不到realm的异常。
通过ModularRealmAuthenticator 这个bean和realm bean在这里的设置就可以实现0外部配置来集成多realm,完全符合springboot开发规则。
- Springboot多realm集成,无ini文件,无xml配置
- SpringBoot整合Dubbox(无XML配置)
- SpringBoot 无XML的相关配置
- 利用INI文件实现界面无闪烁多语言切换
- xml文件无提示解决方法
- SpringBoot集成篇(一)无状态shiro
- 利用INI文件实现界面无闪烁多语言切换(Delphi)
- delphi利用ini文件实现界面无闪烁多语言转换
- SpringBoot系列(1)---无配置文件配置基础1
- SpringBoot系列(2)---无配置文件配置基础2
- 配置Struts.xml无提示信息解决
- Spring MVC 无XML配置入门示例
- 三,基于Java配置、无web.xml文件方式配置Spring MVC
- Eclipse中xml文件无错误提示
- SpringBoot 多文件配置.
- spring Boot 填坑手册<一>: 无web.xml文件时的上下文监听的配置
- SpringBoot集成MyBatis时要配置的Mapper中的xml
- springboot-actuator 401 无权限
- 回文串问题两题
- 【BTree、B-树】B树的C++实现
- JZOJsenior3470.【NOIP2013模拟联考8】最短路(path)
- device_create() class_create()
- dairy
- Springboot多realm集成,无ini文件,无xml配置
- KEIL调用GCC编译STM32
- 阿里云 安装 python 全家桶
- 解惑 spring 嵌套事务
- 2016-9第一题关于最大波动
- Mybatis逆向工程 自动生成po mapper 。。。解压即用 拿走不谢 哈哈
- RN开发模型,供参考
- 蓝桥杯 基础训练 FJ的字符串
- 手机/移动前端开发需要注意的20个要点