SpringSecurity用户密码验证过程

SpringSecurity中的UsernamePasswordAuthenticationFilter处理登录密码验证,基于AbstractAuthenticationProcessingFilter和AuthenticationManager。AuthenticationManager的子类ProviderManager遍历AuthenticationProvider,使用DaoAuthenticationProvider进行实际验证,通过AbstractUserDetailsAuthenticationProvider和passwordEncoder对比加密后的密码。用户信息从Spring缓存或数据库加载。
部署运行你感兴趣的模型镜像
SpringSecurity过滤链当中的UsernamePasswordAuthenticationFilter负责登陆密码验证
  • AbstractAuthenticationProcessingFilter是UsernamePasswordAuthenticationFilter的父接口,接口当中就有一个this.getAuthenticationManager()方法,获取AuthenticationManager对象。
  • AuthenticationManager对象当中有一个方法:Authentication authenticate(Authentication authentication) ,传入authentication对象用来密码验证。
  • AuthenticationManager的子类ProviderManager实现了该方法,可能有多种验证方式,因此遍历一个集合,有一个满足就可以,代码如下
@Override
	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
		Class<? extends Authentication> toTest = authentication.getClass();
		//判断使用哪种验证方式
			if (!provider.supports(toTest)) {
				continue;
			}
			...
			try {
			//进行真正认证的地方
				result = provider.authenticate(authentication);
				if (result != null) {
					copyDetails(authentication, result);
					break;
				}
			}
			return result;
	}
  • provider.authenticate(authentication); 其中provider是AuthenticationProvider的一个子类的实现类,如果用的是用户名密码,就是AbstractUserDetailsAuthenticationProvider抽象类,该抽象类当中有一子类,DaoAuthenticationProvider,真正负责的密码验证。
  • AbstractUserDetailsAuthenticationProvider中的public Authentication authenticate(Authentication authentication)方法吗,调用
protected void additionalAuthenticationChecks(UserDetails userDetails,
			UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
		if (authentication.getCredentials() == null) {
			this.logger.debug("Failed to authenticate since no credentials provided");
			throw new BadCredentialsException(this.messages
					.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
		}
		//presentedPassword 用户登陆信息
		//userDetails.getPassword()数据库根据用户名的得到的信息,判断是否相同
		String presentedPassword = authentication.getCredentials().toString();
		if (!this.passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {
			this.logger.debug("Failed to authenticate since password does not match stored value");
			throw new BadCredentialsException(this.messages
					.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
		}
	}
  • passwordEncoder.matches 把presentedPassword加密之后与原来的结果进行比较,返回结果

其中数据库加载用户信息是从spring缓存当中进行加载,如果缓存当中没有,再从数据库加载。

参考文章
Spring Security到底在哪里进行密码方式认证
SpringSecurity是如何实现账号密码的验证登录的

您可能感兴趣的与本文相关的镜像

AutoGPT

AutoGPT

AI应用

AutoGPT于2023年3月30日由游戏公司Significant Gravitas Ltd.的创始人Toran Bruce Richards发布,AutoGPT是一个AI agent(智能体),也是开源的应用程序,结合了GPT-4和GPT-3.5技术,给定自然语言的目标,它将尝试通过将其分解成子任务,并在自动循环中使用互联网和其他工具来实现这一目标

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值