apache-shiro杂记(二) 关于多realm认证的策略
来源:互联网 发布:河北seo按效果付费 编辑:程序博客网 时间:2024/06/10 23:30
一直以为给定的下面配置是短路方式的,即defaultJdbcRealm可以成功认证,backDoorJdbcRealm就不会被调用。
其实不然,org.apache.shiro.authc.pam.FirstSuccessfulStrategy并不是这个意思,所有的realm依然都会被调用。
只不过是第一个认证成功的AuthenticationInfo作为最后的结果返回。
为了实现目的,必须对org.apache.shiro.authc.pam.ModularRealmAuthenticator改造。
其实不然,org.apache.shiro.authc.pam.FirstSuccessfulStrategy并不是这个意思,所有的realm依然都会被调用。
只不过是第一个认证成功的AuthenticationInfo作为最后的结果返回。
- <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
- <!-- 其他配置 -->
- <property name="authenticator" ref="authenticator" />
- <property name="realms">
- <list>
- <ref bean="defaultJdbcRealm" />
- <ref bean="backDoorJdbcRealm" />
- </list>
- </property>
- </bean>
- <bean id="defaultJdbcRealm" class="..." />
- <bean id="backDoorJdbcRealm" class="..." />
- <bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
- <property name="authenticationStrategy">
- <bean class="org.apache.shiro.authc.pam.FirstSuccessfulStrategy" />
- </property>
- </bean>
为了实现目的,必须对org.apache.shiro.authc.pam.ModularRealmAuthenticator改造。
- package xxx.yyy.security;
- import java.util.Collection;
- import org.apache.shiro.authc.AuthenticationInfo;
- import org.apache.shiro.authc.AuthenticationToken;
- import org.apache.shiro.authc.pam.AuthenticationStrategy;
- import org.apache.shiro.realm.Realm;
- import org.apache.shiro.util.CollectionUtils;
- public class ModularRealmAuthenticator extends org.apache.shiro.authc.pam.ModularRealmAuthenticator {
- @Override
- protected AuthenticationInfo doMultiRealmAuthentication(Collection<Realm> realms, AuthenticationToken token) {
- AuthenticationStrategy strategy = getAuthenticationStrategy();
- AuthenticationInfo aggregate = strategy.beforeAllAttempts(realms, token);
- for (Realm realm : realms) {
- aggregate = strategy.beforeAttempt(realm, token, aggregate);
- if (realm.supports(token)) {
- AuthenticationInfo info = null;
- Throwable t = null;
- try {
- info = realm.getAuthenticationInfo(token);
- } catch (Throwable throwable) {
- t = throwable;
- }
- aggregate = strategy.afterAttempt(realm, token, info, aggregate, t);
- // dirty dirty hack
- if (aggregate != null && !CollectionUtils.isEmpty(aggregate.getPrincipals())) {
- return aggregate;
- }
- // end dirty dirty hack
- } else {
- }
- }
- aggregate = strategy.afterAllAttempts(token, aggregate);
- return aggregate;
- }
- }
- <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
- <!-- 其他配置 -->
- <property name="authenticator" ref="authenticator" />
- <property name="realms">
- <list>
- <ref bean="defaultJdbcRealm" />
- <ref bean="backDoorJdbcRealm" />
- </list>
- </property>
- </bean>
- <bean id="defaultJdbcRealm" class="..." />
- <bean id="backDoorJdbcRealm" class="..." />
- <bean id="authenticator" class="xxx.yyy.security.ModularRealmAuthenticator">
- <property name="authenticationStrategy">
- <bean class="org.apache.shiro.authc.pam.FirstSuccessfulStrategy" />
- </property>
- </bean>
0 0
- apache-shiro杂记(二) 关于多realm认证的策略
- Shiro之多Realm的认证及认证策略-yellowcong
- Shiro 的身份认证-Realm
- (二)Apache Shiro 身份验证之Realm
- shiro 认证 配置单个realm和多个realm时的写法
- shiro多登录入口,多realm认证
- Shiro多个Realm认证及授权
- shiro-realm认证
- Shiro 自定义realm认证
- Apache Shiro 使用手册(二)Shiro 认证
- Apache Shiro 使用手册(二)Shiro 认证
- Apache Shiro 使用手册(二)Shiro 认证
- Apache Shiro 使用手册(二)Shiro 认证
- Apache Shiro 使用手册(二)Shiro 认证
- Apache Shiro 使用手册(二)Shiro 认证
- Apache Shiro 使用手册(二)Shiro 认证
- Apache Shiro 使用手册(二)Shiro 认证
- Apache Shiro 使用手册(二)Shiro 认证
- HDU 5974 A Simple Math Problem (数学)
- Android开发-DesignDemo-AndroidStudio(十一)FloatingActionButton(3)
- android studio使用教学提高篇 重点难点
- 数据结构与算法-实验2-自定义栈,并实现push、pop、改栈顶元素、取栈顶元素
- LeetCode 33. Search in Rotated Sorted Array
- apache-shiro杂记(二) 关于多realm认证的策略
- BSTR ASCII Unicode _T 宏
- java 签名
- 自定义View笔记
- 正则表达式
- Django(Session,Cookie)
- bootstrap学习总结-css组件(三)
- cpp 4.22
- Android官方MVP架构示例项目解析