核心组件

AuthenticationManager,ProviderManager和AuthenticationProvider

AuthenticationManager只是一个接口,实际中是如何运作的?如果我们需要检查多个身份验证数据库或不同身份验证服务(如数据库和LDAP服务器)的组合,该怎么办?

Spring Security中的默认实现为ProviderManager,ProviderManager本身不处理身份验证请求,它会委托给AuthenticationProvider列表进行处理,每个列表都会被查询以查看它是否可以执行认证。每个提供程序将抛出异​​常或返回完全填充的Authentication对象。

org.springframework.security.authentication.ProviderManager内部实现:

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
遍历List<AuthenticationProvider> 列表{
if(provider.supports(toTest)){
result = provider.authenticate(authentication);
this.copyDetails(authentication, result);
}
}
this.parent.authenticate(authentication);
eventPublisher.publishAuthenticationSuccess(result);
}

还记得我们的好朋友UserDetails和UserDetailsService吗?验证身份验证请求的最常用方法是加载相应的UserDetails并检查加载的密码与用户输入的密码。这是DaoAuthenticationProvider使用的方法(见下文)。加载的UserDetails对象 - 特别是它包含的GrantedAuthority - 将在构建完全填充的Authentication对象时使用,该对象从成功的身份验证返回并存储在SecurityContext中。

DaoAuthenticationProvider继承org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
UserDetails user = this.userCache.getUserFromCache(username);
if(user==null){
user = this.retrieveUser(username, (UsernamePasswordAuthenticationToken)authentication);
}
this.preAuthenticationChecks.check(user);
this.additionalAuthenticationChecks(user, (UsernamePasswordAuthenticationToken)authentication);
this.postAuthenticationChecks.check(user); Object principalToReturn = user.getUsername();
return this.createSuccessAuthentication(principalToReturn, authentication, user);
}

AuthenticationProvider实现类:org.springframework.security.authentication.dao.DaoAuthenticationProvider内部实现

protected final UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
UserDetails loadedUser = this.getUserDetailsService().loadUserByUsername(username);
if (loadedUser == null) {
throw new InternalAuthenticationServiceException("UserDetailsService returned null, which is an interface contract violation");
} else {
return loadedUser;
}
}

UserDetailsS​​ervice实现

In-Memory Authentication

<user-service id="userDetailsService">
<!-- Password以{noop}为前缀,指示将其委托给passwordencoder应该使用NoOpPasswordEncoder。这是不安全的生产,但使阅读样本中更容易。通常密码应该使用BCrypt散列 -->
<user name="jimi" password="{noop}jimispassword" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="bob" password="{noop}bobspassword" authorities="ROLE_USER" />
</user-service>

JdbcDaoImpl

从数据库加载

密码编码

历史的密码编码

  • 存储密码原始文本
  • 存储通过单向散列(如SHA-256)的密码
  • 为了降低Rainbow Tables的有效性,鼓励开发人员使用salted密码
  • 现在鼓励开发人员利用自适应单向函数来存储密码。开发人员来设定不同的worker factor满足不同的安全需要,在安全和效率之间做出权衡。例如: bcrypt , PBKDF2 , scrypt , and Argon2 .

DelegatingPasswordEncoder(spring5.0后的新特性)

  • 确保使用当前密码存储建议对密码进行编码
  • 允许验证现代和传统格式的密码
  • 允许将来升级编码

将编码方式存入密码文本

BCryptPasswordEncoder

https://en.wikipedia.org/wiki/Bcrypt

Pbkdf2PasswordEncoder

https://en.wikipedia.org/wiki/PBKDF2

SCryptPasswordEncoder

https://en.wikipedia.org/wiki/Scrypt

参考

英文版文档

https://docs.spring.io/spring-security/site/docs/5.0.5.RELEASE/reference/htmlsingle/#overall-architecture

中文版文档

https://www.springcloud.cc/spring-security.html

spring security原理-学习笔记2-核心组件的更多相关文章

  1. spring security原理-学习笔记1-整体概览

    整体概述 运行时环境 Spring Security 3.0需要Java 5.0 Runtime Environment或更高版本. 核心组件 SecurityContextHolder,Securi ...

  2. Spring Security Filter 学习笔记

    过滤器可以简单理解成用于拦截请求,并执行相应逻辑的代码. 在Spring Security架构中实现过滤器 在SpringSecurity中,可以通过实现 javax.servlet 包中的 Filt ...

  3. spring security 原理+实战

    疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 架构师成长+面试必备之 高并发基础书籍 [Netty Zookeeper Redis 高并发实战 ] 前言 Crazy ...

  4. Spring源码学习笔记9——构造器注入及其循环依赖

    Spring源码学习笔记9--构造器注入及其循环依赖 一丶前言 前面我们分析了spring基于字段的和基于set方法注入的原理,但是没有分析第二常用的注入方式(构造器注入)(第一常用字段注入),并且在 ...

  5. Spring 源码学习笔记11——Spring事务

    Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...

  6. Spring源码学习笔记12——总结篇,IOC,Bean的生命周期,三大扩展点

    Spring源码学习笔记12--总结篇,IOC,Bean的生命周期,三大扩展点 参考了Spring 官网文档 https://docs.spring.io/spring-framework/docs/ ...

  7. Find security bugs学习笔记V1.0

    Find security bugs学习笔记V1.0 http://www.docin.com/p-779309481.html

  8. spring cloud(学习笔记)高可用注册中心(Eureka)的实现(二)

    绪论 前几天我用一种方式实现了spring cloud的高可用,达到两个注册中心,详情见spring cloud(学习笔记)高可用注册中心(Eureka)的实现(一),今天我意外发现,注册中心可以无限 ...

  9. Spring 源码学习笔记10——Spring AOP

    Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...

随机推荐

  1. 11 (OC)* CoreImage

    coreimage framework 组成 apple 已经帮我们把image的处理分类好,来看看它的结构: 主要分为三部分: 1)定义部分:CoreImage 何CoreImageDefines. ...

  2. Kubernetes学习之应用部署变迁

    从物理单机.虚拟化(容器化)到云原生 历史 云原生 ---初期 总结

  3. elasticsearch深度分页问题

    elasticsearch专栏:https://www.cnblogs.com/hello-shf/category/1550315.html 一.深度分页方式from + size es 默认采用的 ...

  4. Python学习笔记整理总结【Memcache & Redis(基础+主从架构)】

    一.Memcached1.简介Memcached 是一个高性能的分布式内存对象缓存系统,一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度.提高可扩展性.用来存储 ...

  5. 读《深入理解Elasticsearch》点滴-过滤器

    1.过滤器不影响文档得分 2.过滤的唯一目的是用特定筛选条件来缩小结果范围:而查询不仅缩小结果范围,还会影响文档的得分 3.过滤器运行更加高效(因为不用计算得分) 4.通常过滤器使用Bits接口,返回 ...

  6. .Net Core中间件和过滤器实现错误日志记录

    1.中间件的概念 ASP.NET Core的处理流程是一个管道,中间件是组装到应用程序管道中用来处理请求和响应的组件. 每个中间件可以: 选择是否将请求传递给管道中的下一个组件. 可以在调用管道中的下 ...

  7. 主流视觉SLAM、激光SLAM总结

    SLAM预备知识 SLAM for Dummies 全文总结 视觉里程计 卡尔曼滤波推导 MonoSLAM MonoSLAM:Real-Time Single Camera SLAM全文总结 PTAM ...

  8. Shell之Glob和RE的区别

    目录 Shell之Glob和RE的区别 参考 Glob RE Shell之Glob和RE的区别

  9. 01、JDBC连接

    ① 下载JDBC-MySQL数据库驱动 链接:https://pan.baidu.com/s/1KtSZZ5hXlxu2QrmsXg3lkA 提取码:1pbl ② 加载JDBC-MySQL数据库驱动 ...

  10. python 虚拟环境配置

    刚学习 python 的同学经常会遇到一个问题: 已经安装了特定的包或者第三库,但是 pycharm 总是提示没有找到.