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

    而我是这么解决的。

    复制请注明出处,在世界中挣扎的灰太狼
  • 相关阅读:
    NGINX学习(二)--nginx配置文件详解
    NGINX学习(一)--nginx的安装与启动
    Linux服务器参数查看命令
    记录工作中遇到的那些坑(一)--NGINX配置worker_connections
    Mysql视图触发器函数事务
    pymsql
    装饰器模型
    多进程属性和方法
    mysql
    FTP简单的大文件传输
  • 原文地址:https://www.cnblogs.com/XingXiaoMeng/p/14776672.html
Copyright © 2011-2022 走看看