多个realm时,抛出自定义的异常

1.继承一个ModularRealmAuthenticator类

创建一个类,继承ModularRealmAuthenticator ,然后重写 doMultiRealmAuthentication方法

/**
  * ,抛出realm中第一个遇到的异常
  * @param realms 领域认证器
  * @param token 认证凭证
  * @return aggregate
  */
@Override
protected AuthenticationInfo doMultiRealmAuthentication(Collection<Realm> realms, AuthenticationToken token) {
    AuthenticationStrategy strategy = getAuthenticationStrategy();
    AuthenticationInfo aggregate = strategy.beforeAllAttempts(realms, token);
    if (log.isTraceEnabled()) {
        log.trace("Iterating through {} realms for PAM authentication", realms.size());
    }
    for (Realm realm : realms) {
        aggregate = strategy.beforeAttempt(realm, token, aggregate);
        if (realm.supports(token)) {
            log.trace("Attempting to authenticate token [{}] using realm [{}]", token, realm);
            AuthenticationInfo info = null;
            //有异常从此处抛出
            info = realm.getAuthenticationInfo(token);
            aggregate = strategy.afterAttempt(realm, token, info, aggregate, null);
        } else {
            log.debug("Realm [{}] does not support token {}.  Skipping realm.", realm, token);
        }
    }
    aggregate = strategy.afterAllAttempts(token, aggregate);
    return aggregate;
}

2.在shiroConfig中把自定义的 ModularRealmAuthenticator 加入到shiro中

     /**
     * shiro 安全管理器设置
     * @return SecurityManager
     */
    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setSessionManager(sessionManager());
        //自定义的模块化领域认证(ModularRealmAuthenticator)
        ModularRealmAuthenticator authenticator = new WxModularRealmAuthenticator();
        securityManager.setAuthenticator( authenticator );
        return securityManager;
    }

转载于:https://my.oschina.net/u/2555967/blog/2961814

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值