使用中的项目采用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;
}