【Shiro权限管理】13. SecurityManager配置realms

2023-10-08 04:50

本文主要是介绍【Shiro权限管理】13. SecurityManager配置realms,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

注:该系列所有测试均在之前创建的Shiro3的Web工程的基础上。
大家可以注意到,在没有使用认证器之前,我们是这样配置Realm的:  
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="cacheManager" ref="cacheManager"/><property name="realm" ref="shiroRealm"/>
</bean>
<bean id="shiroRealm" class="com.test.shiro.realms.ShiroRealm"></bean>

后来使用了多Realm验证时,会将多个Realm配置到认证器中,再将认证器配置给securityManager:
<!--1. 配置 SecurityManager-->     
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="cacheManager" ref="cacheManager"/><property name="authenticator" ref="authenticator"/>
</bean>
<!-- 认证器 -->
<bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator"><property name="realms"><list><ref bean="shiroRealm"/><ref bean="secordRealm"/></list></property><property name="authenticationStrategy"><bean class="org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy"></bean></property>
</bean>

那么,我们如果将authenticator中的realms删除,而重新在securityManager添加两个Realm配置,
我们的工程还能跑起来吗?可以试验一下,首先将配置改为一下配置:
<!--1. 配置 SecurityManager-->     
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="cacheManager" ref="cacheManager"/><property name="authenticator" ref="authenticator"/><property name="realms"><list><ref bean="shiroRealm"/><ref bean="secordRealm"/></list></property>
</bean>
<!-- 认证器 -->
<bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator"><property name="authenticationStrategy"><bean class="org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy"></bean></property>
</bean>
重启Shiro3项目,然后使用jack登录:

发现依然可以进行正常的校验:

那么可以证明这种写法是没有问题的,但是我们要想一下,为什么这样可以呢?

可以看到,我们是在securityManager中配置了authenticator的,所以在进行校验的时候是
通过authenticator来获取认证信息并且校验的(ModularRealmAuthenticator源码):

而使用的realms集合是authenticator类(ModularRealmAuthenticator)自己的realms属性,而
我们在上面的配置中又没有给ModularRealmAuthenticator配置realms参数,而是给securityManager
配置了realms属性,那么我们的程序是如何获取两个realm的认证信息的呢?

观察ModularRealmAuthenticator的源码我们可以看到,它除了包含名为realms的集合变量外,还有
相应的get和set方法(当然这是IOC必须的),是不是有人在某个时候给它悄悄set了realms属性呢?

我们在AuthenticationSecurityManager中找到了答案:

发现在上面的方法中,只要校验出authenticator的类型属于ModularRealmAuthenticator,则会将
SecurityManager的realms属性赋值给对应的authenticator类。
所以确实是SecurityManager将自己的realms集合属性给了authenticator,所以authenticator才有realm可以用。

以后我们将使用这种写法来配置realm,因为后面我们讲解授权的时候,需要使用到SecurityManager的realms属性。

转载请注明出处:http://blog.csdn.net/acmman/article/details/78636987

这篇关于【Shiro权限管理】13. SecurityManager配置realms的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/162803

相关文章

jdk1.8的Jenkins安装配置实践

《jdk1.8的Jenkins安装配置实践》Jenkins是一款流行的开源持续集成工具,支持自动构建、测试和部署,通过Jenkins,开发团队可以实现代码提交后自动进行构建、测试,并将构建结果分发到测... 目录Jenkins介绍Jenkins环境搭建Jenkins安装配置Jenkins插件安装Git安装配

Nginx之https证书配置实现

《Nginx之https证书配置实现》本文主要介绍了Nginx之https证书配置的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起... 目录背景介绍为什么不能部署在 IIS 或 NAT 设备上?具体实现证书获取nginx配置扩展结果验证

使用Redis实现会话管理的示例代码

《使用Redis实现会话管理的示例代码》文章介绍了如何使用Redis实现会话管理,包括会话的创建、读取、更新和删除操作,通过设置会话超时时间并重置,可以确保会话在用户持续活动期间不会过期,此外,展示了... 目录1. 会话管理的基本概念2. 使用Redis实现会话管理2.1 引入依赖2.2 会话管理基本操作

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

nginx跨域访问配置的几种方法实现

《nginx跨域访问配置的几种方法实现》本文详细介绍了Nginx跨域配置方法,包括基本配置、只允许指定域名、携带Cookie的跨域、动态设置允许的Origin、支持不同路径的跨域控制、静态资源跨域以及... 目录一、基本跨域配置二、只允许指定域名跨域三、完整示例四、配置后重载 nginx五、注意事项六、支持

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Spring Boot Interceptor的原理、配置、顺序控制及与Filter的关键区别对比分析

《SpringBootInterceptor的原理、配置、顺序控制及与Filter的关键区别对比分析》本文主要介绍了SpringBoot中的拦截器(Interceptor)及其与过滤器(Filt... 目录前言一、核心功能二、拦截器的实现2.1 定义自定义拦截器2.2 注册拦截器三、多拦截器的执行顺序四、过

springboot的controller中如何获取applicatim.yml的配置值

《springboot的controller中如何获取applicatim.yml的配置值》本文介绍了在SpringBoot的Controller中获取application.yml配置值的四种方式,... 目录1. 使用@Value注解(最常用)application.yml 配置Controller 中

springboot中配置logback-spring.xml的方法

《springboot中配置logback-spring.xml的方法》文章介绍了如何在SpringBoot项目中配置logback-spring.xml文件来进行日志管理,包括如何定义日志输出方式、... 目录一、在src/main/resources目录下,也就是在classpath路径下创建logba

C++多线程开发环境配置方法

《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks.... 目录下载安装 MinGW-w64下载安装VS code创建测试项目配置编译任务创建 tasks.js