国内知名
计算机技术平台

SpringSecurity是如何验证用户的?

  1. 首先调用login接口,传入登录用户名和密码;
  2. LoginController:
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(用户名, 密码);
Authentication authentication = authenticationManager.authenticate(usernamePasswordAuthenticationToken);
  1. 上述第二句调用WebSecurityConfigurerAdapter的authenticate方法,执行到return this.delegate.authenticate(authentication),this.delegate即AuthenticationManager接口,ProviderManager实现了该接口(authenticate);
  2. 跳转到ProviderManager继续执行,result = provider.authenticate(authentication)调用AbstractUserDetailsAuthenticationProvider的authenticate方法;
  3. AbstractUserDetailsAuthenticationProvider的authenticate执行user = retrieveUser(username, (UsernamePasswordAuthenticationToken) authentication);
  4. 进入到DaoAuthenticationProvider的retrieveUser方法,执行UserDetails loadedUser = this.getUserDetailsService().loadUserByUsername(username);
  5. 此时会调用我们自定义的UserDetailsService中的loadUserByUsername方法从数据库获取用户信息,并写入UserDetails中;

实际的密码校验逻辑是在类DaoAuthenticationProvider的additionalAuthenticationChecks中

@Override
@SuppressWarnings("deprecation")
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"));
	}
	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"));
	}
}

处理登录异常是在这里跳转(类ExceptionTranslationFilter):

private void handleSpringSecurityException(HttpServletRequest request, HttpServletResponse response, FilterChain chain, RuntimeException exception) throws IOException, ServletException {
	if (exception instanceof AuthenticationException) {
		handleAuthenticationException(request, response, chain, (AuthenticationException) exception);
	}else if (exception instanceof AccessDeniedException) {
		handleAccessDeniedException(request, response, chain, (AccessDeniedException) exception);
	}
}
赞(0) 打赏
未经允许不得转载:东云网 » SpringSecurity是如何验证用户的?

评论 抢沙发

东云IT,值得信赖

联系我们关于我们

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

微信扫一扫

支付宝扫一扫