AuthenticationManager 接口

AuthenticationManager 对传入的 Authentication 对象进行身份认证。在认证过程中,传入的 Authentication 参数仅包含用户名、密码等基础属性。认证成功后,返回的 Authentication 对象将会包含完整的属性信息,其中包括用户的角色信息。

主要的作用是协调多个 AuthenticationProvider 来进行身份验证,最主要的实现类是 ProviderManager

AuthenticationProvider

具体执行身份验证,每个实现类负责一种验证方法(例如 用户名密码、OAuth、记住我)

AbstractUserDetailsAuthenticationProvider

又开始讲源码了,值得注意的地方:

DaoAuthenticationProvider

AbstractUserDetailsAuthenticationProvider 目前唯一的实现类,主要是补充了几个抽象方法,例如获取 UserDetails、密码匹配等

比较细节的地方:当找不到用户信息的时候,不会直接返回,而是将其与一个「假密码」进行匹配,避免对于攻击者来说,根据接口响应时间就能判断到底是用户不存在还是密码错误,源码:

	private void mitigateAgainstTimingAttack(UsernamePasswordAuthenticationToken authentication) {
		if (authentication.getCredentials() != null) {
			String presentedPassword = authentication.getCredentials().toString();
			this.passwordEncoder.get().matches(presentedPassword, this.userNotFoundEncodedPassword);
		}
	}

ProviderManager

AuthenticationManager 的一个重要实现类