zoukankan      html  css  js  c++  java
  • Shiro集成多个Realm,认证都不通过返回y configured realms. Please ensure that at least one realm can authenticate these tokens.

    异常内容: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的配置类,至少我这边没有使用上,如果你的代码使用了,继承此类重写对应的方法返回的实例即可。

    而我是这么解决的。

    复制请注明出处,在世界中挣扎的灰太狼
  • 相关阅读:
    编译安装glibc
    Android SDK下载
    Ubuntu下Android编译环境的配置
    ubuntu常用命令
    硬盘概念解析
    ubuntu官方源列表网址
    Win7系统下利用U盘安装Ubuntu14.04麒麟版
    jdk5下载链接
    vim跳到文件头和文末结尾
    vim 全局替换命令
  • 原文地址:https://www.cnblogs.com/XingXiaoMeng/p/14776672.html
Copyright © 2011-2022 走看看