首页 > 其他 > 详细

Shiro集成多个Realm,认证都不通过返回y configured realms. Please ensure that at least one realm can authenticate these tokens.

时间:2021-05-17 15:37:34      阅读:15      评论:0      收藏:0      [点我收藏+]

异常内容:Authentication token of type [class org.apache.shiro.authc.UsernamePasswordToken] could not be authenticated by any configured realms. Please ensure that at least one realm can authenticate these tokens.

意思是没有Realm进行处理,但实际上是当配置了多个realm时,就会 org.apache.shiro.authc.pam.ModularRealmAuthenticator#doMultiRealmAuthentication 通过这个方法进行执行,如果所有的对应的realm都返回认证异常或者null的话,就会出现以上错误

解决办法

第一种  集成ModularRealmAuthenticator重写doMultiRealmAuthentication方法

第二种,重写Authenticator的认证策略,由于ModularRealmAuthenticator默认是配置的这个 AtLeastOneSuccessfulStrategy策略,那么重写以下方法,当异常是认证异常时,则进行抛出认证异常

public class MyAtLeastOneSuccessfulStrategy extends AtLeastOneSuccessfulStrategy {

    @Override
    public AuthenticationInfo afterAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo singleRealmInfo, AuthenticationInfo aggregateInfo, Throwable t) throws AuthenticationException {
        if(t instanceof AuthenticationException){
            throw (AuthenticationException)t;
        }
        return super.afterAttempt(realm, token, singleRealmInfo, aggregateInfo, t);
    }

    public static void main(String[] args) {
        Provider[] providers = Security.getProviders();
        System.out.println(providers);
    }

}

技术分享图片

 

 根据所需抛出的异常时机,重写以上方法即可,方法作用直接看doc就能明白

 

 

那么最关键的怎么配置呢?

技术分享图片

 

 这2个shiro的配置类,至少我这边没有使用上,如果你的代码使用了,继承此类重写对应的方法返回的实例即可。

而我是这么解决的。

技术分享图片

 

Shiro集成多个Realm,认证都不通过返回y configured realms. Please ensure that at least one realm can authenticate these tokens.

原文:https://www.cnblogs.com/XingXiaoMeng/p/14776672.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!