6. Shiro_多 Realm 验证

1.多Realm配置

存在这样一种场景,同一个密码可能在MqSQL中存储,也可能在Oracle中存储,有可能MqSQL中使用的是MD5加密算法,而Oracle使用SHA1加密算法。这就需要有多个Realm以及认证策略的问题

<bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
         <property name="realms">
             <list>
                 <ref bean="jdbcRealm"/>
                 <ref bean="secondRealm"/>
             </list>
         </property>
</bean>
<!-- 
        3.配置Realm
        3.1直接配置实现了org.apache.shiro.realm.Realm接口的Realm;
     -->
     <!-- id设置名称,class对应写的类 -->
    <bean id="jdbcRealm" class="com.yang.shiro.realm.ShiroRealm">
         <property name="credentialsMatcher">
             <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
               <!-- 指定加密方式和次数  -->
                <property name="hashAlgorithmName" value="MD5"></property>
                <property name="hashIterations" value="1024"></property>
             </bean>
         </property>
    </bean>
    
    <bean id="secondRealm" class="com.yang.shiro.realm.SecondRealm">
         <property name="credentialsMatcher">
             <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
               <!-- 指定加密方式和次数  -->
                <property name="hashAlgorithmName" value="SHA1"></property>
                <property name="hashIterations" value="1024"></property>
             </bean>
         </property>
    </bean>

配置完两个Realm之后,还需要把这两个Realm加入到securityManager管理器中:

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="cacheManager" ref="cacheManager"/>
        <!-- <property name="realm" ref="jdbcRealm"/> -->
        <property name="authenticator" ref="authenticator"></property>
    </bean>

2.认证的策略:怎样算认证成功

<bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
         <property name="realms">
             <list>
                 <ref bean="jdbcRealm"/>
                 <ref bean="secondRealm"/>
             </list>
         </property>
         <!-- 认证策略,都认证成功才行 -->
         <property name="authenticationStrategy">
             <bean class="org.apache.shiro.authc.pam.AllSuccessfulStrategy"></bean>
         </property>
</bean>

3.15. Shiro_把 realms 直接配置给 SecurityManager

这样shiro初始化得时候就会把securityManager得reals给authenticator。

<!-- 1.配置securityManager-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="cacheManager" ref="cacheManager"/>
        <!-- <property name="realm" ref="jdbcRealm"/> -->
        <property name="authenticator" ref="authenticator"></property>
        
        <property name="realms">
             <list>
                 <ref bean="jdbcRealm"/>
                 <ref bean="secondRealm"/>
             </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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值