八、AuthenticationStrategy(身份验证策略)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Mr_yangzc/article/details/82787359

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"/>

猜你喜欢

转载自blog.csdn.net/Mr_yangzc/article/details/82787359