Spring Security账号密码认证源码解析

本文详细解析了Spring Security的认证流程,从AuthenticationProvider接口到ProviderManager的authenticate方法,再到DaoAuthenticationProvider的实现,包括如何进行用户信息检索、密码验证及安全检查。通过对UsernamePasswordAuthenticationToken的处理,阐述了整个认证过程。
摘要由CSDN通过智能技术生成

// 该接口有两个方法,该接口的实现类主要做认证的。

public interface AuthenticationProvider {

Authentication authenticate(Authentication var1) throws AuthenticationException;

// supports是用来检测该类型的认证信息是否可以被自己处理。可以被处理则调用自身的authenticate方法。

boolean supports(Class<?> var1);

}

(4)ProviderManager的authenticate()方法源码关键部分如下:

public Authentication authenticate(Authentication authentication) throws AuthenticationException {

Class toTest = authentication.getClass();

Object lastException = null;

Authentication result = null;

boolean debug = logger.isDebugEnabled();

// 拿到全部的provider

Iterator e = this.getProviders().iterator();

// 遍历provider

while(e.hasNext()) {

AuthenticationProvider provider = (AuthenticationProvider)e.next();

// 挨着个的校验是否支持当前token

if(provider.supports(toTest)) {

if(debug) {

logger.debug("Authentication attempt using " + provider.getClass().getName());

}

try {

// 找到后直接break,并由当前provider来进行校验工作

result = provider.authenticate(authentication);

if(result != null) {

this.copyDetails(authentication, result);

break;

}

} catch (AccountStatusException var11) {

this.prepareException(var11, authentication);

throw var11;

} catch (InternalAuthenticationServiceException var12) {

this.prepareException(var12, authentication);

throw var12;

} catch (AuthenticationException var13) {

lastException = var13;

}

}

}

// 若没有一个支持,则尝试交给父类来执行

if(result == null && this.parent != null) {

try {

result = this.parent.authenticate(authentication);

} catch (ProviderNotFoundException var9) {

;

} catch (AuthenticationEx

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值