阅读背景:

Spring Boot(十一)Shiro 整合 配置多 Realm_好好学习 天天向上

来源:互联网 
import java.util.ArrayList;
import java.util.Collection;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;

/**
 * CustomizedModularRealmAuthenticator
 * 多Realm验证模块
 */
public class CustomizedModularRealmAuthenticator extends ModularRealmAuthenticator {

    @Override
    protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken)
            throws AuthenticationException {
        // 判断getRealms()是否返回为空
        assertRealmsConfigured();
        // CustomizedToken
        CustomizedToken customizedToken = (CustomizedToken) authenticationToken;
        // 登录类型
        String loginType = customizedToken.getLoginType();
        // 所有Realm
        Collection<Realm> realms = getRealms();
        // 登录类型对应的所有Realm
        Collection<Realm> typeRealms = new ArrayList<>();
        for (Realm realm : realms) {
            //可以修改此规则来匹配你的登录类型
            if (realm.getName().contains(loginType)) {
                typeRealms.add(realm);
            }
        }
        if (typeRealms.size() == 1) {
            //单Realm
            return doSingleRealmAuthentication(typeRealms.iterator().next(), customizedToken);
        }
        else {
            //多Realm
            return doMultiRealmAuthentication(typeRealms, customizedToken);
        }
    }
}
import java.util.ArrayList;
import java.



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: