八、AuthenticationStrategy(身份验证策略)

AuthenticationStrategy(身份验证策略)

官方文档:https://shiro.apache.org/authentication.html#authenticationstrategy

AuthenticationStrategy 是个无状态的组件,在认证过程中会进行4次调用。
① 在所有Realm被调用之前
②在调用Realm的getAuthenticationInfo方法之前
③在调用Realm的getAuthenticationInfo 方法之后
④在所有Realm被调用之后

ModularRealmAuthenticator
org.apache.shiro.authc.pam.ModularRealmAuthenticator

在这里插入图片描述

AuthenticationStrategy
org.apache.shiro.authc.pam.AuthenticationStrategy

在这里插入图片描述

Shiro有3中认证策略的具体实现 AuthenticationStrategy类:
AtLeastOneSuccessfulStrategy(默认)
只要一个或者多个Realm认证通过,则整体身份认证就会视为成功。
FirstSuccessfulStrategy
只有第一个验证通过,才会视为整体认证通过。其他的会被忽略。
AllSuccessfulStrategy
只有所有的Realm认证成功,才会被视为认证通过
自定义策略:继承org.apache.shiro.authc.pam.AbstractAuthenticationStrategy。

  • Realm顺序对认证是有影响的。

spring-shiro.xml 配置 认证策略:


<!-- Shiro默认会使用Servlet容器的Session,可通过sessionMode属性来指定使用Shiro原生Session -->
<!-- 这里主要是设置自定义的单Realm应用,若有多个Realm,可使用'realms'属性代替 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <property name="cacheManager" ref="cacheManager"/>
     <!--<property name="realm" ref="myRealm"/>-->
     
    <property name="authenticator" ref="authenticator"/>
</bean>

<!--多个realm 配置-->
<bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
    <!--配置认证策略-->
    <property name="authenticationStrategy" ref="allSuccessfulStrategy"/>
    <property name="realms">
        <list>
            <ref bean="firstRealm"/>
            <ref bean="secondRealm"/>
        </list>
    </property>
</bean>
<!--全部通过-->
<bean id="allSuccessfulStrategy" class="org.apache.shiro.authc.pam.AllSuccessfulStrategy"/>
<!--只有第一个验证通过,才会视为整体认证通过。其他的会被忽略。-->
<bean id="firstSuccessfulStrategy" class="org.apache.shiro.authc.pam.FirstSuccessfulStrategy"/>
<!--默认-->
<bean id="atLeastOneSuccessfulStrategy" class="org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy"/>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值