上一篇里边和大家分享了spring security架构,也就是filter链。大家也许会问,spring不是一直自诩开箱即用吗?那怎么实现最简单的登录认证呢?难道还需要我自己写个filter,再写个登录校验的复杂模块?当然不是,下面给大家分享一下spring security在认证这一块的设计和实现。
目录
一、认证上下文
首先,spring security认证上下文这一块的设计如下:
其中,SecurityContextHolder是上下文线程持有者,SecurityContex是SecurityContextHolder持有的上下文,SecurityContext中包含当前认证对象Authentication,Authentication又由三部分组成:Principal,Credentials,Authorities。Principal是用户UserDetails实例,Credentials代表证书(密码),Authorities是用户的授权(角色)。
这个代表什么意思呢?这个模块设计说明spring security是通过SecurityContextHolder实现最终的认证效果的,通俗的说,就是通过这个对象存储和管理认证用户,那也就是说,如果想实现用户登录认证,最终的结果,也就是改变SecurityContextHolder中的认证值,就能实现登录的效果。不过这种情况一般会在三方登录的时候使用,比如说微信登录,回调你的本地地址以后,如果校验参数都没有问题,也没有攻击登录,那么就直接通过SecurityContextHolder的值,就能达到用户登录的效果,大概如下:
SecurityContext context = SecurityContextHolder.createEmptyContext();
Authentication authentication =
new TestingAuthenticationToken("username", "password", "ROLE_USER");
context.setAuthentication(authentication);
SecurityContextHolder.setContext(context);
这样设置以后,你指定的用户、密码、角色就会存储在当前线程的ThreadLocal中,说白了,也就是session里,便达到了登录目的。因为spring security判断用户是否登录就是通过SecurityContex实现的。有兴趣的可以试试。
反过来,你也可以通过认证上下文获取已经认证过的当前用户的相关信息,如下:
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
String username = authentication.getName();
Object principal = authentication.getPrincipal();
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
二、认证管理设计
上文我们了解到了spring security的上下文设计,也就是最终的认证结果实现,那spring security是怎么去管理这些用户的上下文的,认证的过程是怎么管理的,肯定不是手工一个一个硬编码在代码当中,那么这个时候就需要看下Spring security在这块的设计图(官网):
我们大概解释一下。从图中可以大概的看到,一个web请求进来,首先就是通过我们上一篇里说到的filter chain,然后在authfilter中,调用Authentication,实现请求的认证。那么校验如何做,spring security做了任务分工分层设计。首先由AuthenticationManager总体做认证的管理,是个大总管。当然它不是自己做,自己只是一个抽象接口,而是由自己的实现类认证提供者ProviderManager来做。AuthenticationManager如下:
public interface AuthenticationManager {
Authentication authenticate(Authentication authentication)
throws AuthenticationException;
}
可以看出, AuthenticationManager接入一个未认证的Authentication,然后通过具体的认证验证,得到一个认证后的Authentication。入参是通过request得到的,出参是最终的结果,通过与否是通过Authentication里的的认证标识来区别的。然后,认证提供管理者ProviderManager如下:
public class ProviderManager implements AuthenticationManager, MessageSourceAware,
InitializingBean {
// ~ Static fields/initializers
// =====================================================================================
private static final Log logger = LogFactory.getLog(ProviderManager.class);
// ~ Instance fields
// ================================================================================================
private AuthenticationEventPublisher eventPublisher = new NullEventPublisher();
private List<AuthenticationProvider> providers = Collections.emptyList();
protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();
private AuthenticationManager parent;
private boolean eraseCredentialsAfterAuthentication = true;
/**
* Construct a {@link ProviderManager} using the given {@link AuthenticationProvider}s
*
* @param providers the {@link AuthenticationProvider}s to use
*/
public ProviderManager(AuthenticationProvider... providers) {
this(Arrays.asList(providers), null);
}
/**
* Construct a {@link ProviderManager} using the given {@link AuthenticationProvider}s
*
* @param providers the {@link AuthenticationProvider}s to use
*/
public ProviderManager(List<AuthenticationProvider> providers) {
this(providers, null);
}
/**
* Construct a {@link ProviderManager} using the provided parameters
*
* @param providers the {@link AuthenticationProvider}s to use
* @param paren