赞
踩
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"));
- }
- }

其中数据库加载用户信息是从spring缓存当中进行加载,如果缓存当中没有,再从数据库加载。
参考文章
Spring Security到底在哪里进行密码方式认证
SpringSecurity是如何实现账号密码的验证登录的
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。