apache-shiro杂记(二) 关于多realm认证的策略

来源:互联网 发布:河北seo按效果付费 编辑:程序博客网 时间:2024/06/10 23:30
一直以为给定的下面配置是短路方式的,即defaultJdbcRealm可以成功认证,backDoorJdbcRealm就不会被调用。
其实不然,org.apache.shiro.authc.pam.FirstSuccessfulStrategy并不是这个意思,所有的realm依然都会被调用。
只不过是第一个认证成功的AuthenticationInfo作为最后的结果返回。
Xml代码  收藏代码
  1. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
  2.     <!-- 其他配置 -->  
  3.     <property name="authenticator" ref="authenticator" />  
  4.     <property name="realms">  
  5.         <list>  
  6.             <ref bean="defaultJdbcRealm" />  
  7.             <ref bean="backDoorJdbcRealm" />  
  8.         </list>  
  9.     </property>  
  10. </bean>  
  11.   
  12. <bean id="defaultJdbcRealm" class="..." />  
  13. <bean id="backDoorJdbcRealm" class="..." />  
  14.   
  15. <bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">  
  16.     <property name="authenticationStrategy">  
  17.         <bean class="org.apache.shiro.authc.pam.FirstSuccessfulStrategy" />  
  18.     </property>  
  19. </bean>  


为了实现目的,必须对org.apache.shiro.authc.pam.ModularRealmAuthenticator改造。
Java代码  收藏代码
  1. package xxx.yyy.security;  
  2.   
  3. import java.util.Collection;  
  4. import org.apache.shiro.authc.AuthenticationInfo;  
  5. import org.apache.shiro.authc.AuthenticationToken;  
  6. import org.apache.shiro.authc.pam.AuthenticationStrategy;  
  7. import org.apache.shiro.realm.Realm;  
  8. import org.apache.shiro.util.CollectionUtils;  
  9.   
  10. public class ModularRealmAuthenticator extends org.apache.shiro.authc.pam.ModularRealmAuthenticator {  
  11.   
  12.     @Override  
  13.     protected AuthenticationInfo doMultiRealmAuthentication(Collection<Realm> realms, AuthenticationToken token) {  
  14.           
  15.         AuthenticationStrategy strategy = getAuthenticationStrategy();  
  16.         AuthenticationInfo aggregate = strategy.beforeAllAttempts(realms, token);  
  17.           
  18.         for (Realm realm : realms) {  
  19.             aggregate = strategy.beforeAttempt(realm, token, aggregate);  
  20.             if (realm.supports(token)) {  
  21.                 AuthenticationInfo info = null;  
  22.                 Throwable t = null;  
  23.                 try {  
  24.                     info = realm.getAuthenticationInfo(token);  
  25.                 } catch (Throwable throwable) {  
  26.                     t = throwable;  
  27.                 }  
  28.                 aggregate = strategy.afterAttempt(realm, token, info, aggregate, t);  
  29.                 // dirty dirty hack  
  30.                 if (aggregate != null && !CollectionUtils.isEmpty(aggregate.getPrincipals())) {  
  31.                     return aggregate;  
  32.                 }  
  33.                 // end dirty dirty hack  
  34.             } else {  
  35.   
  36.             }  
  37.         }  
  38.         aggregate = strategy.afterAllAttempts(token, aggregate);  
  39.         return aggregate;  
  40.     }  
  41. }  

Xml代码  收藏代码
  1. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
  2.     <!-- 其他配置 -->  
  3.     <property name="authenticator" ref="authenticator" />  
  4.     <property name="realms">  
  5.         <list>  
  6.             <ref bean="defaultJdbcRealm" />  
  7.             <ref bean="backDoorJdbcRealm" />  
  8.         </list>  
  9.     </property>  
  10. </bean>  
  11.   
  12. <bean id="defaultJdbcRealm" class="..." />  
  13. <bean id="backDoorJdbcRealm" class="..." />  
  14.   
  15. <bean id="authenticator" class="xxx.yyy.security.ModularRealmAuthenticator">  
  16.     <property name="authenticationStrategy">  
  17.         <bean class="org.apache.shiro.authc.pam.FirstSuccessfulStrategy" />  
  18.     </property>  
  19. </bean> 
0 0
原创粉丝点击