地 址:联系地址联系地址联系地址
电 话:020-123456789
网址:y.y.99t.twqueen.com
邮 箱:admin@aa.com
目录
一些权限框架一般都包含认证器和决策器,前者处理登陆验证,器实后者处理访问资源的认证控制
Spring Security的登陆请求处理如图
下面来分析一下是怎么实现认证器的
首先登陆请求会被UsernamePasswordAuthenticationFilter拦截 ,这个过滤器看名字就知道是器实一个拦截用户名密码的拦截器
主要的验证是在attemptAuthentication()方法里 ,他会去获取在请求中的认证用户名密码 ,并且创建一个该用户的器实上下文,然后在去执行一个验证过程
String username = this.obtainUsername(request);nString password = this.obtainPassword(request);n//创建上下文nUsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username,认证 password);nthis.setDetails(request, authRequest);nreturn this.getAuthenticationManager().authenticate(authRequest);n
可以看看UsernamePasswordAuthenticationToken这个类 ,他是器实继承了AbstractAuthenticationToken,然后这个父类实现了Authentication
由这个类的方法和属性可得知他就是存储用户验证信息的 ,认证器的器实主要功能应该就是验证完成后填充这个类
回到UsernamePasswordAuthenticationToken中 ,在上面创建的认证过程了可以发现
public UsernamePasswordAuthenticationToken(Object principal,Object credentials){ n super(null);n this.principal=principal;n this.credentials=credentials;n //还没认证n setAuthenticated(false);n}n
还有一个super(null)的处理,因为刚进来是器实还不知道有什么权限的 ,设置null是认证初始化一个空的权限
//权限利集合nprivate final Collection<GrantedAuthority> authorities;n//空的集合npublic static final List<GrantedAuthority> NO_AUTHORITIES = Collections.emptyList();n//初始化nif (authorities == null) { n this.authorities = AuthorityUtils.NO_AUTHORITIES;n return;n}n
那么后续认证完还会把权限设置尽量,此时可以看UsernamePasswordAuthenticationToken的器实另一个重载构造器
//认证完成npublic UsernamePasswordAuthenticationToken(Object principal, Object credentials,n Collection<? extends GrantedAuthority> authorities) { n super(authorities);n this.principal = principal;n this.credentials = credentials;n super.setAuthenticated(true); // must use super, as we overriden}n
在看源码的过程中,注释一直在强调这些上下文的认证填充和设置都应该是由AuthenticationManager或者AuthenticationProvider的实现类去操作
接下来会把球踢给AuthenticationManager ,但他只是个接口
/**n * Attempts to authenticate the passed { @link Authentication} object, returning an * fully populated <code>Authentication</code> object (including granted authorities)n * if successful.n **/npublic interface AuthenticationManager { n Authentication authenticate(Authentication authentication)n throws AuthenticationException;n}n
注释也写的很清楚了 ,认证完成后会填充Authentication
接下来会委托给ProviderManager,因为他实现了AuthenticationManager
刚进来看authenticate()方法会发现他先遍历了一个List<AuthenticationProvider>集合
/**n * Indicates a class can process a specific Authentication n **/npublic interface AuthenticationProvider { n Authentication authenticate(Authentication authentication)n throws AuthenticationException;n //支不支持特定类型的authenticationn boolean supports(Class<?> authentication);n}n
实现这个类就可以处理不同类型的Authentication ,比如上边的UsernamePasswordAuthenticationToken ,对应的处理类是AbstractUserDetailsAuthenticationProvider,为啥知道呢 ,因为在这个supports()里
public boolean supports(Class<?> authentication) { nttreturn (UsernamePasswordAuthenticationToken.classntttt.isAssignableFrom(authentication));n}n
注意到这个是抽象类,实际的处理方法是在他的子类DaoAuthenticationProvider里 ,但是最重要的authenticate()方法子类好像没有继承 ,看看父类是怎么实现这个方法的
由上述流程来看 ,Security的检验过程还是比较清晰的 ,通过AuthenticationManager来委托给ProviderManager ,在通过具体的实现类来处理请求 ,在这个过程中,将查询用户的实现和验证代码分离开来
整个过程看着像是策略模式,后边将变化的部分抽离出来,实现解耦
前边提到的认证成功会调用createSuccessAuthentication()方法 ,里边的内容很简单
UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(n principal, authentication.getCredentials(),n authoritiesMapper.mapAuthorities(user.getAuthorities()));n result.setDetails(authentication.getDetails());n
public UsernamePasswordAuthenticationToken(Object principal, Object credentials,n Collection<? extends GrantedAuthority> authorities) { n super(authorities);n this.principal = principal;n this.credentials = credentials;n super.setAuthenticated(true); // must use super, as we overriden }n
这次往supe里放了权限集合 ,父类的处理是判断里边的权限有没有空的,没有则转换为只读集合
for (GrantedAuthority a : authorities) { n if (a == null) { n throw new IllegalArgumentException(n "Authorities collection cannot contain any null elements");n }n}nArrayList<GrantedAuthority> temp = new ArrayList<>(nauthorities.size());ntemp.addAll(authorities);nthis.authorities = Collections.unmodifiableList(temp);n
回到ProviderManager里的authenticate方法,当我们终于从
result = provider.authenticate(authentication);n
走出来时,后边还有什么操作
if (result != null) { n tcopyDetails(authentication, result);n tbreak;n }n
最后返回到UsernamePasswordAuthenticationFilter中通过过滤
这就是Spring Security实现认证的过程了
通过实现自己的上下文Authentication和处理类AuthenticationProvider以及具体的查询用户的方法就可以自定义自己的登陆实现
具体可以看Spring Security自定义认证器
原文链接 :https://www.cnblogs.com/aruo/p/16306421.html#%E9%AA%8C%E8%AF%81%E8%BF%87%E7%A8%8B