1、序
由于项目中需要同时实现两种访问方式(JWT和普通登陆访问),在网上搜了下使用shiro相关的实现,方法有挺多的,但是有部分方法的代码耦合性太强,因为另一种方式可能以后会拆分出去作为一个独立项目的登陆,因此希望两种登陆方式彼此间的耦合尽量少。
2、登陆流程
由于希望找到一种低入侵性的实现方式,所以整理了一下登陆流程,从FormAuthenticationFilter的onAccessDenied方法作为查看登陆的入口对代码进行追踪,发现了一个有意思的地方,FormAuthenticationFilter的executeLogin方法最后调用了ModularRealmAuthenticator这个类的doAuthenticate方法
protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException{
this.assertRealmsConfigured();
Collection realms = this.getRealms();
return realms.size() == 1 ? this.doSingleRealmAuthentication((Realm)realms.iterator().next(), authenticationToken) : this.doMultiRealmAuthentication(realms, authenticationToken);
}
复制代码
可以看到这里的realms实际是个集合,且单realm和多个realm的登陆方式不同,于是我追进多realm的登陆方法中看看
protected AuthenticationInfo doMultiRealmAuthentication(Collection realms, AuthenticationToken token){
AuthenticationStrategy strategy = this.getAuthenticationStrategy();
AuthenticationInfo aggregate = strategy.beforeAllAttempts(realms, token);
if (log.isTraceEnabled()) {
log.trace("Iterating through {} realms for PAM authentication", realms.size());
}
Iterator var5 = realms.iterator();
while(var5.hasNext()) {