shiro 实现多种方式登录_同一个项目中shiro多种登陆方式的一种实现

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()) {

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值