过滤器:UsernamePasswordAuthenticationFilter继承 AbstractAuthenticationProcessingFilter(抽象类)
public class UsernamePasswordAuthenticationFilter extends
AbstractAuthenticationProcessingFilter {
}
核心方法,返回Authentication接口,里面有getAuthorities,getCredentials,getDetails,getPrincipal,isAuthenticated,setAuthenticated等方法

先执行attemptAuthentication方法。这个方法会从request中读出用户名、密码,组成UsernamePasswordAuthenticationToken对象,调用父类this.getAuthenticationManager().authenticate(authRequest)方法返回Authentication对象,UsernamePasswordAuthenticationToken是实现了Authentication。然后将Authentication对象传给AuthenticationManager进行登录认证。AuthenticationManager的认证过程是springsecurity认证的核心。大致记录下。
AuthenticationManager(接口)实际是ProviderManager(类),ProviderManager实现了AuthenticationManager接口。将认证的工作交给AuthenticationProvider(多个),AuthenticationManager都会包含多个AuthenticationProvider对象,有任何一个AuthenticationProvider验证通过,都属于认证通过。AuthenticationManager和AuthenticationProvider在哪里来的呢?在spring-security.xml的配置中来的
authenticate(authRequest)实现类ProviderManager

ProviderManager类核心方法循环遍历所有provider,有一个成功即认为通过

provider.authenticate(authentication)接口 AuthenticationProvider

实现类public abstract class AbstractUserDetailsAuthenticationProvider implements
AuthenticationProvider, InitializingBean, MessageSourceAware {}


UserDetails retrieveUser(String username,
UsernamePasswordAuthenticationToken authentication) 这个抽象方法实现类

核心方法

我们可以自定义userService 去重写loadByUsername(String username)方法

如果要自定义登录认证,如第三方登录后,用springsecurity接管,就需要增加类似于UsernamePasswordAuthenticationFilter的过滤器
本文详细介绍了Spring Security的登录认证过程,从UsernamePasswordAuthenticationFilter到AuthenticationManager,再到AuthenticationProvider的认证逻辑。重点讨论了Authentication对象的创建、自定义UserService以及如何扩展实现第三方登录的集成。
3436

被折叠的 条评论
为什么被折叠?



