shiro 多realm报错could not be authenticated by any configured realms. Please ensure that at least o

使用中的项目采用shiro,虽然使用下来感觉虽然轻量化,但是很多东西还是要自己摸索,最近正好碰到一个问题就是采用多种方式登录,有一个方式成功就成功登录,相比一些功能比如说踢人什么的,这个多种方式登录时shiro自带的。这里的登录方式在有的文档叫做域,在有的文档直接用文档上的英文realm来使用。

具体的配置过程就不啰嗦了,很多文档都有,我就讲一下我遇到的问题

但是哪有那么一帆风顺,就是按照先相关资料实现多realm验证结果报错

Authentication token of type [class com.xxx.xxx.web.config.shiro.common.XxxxToken] could not be authenticated by any configured realms.  Please ensure that at least one realm can authenticate these tokens.

第一种情况 输错密码或者没有按照材料配置好

实际上细心一点不会出现,这种错误就是类似那种“我大意了,没有闪”,自己掉坑里

实际上这个问题看错误提示第一个问题就是有没有,就是at least one,就是有没有对应realm验证通过,其实在配置得当的情况下,说明要不然你的输入有问题。比如你输入密码账户错误,或者你输入的验证码错误,这种问题纯属于自己个自己找麻烦,排除这个很简单就是debug你想要的通过的realm的实现逻辑,就一目了然问题出在什么情况上。

当然有一种情况是没有设置,多realm下只有一个通过就通过AtLeastOneSuccessfulStrategy
一般来说我们针对多realm一般会自己定义一个继承ModularRealmAuthenticator的,如下

package com.lxzl.ams.web.config.shiro.common;

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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 自定义Authenticator 当配置了多个Realm时,我们通常使用的认证器是shiro自带的org.apache.shiro.authc.pam.ModularRealmAuthenticator,其中决定使用的Realm的是doAuthenticate()方法
 */
class UserModularRealmAuthenticator extends ModularRealmAuthenticator {

    private static final Logger logger = LoggerFactory.getLogger(UserModularRealmAuthenticator.class);

    @Override
    protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken)
            throws AuthenticationException {
        logger.info("UserModularRealmAuthenticator:method doAuthenticate() execute ");
        // 判断getRealms()是否返回为空
        assertRealmsConfigured();
        // 强制转换回自定义的CustomizedToken
        UserToken userToken = (UserToken) authenticationToken;
        // 登录类型
        String loginType = userToken.getLoginType();
        // 所有Realm
        Collection<Realm> realms = getRealms();
        // 登录类型对应的所有Realm
        Collection<Realm> typeRealms = new ArrayList<>();
        for (Realm realm : realms) {
            if (realm.getName().contains(loginType)) {
                typeRealms.add(realm);
            }
        }
        // 判断是单Realm还是多Realm
        if (typeRealms.size() == 1){
            logger.info("doSingleRealmAuthentication() execute ");
            return doSingleRealmAuthentication(typeRealms.iterator().next(), userToken);
        }
        else{
            logger.info("doMultiRealmAuthentication() execute ");
            return doMultiRealmAuthentication(typeRealms, userToken);
        }
    }
}

设置atleast,在配置类里面

  /**
     * 系统自带的Realm管理,主要针对多realm
     */
    @Bean
    public ModularRealmAuthenticator modularRealmAuthenticator() {
        //自己重写的ModularRealmAuthenticator
        UserModularRealmAuthenticator modularRealmAuthenticator = new UserModularRealmAuthenticator();
        modularRealmAuthenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());
        return modularRealmAuthenticator;
    }

当然这种错误也不是我犯的错误

第二种错误 没有返回可通过的authenticationInfo
这个我这里是因为,我们之前账户设置要验证一些逻辑,结果我加了一个第三方登录直接卡在这,shiro的subject不认,authenticationInfo无效。

这可咋整,这时候shiro的一个api可以救你,他就是

allowAllCredentialsMatcher

我们看一下shiro的allowAllCredentialsMatcher的api怎么说的
在这里插入图片描述
上面写得很清楚,添加了他,只要通过那就永远返回一个可信任的为true的结果

使用方式如下:

 @Bean
    public WeChatWorkShiroRealm weChatWorkShiroRealm() {
        WeChatWorkShiroRealm weChatWorkShiroRealm = new WeChatWorkShiroRealm();
        weChatWorkShiroRealm.setCredentialsMatcher(allowAllCredentialsMatcher());
        return weChatWorkShiroRealm;
    }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值