当前位置: 首页 > news >正文

【第二篇】SpringSecurity源码详解

一、SpringSecurity中的核心组件

  在SpringSecurity中的jar分为4个,作用分别为

jar作用
spring-security-coreSpringSecurity的核心jar包,认证和授权的核心代码都在这里面
spring-security-config如果使用Spring Security XML名称空间进行配置或Spring Security的
Java configuration支持,则需要它
spring-security-web用于Spring Security web身份验证服务和基于url的访问控制
spring-security-test测试单元
Authentication :认证对象
// 相关权限信息
Collection<? extends GrantedAuthority> getAuthorities();
// 获取当前用户的凭证
Object getCredentials();
// 获取当前用户的详情
Object getDetails();
// 当前登录的用户对象
Object getPrincipal();
// 判断当前用户的登录状态:true:登录 false:未登录
boolean isAuthenticated();
// 更新用户的认证状态
void setAuthenticated(boolean var1) throws IllegalArgumentException;

Authentication保存在哪了?

​ 在SecurityContextHolder中获取

1.SecurityContextHolder

默认情况下,SecurityContextHolder是通过 ThreadLocal来存储对应的信息的。也就是在一个线程中可以通过这种方式来获取当前登录的用户的相关信息。而在SecurityContext中就只提供了对Authentication对象操作的方法

SecurityContext:容器上下文

// 获取认证对象
Authentication getAuthentication();
// 设置认证对象
void setAuthentication(Authentication var1);

SecurityContextHolder:

// 获取容器上下文
public static SecurityContext getContext()
// 设置容器上下文
public static void setContext(SecurityContext context)

从而可以这样获取到当前登录人信息

Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserDetails userDetails = (UserDetails) principal;
System.out.println("=========="+userDetails.getUsername());

SecurityContextHolder中可以设置自定义的存储方式

public static final String SYSTEM_PROPERTY = "spring.security.strategy";
private static String strategyName = System.getProperty("spring.security.strategy");
private static SecurityContextHolderStrategy strategy;// 源码,初始化静态方法
private static void initialize() {if (!StringUtils.hasText(strategyName)) {strategyName = "MODE_THREADLOCAL";}// 3种默认的存储方式if (strategyName.equals("MODE_THREADLOCAL")) {// 本地线程中strategy = new ThreadLocalSecurityContextHolderStrategy();} else if (strategyName.equals("MODE_INHERITABLETHREADLOCAL")) {// 父子线程中,可继承strategy = new InheritableThreadLocalSecurityContextHolderStrategy();} else if (strategyName.equals("MODE_GLOBAL")) {// 全局strategy = new GlobalSecurityContextHolderStrategy();} else {try {Class<?> clazz = Class.forName(strategyName);
http://www.lryc.cn/news/365655.html

相关文章:

  • 基于Python+FFMPEG环境下载B站歌曲
  • 静态 VxLAN 浅析及配置示例(头端复制)
  • 2023年与2024年AI代理基础设施的演进:六大关键变化
  • 实验三-8086指令的应用《计算机组成原理》
  • 《维汉翻译通》App全新升级:维吾尔语短文本翻译、汉语拼音标注、维语词典、谚语格言名句等功能统统免费!还支持维吾尔文OCR识别提取文字!
  • 全年申报!2024年陕西省双软企业认定条件标准、申报好处费用
  • 系统移植 (以将Linux系统移植到S5P6818开发板为例)
  • 超长正整数的加法
  • C++ - 查找算法 和 其他 算法
  • 字符串的信号(SIGNAL)和槽(SLOT)的宏连接方式弊端
  • Kali linux学习入门
  • selenium中,怎么判断是否已选多选框
  • WindowManager相关容器类
  • 零售行业运营有哪些业务场景?详解各业务场景的分析指标和维度
  • 无锡哲讯携手SAP,赋能装备制造业数字化转型
  • TPM仿真环境搭建
  • 提高篇(五):使用Processing创作互动艺术:从灵感到实现
  • 华为od-C卷100分题目-3用连续自然数之和来表达整数
  • Chrome 自动执行 JS 脚本 | Tampermonkey 插件
  • ffmplay 源码解读
  • java web如何调用py脚本文件
  • K8s:无状态
  • Docker 入门篇(九)-- 使用 Maven 插件 构建 Docker 镜像
  • 网络协议三
  • LeetCode LRU缓存
  • Parallels Desktop for Mac 19.4.0更新了哪些内容?有什么改进?
  • Python 将CSV文件转为PDF文件
  • 4_XMR交易过程
  • 02_共享锁和排他锁
  • Ubuntu的启动过程