【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

相关文章

Vue 2 项目中配置 Tailwind CSS 和 Font Awesome 的最佳实践举例

《Vue2项目中配置TailwindCSS和FontAwesome的最佳实践举例》:本文主要介绍Vue2项目中配置TailwindCSS和FontAwesome的最... 目录vue 2 项目中配置 Tailwind css 和 Font Awesome 的最佳实践一、Tailwind CSS 配置1. 安

SpringBoot中HTTP连接池的配置与优化

《SpringBoot中HTTP连接池的配置与优化》这篇文章主要为大家详细介绍了SpringBoot中HTTP连接池的配置与优化的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、HTTP连接池的核心价值二、Spring Boot集成方案方案1:Apache HttpCl

Maven 插件配置分层架构深度解析

《Maven插件配置分层架构深度解析》:本文主要介绍Maven插件配置分层架构深度解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Maven 插件配置分层架构深度解析引言:当构建逻辑遇上复杂配置第一章 Maven插件配置的三重境界1.1 插件配置的拓扑

Spring Boot集成Logback终极指南之从基础到高级配置实战指南

《SpringBoot集成Logback终极指南之从基础到高级配置实战指南》Logback是一个可靠、通用且快速的Java日志框架,作为Log4j的继承者,由Log4j创始人设计,:本文主要介绍... 目录一、Logback简介与Spring Boot集成基础1.1 Logback是什么?1.2 Sprin

VSCode中配置node.js的实现示例

《VSCode中配置node.js的实现示例》本文主要介绍了VSCode中配置node.js的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录一.node.js下载安装教程二.配置npm三.配置环境变量四.VSCode配置五.心得一.no

Gradle在国内配置镜像加速的实现步骤

《Gradle在国内配置镜像加速的实现步骤》在国内使用Gradle构建项目时,最大的痛点就是依赖下载贼慢,甚至卡死,下面教你如何配置国内镜像加速Gradle下载依赖,主要是通过改写repositori... 目录引言一、修改 build.gradle 或 settings.gradle 的 reposito

使用easy connect之后,maven无法使用,原来需要配置-Djava.net.preferIPv4Stack=true问题

《使用easyconnect之后,maven无法使用,原来需要配置-Djava.net.preferIPv4Stack=true问题》:本文主要介绍使用easyconnect之后,maven无法... 目录使用easGWowCy connect之后,maven无法使用,原来需要配置-DJava.net.pr

Python+PyQt5开发一个Windows电脑启动项管理神器

《Python+PyQt5开发一个Windows电脑启动项管理神器》:本文主要介绍如何使用PyQt5开发一款颜值与功能并存的Windows启动项管理工具,不仅能查看/删除现有启动项,还能智能添加新... 目录开篇:为什么我们需要启动项管理工具功能全景图核心技术解析1. Windows注册表操作2. 启动文件

史上最全nginx详细参数配置

《史上最全nginx详细参数配置》Nginx是一个轻量级高性能的HTTP和反向代理服务器,同时也是一个通用代理服务器(TCP/UDP/IMAP/POP3/SMTP),最初由俄罗斯人IgorSyso... 目录基本命令默认配置搭建站点根据文件类型设置过期时间禁止文件缓存防盗链静态文件压缩指定定错误页面跨域问题

nginx负载均衡及详细配置方法

《nginx负载均衡及详细配置方法》Nginx作为一种高效的Web服务器和反向代理服务器,广泛应用于网站的负载均衡中,:本文主要介绍nginx负载均衡及详细配置,需要的朋友可以参考下... 目录一、 nginx负载均衡策略1.1 基本负载均衡策略1.2 第三方策略1.3 策略对比二、 nginx配置2.1