一些权限框架一般都包含认证器和决策器,前者处理登陆验证,后者处理访问资源的控制

Spring Security的登陆请求处理如图

下面来分析一下是怎么实现认证器的

拦截请求

首先登陆请求会被UsernamePasswordAuthenticationFilter拦截,这个过滤器看名字就知道是一个拦截用户名密码的拦截器

主要的验证是在attemptAuthentication()方法里,他会去获取在请求中的用户名密码,并且创建一个该用户的上下文,然后在去执行一个验证过程

String username = this.obtainUsername(request);
String password = this.obtainPassword(request);
//创建上下文
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
this.setDetails(request, authRequest);
return this.getAuthenticationManager().authenticate(authRequest);

可以看看UsernamePasswordAuthenticationToken这个类,他是继承了AbstractAuthenticationToken,然后这个父类实现了Authentication

由这个类的方法和属性可得知他就是存储用户验证信息的,认证器的主要功能应该就是验证完成后填充这个类

回到UsernamePasswordAuthenticationToken中,在上面创建的过程了可以发现

public UsernamePasswordAuthenticationToken(Object principal,Object credentials){
super(null);
this.principal=principal;
this.credentials=credentials;
//还没认证
setAuthenticated(false);
}

还有一个super(null)的处理,因为刚进来是还不知道有什么权限的,设置null是初始化一个空的权限

//权限利集合
private final Collection<GrantedAuthority> authorities;
//空的集合
public static final List<GrantedAuthority> NO_AUTHORITIES = Collections.emptyList();
//初始化
if (authorities == null) {
this.authorities = AuthorityUtils.NO_AUTHORITIES;
return;
}

那么后续认证完还会把权限设置尽量,此时可以看UsernamePasswordAuthenticationToken的另一个重载构造器

//认证完成
public UsernamePasswordAuthenticationToken(Object principal, Object credentials,
Collection<? extends GrantedAuthority> authorities) {
super(authorities);
this.principal = principal;
this.credentials = credentials;
super.setAuthenticated(true); // must use super, as we override
}

在看源码的过程中,注释一直在强调这些上下文的填充和设置都应该是由AuthenticationManager或者AuthenticationProvider的实现类去操作

验证过程

接下来会把球踢给AuthenticationManager,但他只是个接口

/**
* Attempts to authenticate the passed {@link Authentication} object, returning a
* fully populated <code>Authentication</code> object (including granted authorities)
* if successful.
**/
public interface AuthenticationManager {
Authentication authenticate(Authentication authentication)
throws AuthenticationException;
}

注释也写的很清楚了,认证完成后会填充Authentication

接下来会委托给ProviderManager,因为他实现了AuthenticationManager

刚进来看authenticate()方法会发现他先遍历了一个List<AuthenticationProvider>集合

/**
* Indicates a class can process a specific Authentication
**/
public interface AuthenticationProvider {
Authentication authenticate(Authentication authentication)
throws AuthenticationException;
//支不支持特定类型的authentication
boolean supports(Class<?> authentication);
}

实现这个类就可以处理不同类型的Authentication,比如上边的UsernamePasswordAuthenticationToken,对应的处理类是AbstractUserDetailsAuthenticationProvider,为啥知道呢,因为在这个supports()

public boolean supports(Class<?> authentication) {
return (UsernamePasswordAuthenticationToken.class
.isAssignableFrom(authentication));
}

注意到这个是抽象类,实际的处理方法是在他的子类DaoAuthenticationProvider里,但是最重要的authenticate()方法子类好像没有继承,看看父类是怎么实现这个方法的

  1. 首先是继续判断Authentication是不是特定的类

     Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class, authentication,
    () -> messages.getMessage(
    "AbstractUserDetailsAuthenticationProvider.onlySupports",
    "Only UsernamePasswordAuthenticationToken is supported"));
  2. 查询根据用户名用户,这次就是到了子类的方法了,因为这个方法是抽象的

     user=retrieveUser(username,
    (UsernamePasswordAuthenticationToken)authentication);

    接着DaoAuthenticationProvider会调用真正实现查询用户的类UserDetailsService

    UserDetails loadedUser = this.getUserDetailsService().loadUserByUsername(username);

    UserDetailsService这个类信息就不陌生了,我们一般都会去实现这个类来自定义查询用户的方式,查询完后会返回一个UserDetails,当然也可以继承这个类来扩展想要的字段,主要填充的是权限信息和密码

  3. 检验用户,如果获取到的UserDetails是null,则抛异常,不为空则继续校验

    //检验用户合法性
    preAuthenticationChecks.check(user);
    //校验密码
    additionalAuthenticationChecks(user,
    (UsernamePasswordAuthenticationToken) authentication);

    第一个教育是判断用户的合法性,就是判断UserDetails里的几个字段

    //账号是否过期
    boolean isAccountNonExpired();
    //账号被锁定或解锁状态。
    boolean isAccountNonLocked();
    //密码是否过期
    boolean isCredentialsNonExpired();
    //是否启用
    boolean isEnabled();

    第二个则是由子类实现的,判断从数据库获取的密码和请求中的密码是否一致,因为用的登陆方式是根据用户名称登陆,所以有检验密码的步骤

     String presentedPassword = authentication.getCredentials().toString();
    if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {
    logger.debug("Authentication failed: password does not match stored value");
    throw new BadCredentialsException(messages.getMessage(
    "AbstractUserDetailsAuthenticationProvider.badCredentials",
    "Bad credentials"));
    }

    需要主要的是请求中的密码是被加密过的,所以从数据库获取到的密码也应该是被加密的

    注意到当完成校验的时候会把信息放入缓存

    //当没有从缓存中获取到值时,这个字段会被设置成false
    if (!cacheWasUsed) {
    this.userCache.putUserInCache(user);
    }
    //下次进来的时候回去获取
    UserDetails user = this.userCache.getUserFromCache(username);

    如果是从缓存中获取,也是会走检验逻辑的

    最后完成检验,并填充一个完整的Authentication

    return createSuccessAuthentication(principalToReturn, authentication, user);

由上述流程来看,Security的检验过程还是比较清晰的,通过AuthenticationManager来委托给ProviderManager,在通过具体的实现类来处理请求,在这个过程中,将查询用户的实现和验证代码分离开来

整个过程看着像是策略模式,后边将变化的部分抽离出来,实现解耦

返回完整的Authentication

前边提到的认证成功会调用createSuccessAuthentication()方法,里边的内容很简单

UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(
principal, authentication.getCredentials(),
authoritiesMapper.mapAuthorities(user.getAuthorities()));
result.setDetails(authentication.getDetails());
public UsernamePasswordAuthenticationToken(Object principal, Object credentials,
Collection<? extends GrantedAuthority> authorities) {
super(authorities);
this.principal = principal;
this.credentials = credentials;
super.setAuthenticated(true); // must use super, as we override
}

这次往supe里放了权限集合,父类的处理是判断里边的权限有没有空的,没有则转换为只读集合

for (GrantedAuthority a : authorities) {
if (a == null) {
throw new IllegalArgumentException(
"Authorities collection cannot contain any null elements");
}
}
ArrayList<GrantedAuthority> temp = new ArrayList<>(
authorities.size());
temp.addAll(authorities);
this.authorities = Collections.unmodifiableList(temp);

收尾工作

回到ProviderManager里的authenticate方法,当我们终于从

result = provider.authenticate(authentication);

走出来时,后边还有什么操作

  1. 将返回的用户信息负责给当前的上下文
   if (result != null) {
copyDetails(authentication, result);
break;
}
  1. 删除敏感信息

    ((CredentialsContainer) result).eraseCredentials();

    这个过程会将一些字段设置为null,可以实现eraseCredentials()方法来自定义需要删除的信息

最后返回到UsernamePasswordAuthenticationFilter中通过过滤

结论

这就是Spring Security实现认证的过程了

通过实现自己的上下文Authentication和处理类AuthenticationProvider以及具体的查询用户的方法就可以自定义自己的登陆实现

具体可以看Spring Security自定义认证器

Spring Security认证器实现的更多相关文章

  1. Spring Security认证配置(三)

    学习本章之前,可以先了解下上篇Spring Security认证配置(二) 本篇想要达到这样几个目的: 1.登录成功处理 2.登录失败处理 3.调用方自定义登录后处理类型 具体配置代码如下: spri ...

  2. spring security 认证源码跟踪

    spring security 认证源码跟踪 ​ 在跟踪认证源码之前,我们先根据官网说明一下security的内部原理,主要是依据一系列的filter来实现,大家可以根据https://docs.sp ...

  3. Spring Security 入门(1-4-1)Spring Security - 认证过程

    理解时可结合一下这位老兄的文章:http://www.importnew.com/20612.html 1.Spring Security的认证过程 1.1.登录过程 - 如果用户直接访问登录页面 用 ...

  4. Authentication讲解(Spring security认证)

    标准认证过程: 1.用户使用username和password登录 2.系统验证这个password对于该username是正确的 3.假设第二步验证成功,获取该用户的上下文信息(如他的角色列表) 4 ...

  5. Spring Security认证配置(二)

    学习本章之前,可以先了解下上篇Spring Security基本配置. 本篇想要达到这样几个目的: 1.访问调用者服务时,如果是html请求,则跳转到登录页,否则返回401状态码和错误信息 2.调用方 ...

  6. spring security认证

    1 开发基于表单的认证 Spring security核心的功能 认证(你是谁?) 授权(你能干什么?) 攻击防护(防止伪造身份) spring security实现了默认的用户名+密码认证,默认用户 ...

  7. Spring Security 入门(1-4-2)Spring Security - 认证过程之AuthenticationProvider的扩展补充说明

    1.用户信息从数据库获取 通常我们的用户信息都不会向第一节示例中那样简单的写在配置文件中,而是从其它存储位置获取,比如数据库.根据之前的介绍我们知道用户信息是通过 UserDetailsService ...

  8. Spring Security认证配置(一)

    学习本章之前,可以先了解下上篇 Spring Security基本配置. 本篇主要讲述Spring Security基于表单,自定义用户认证配置(上篇中的配置,本篇将不再阐述).一共分为三步: 1.处 ...

  9. Authentication(Spring Security 认证笔记)

    这篇文章是对Spring Security的Authentication模块进行一个初步的概念了解,知道它是如何进行用户认证的 考虑一个大家比较熟悉的标准认证过程: 1.用户使用username和pa ...

随机推荐

  1. 怎么快速找出帝国CMS数据库配置文件路径及迁移网站后修改技巧!

    首先,我们要了解一下帝国CMS整个目录结构,只有了解清楚结构,我们才有可能快速找到自己想要的文件,比如:帝国CMS数据库配置文件路径! 帝国CMS目录结构介绍 / 系统根目录├d/ 附件和数据存放目录 ...

  2. clickhouse智能提示编辑器

    对于经常写sql的人来说智能提示是非常重要的,这个非常影响写sql的效率和心情. 这里说的智能提示不仅仅是关键字(select等)的智能提示,还得要做到表字段的智能提示. 例如: 下面是mysql的智 ...

  3. 1903021116-吉琛- JAVA第二周作业—Java程序编写

    项目 内容 课程班级博客链接 19级信计班 这个作业要求链接 https://www.cnblogs.com/thelovelybugfly/p/9641367.html 我的课程学习目标 1. 学习 ...

  4. Jmeter监控平台搭建:JMeter+InfluxDB+Grafana

    背景 平时一般用Jmeter的Gui模式,添加对应的插件,查看每秒线程数.TPS.响应时间等曲线,其实高并发是不建议这么看的. 解决方案 可以搭配InfluxDB+Grafana工具,使Jmeter异 ...

  5. Spring 源码(5)BeanFactory使用的准备及自定义属性值解析器

    BeanFactory 使用前的准备 上一篇文章 https://www.cnblogs.com/redwinter/p/16165878.html 介绍了自定义标签的使用,完成了AbstractAp ...

  6. 3.1 常用Linux命令

    1.echo命令 echo命令用于在终端设备上输出字符串或变量提取后的值,语法格式为"echo [字符串] [$变量]". 2.date命令 date命令用于显示或设置系统的时间与 ...

  7. rabbitmq 安装延时队列插件rabbitmq-delayed-message-exchange

    1.下载rabbitmq-delayed-message-exchange(注意版本对应) 链接:https://github.com/rabbitmq/rabbitmq-delayed-messag ...

  8. ucore lab4 内核线程管理 学习笔记

    越学越简单,真是越学越简单啊 看视频的时候着实被那复杂的函数调用图吓到了.看代码的时候发现条理还是很清晰的,远没有没想象的那么复杂. 这节创建了俩内核线程,然后运行第一个线程,再由第一个切换到第二个. ...

  9. 使用allatori混淆代码

    引言 为了保密需要,我们开发出来的程序在实际部署之前,需要先将jar包的代码进行混淆,让用户使用反编译也无法获得源码. allatori就是这样一个混淆java jar包代码的工具. 操作步骤 Ste ...

  10. 【代理是什么?】nginx快速入门+反向代理hexo个人博客

    @ 目录 前言 本文说明 请大家务必查看 工作原理 正向代理 反向代理 环境准备 详细版 入门:搭建步骤 配置阿里云epel源: yum安装nginx: 启动nginx: 配置default.conf ...