8、AuthenticationStrategy(身份验证策略)

AuthenticationStrategy(身份验证策略)html

官方文档:shiro.apache.org/authenticat…web

AuthenticationStrategy 是个无状态的组件,在认证过程当中会进行4次调用。spring

  • ① 在全部Realm被调用以前
  • ②在调用Realm的getAuthenticationInfo方法以前
  • ③在调用Realm的getAuthenticationInfo 方法以后
  • ④在全部Realm被调用以后

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

在这里插入图片描述

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

在这里插入图片描述

Shiro有3中认证策略的具体实现 AuthenticationStrategy类:session

  • AtLeastOneSuccessfulStrategy(默认) 只要一个或者多个Realm认证经过,则总体身份认证就会视为成功。
  • FirstSuccessfulStrategy 只有第一个验证经过,才会视为总体认证经过。其余的会被忽略。
  • AllSuccessfulStrategy 只有全部的Realm认证成功,才会被视为认证经过 自定义策略:继承org.apache.shiro.authc.pam.AbstractAuthenticationStrategy。
  • Realm顺序对认证是有影响的。

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

<!-- 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"/>
复制代码
相关文章
相关标签/搜索