SecurityContextHolder中有哪些方法
时间: 2024-04-17 16:11:18 浏览: 19
SecurityContextHolder中有以下方法:
1. getContext():返回当前线程的安全上下文。
2. clearContext():清除当前线程的安全上下文。
3. setContext():设置当前线程的安全上下文。
4. createEmptyContext():创建一个空的安全上下文。
相关问题
SecurityContextHolder
SecurityContextHolder是Spring Security框架的一个组件,它是一个工具类,提供了一些静态方法,用于保存应用程序中当前用户的安全上下文。
通过使用SecurityContextHolder.getContext()方法,可以获取到当前线程的安全上下文。这意味着,即使在不同的方法之间不以参数的形式传递安全上下文,只要在同一个线程中执行针对某个用户的逻辑,这些方法仍然可以通过SecurityContextHolder工具类获取到该用户的安全上下文。
在请求处理的过程中,一个安全上下文会在处理该请求的线程中被创建并记录到SecurityContextHolder中。而当请求处理结束时,SecurityContextPersistenceFilter会销毁SecurityContextHolder中的安全上下文。因此,每个请求都有自己独立的安全上下文,可以通过SecurityContextHolder.getContext()方法获取到。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [SecurityContextHolder](https://blog.csdn.net/weixin_57128596/article/details/126844394)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
SecurityContextHolder.getContext
SecurityContextHolder.getContext() 是 Spring Security 框架中的一个静态方法,用于获取当前线程的安全上下文对象。
安全上下文对象包含了当前用户的身份认证信息(Authentication)以及授权信息(Authorization)。在 Spring Security 中,它被用来存储和访问当前用户的认证和授权相关信息。
通过调用 SecurityContextHolder.getContext() 方法,可以获取到当前线程的安全上下文对象,然后通过该对象获取当前用户的认证信息、权限信息等。
需要注意的是,SecurityContextHolder 是线程绑定的,因此在不同的线程中调用 SecurityContextHolder.getContext() 方法会返回不同的安全上下文对象。在多线程环境中使用时要注意线程安全性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)