【Shiro权限管理】13. SecurityManager配置realms
来源:互联网 发布:淘宝的美工教学视频 编辑:程序博客网 时间:2024/05/19 06:18
注:该系列所有测试均在之前创建的Shiro3的Web工程的基础上。
大家可以注意到,在没有使用认证器之前,我们是这样配置Realm的:
后来使用了多Realm验证时,会将多个Realm配置到认证器中,再将认证器配置给securityManager:
那么,我们如果将authenticator中的realms删除,而重新在securityManager添加两个Realm配置,
我们的工程还能跑起来吗?可以试验一下,首先将配置改为一下配置:
发现依然可以进行正常的校验:
那么可以证明这种写法是没有问题的,但是我们要想一下,为什么这样可以呢?
可以看到,我们是在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的:
<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
阅读全文
0 0
- 【Shiro权限管理】13. SecurityManager配置realms
- 2.详解SecurityManager(shiro权限管理门面)
- shiro配置报错No SecurityManager
- 自学-Shiro中多个Realms的另外一种配置方式-11
- 【Shiro权限管理】5. Shiro权限URL 配置细节
- Apache Shiro Realms
- Shiro自定义Realms
- Shiro 权限管理filterChainDefinitions过滤器配置
- Shiro 权限管理filterChainDefinitions过滤器配置
- Shiro 权限管理filterChainDefinitions过滤器配置
- Shiro 权限管理filterChainDefinitions过滤器配置
- 4.SSM配置shiro权限管理
- 第六章:Shiro的Realms
- 【Shiro】SpringMVC+Shiro权限管理
- shiro securityManager结构
- Shiro源码之SecurityManager
- Shiro详解之SecurityManager
- SpringMVC+Shiro权限管理
- 6.5
- zlib的编译和使用
- SourceTree windows版本免注册免登陆使用方法
- HDU 5701 中位数计数(思维+暴力)
- 把树莓派的时间调节到北京时间
- 【Shiro权限管理】13. SecurityManager配置realms
- 一步一步学Makefile (1)
- Linux中VMware虚拟机增加磁盘空间的扩容操作[图文]
- android检查内存泄漏
- Yslow---一款很实用的web性能测试插件
- 数据库中间件Atlas调研笔记
- linux 命令生成合法的mac 地址
- 算法入门之动态规划的概念
- 编程之路小细节-工具类随机字的生产