博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
apache-shiro杂记(二) 关于多realm认证的策略
阅读量:3684 次
发布时间:2019-05-21

本文共 2659 字,大约阅读时间需要 8 分钟。

一直以为给定的下面配置是短路方式的,即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> 

转载地址:http://jsxdn.baihongyu.com/

你可能感兴趣的文章
打印三角形及debug单步调试
查看>>
方法详解(命名规则)
查看>>
方法的定义及修饰符
查看>>
方法重载
查看>>
命令行传参及dos编译命令
查看>>
可变参数以及非静态方法如何调用
查看>>
数组内存分析及三种数组初始化方法
查看>>
数组特点,数组越界
查看>>
数组的使用:foreach,数组做方法入参,数组做返回值
查看>>
多维数组
查看>>
arrays类详解,util工具包以及如何查看学习一个新类,打印输出数组hashcode及数组内容,数组排序,遍历输出,程序中的大括号怎么写和看。
查看>>
八大排序之冒泡排序
查看>>
将二维数组转换为稀疏数组,并将稀疏数组还原为二维数组
查看>>
面向对象OOP(Object Oriented Programming)
查看>>
方法如何定义与调用,类与对象详解,值传递和引用传递,new对象快捷键,idea如何并列两个程序,static关键字,方法的回顾与加深,break,continue和return的区别
查看>>
类与对象的创建以及初始化
查看>>
java类中的构造器详解,如何在IDEA中导入查看.java 源文件,IDEA构造方法快捷键,new
查看>>
创建对象内存分析,以及类与对象小结
查看>>
封装详解,private,修改私有属性get/set方法,类和对象总结
查看>>
继承详解及其快捷键
查看>>