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"));
}
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是如何实现账号密码的验证登录的
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)