AuthenticationStrategy(身份驗證策略)
官方文檔:https://shiro.apache.org/authentication.html#authenticationstrategy
AuthenticationStrategy 是個無狀態(tài)的組件,在認(rèn)證過程中會進(jìn)行4次調(diào)用荞胡。
① 在所有Realm被調(diào)用之前
②在調(diào)用Realm的getAuthenticationInfo方法之前
③在調(diào)用Realm的getAuthenticationInfo 方法之后
④在所有Realm被調(diào)用之后
ModularRealmAuthenticator
org.apache.shiro.authc.pam.ModularRealmAuthenticator
AuthenticationStrategy
org.apache.shiro.authc.pam.AuthenticationStrategy
Shiro有3中認(rèn)證策略的具體實(shí)現(xiàn) AuthenticationStrategy類:
AtLeastOneSuccessfulStrategy(默認(rèn))
只要一個或者多個Realm認(rèn)證通過弧轧,則整體身份認(rèn)證就會視為成功矾削。
FirstSuccessfulStrategy
只有第一個驗證通過,才會視為整體認(rèn)證通過哥童。其他的會被忽略挺份。
AllSuccessfulStrategy
只有所有的Realm認(rèn)證成功,才會被視為認(rèn)證通過
自定義策略:繼承org.apache.shiro.authc.pam.AbstractAuthenticationStrategy贮懈。
- Realm順序?qū)φJ(rèn)證是有影響的匀泊。
spring-shiro.xml 配置 認(rèn)證策略:
<!-- Shiro默認(rèn)會使用Servlet容器的Session,可通過sessionMode屬性來指定使用Shiro原生Session -->
<!-- 這里主要是設(shè)置自定義的單Realm應(yīng)用,若有多個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">
<!--配置認(rèn)證策略-->
<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"/>
<!--只有第一個驗證通過优训,才會視為整體認(rèn)證通過。其他的會被忽略各聘。-->
<bean id="firstSuccessfulStrategy" class="org.apache.shiro.authc.pam.FirstSuccessfulStrategy"/>
<!--默認(rèn)-->
<bean id="atLeastOneSuccessfulStrategy" class="org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy"/>