gpt4 book ai didi

java - 何时使用不同的 SecurityContextHolder 策略?

转载 作者:太空宇宙 更新时间:2023-11-04 09:45:57 24 4
gpt4 key购买 nike

我正在阅读 Spring Security 文档,它说 SecurityContextHolder 为具有特定线程行为的应用程序提供了不同类型的策略。例如 - Swing 应用程序。

我知道在网络应用程序中我们可以使用 ThredLocal 策略。但何时使用其他两种策略以及它们如何发挥作用我无法理解。

最佳答案

参见Spring Security Reference :

SecurityContextHolder, SecurityContext and Authentication Objects

The most fundamental object is SecurityContextHolder. This is where we store details of the present security context of the application, which includes details of the principal currently using the application. By default the SecurityContextHolder uses a ThreadLocal to store these details, which means that the security context is always available to methods in the same thread of execution, even if the security context is not explicitly passed around as an argument to those methods. Using a ThreadLocal in this way is quite safe if care is taken to clear the thread after the present principal’s request is processed. Of course, Spring Security takes care of this for you automatically so there is no need to worry about it.

Some applications aren’t entirely suitable for using a ThreadLocal, because of the specific way they work with threads. For example, a Swing client might want all threads in a Java Virtual Machine to use the same security context. SecurityContextHolder can be configured with a strategy on startup to specify how you would like the context to be stored. For a standalone application you would use the SecurityContextHolder.MODE_GLOBAL strategy. Other applications might want to have threads spawned by the secure thread also assume the same security identity. This is achieved by using SecurityContextHolder.MODE_INHERITABLETHREADLOCAL. You can change the mode from the default SecurityContextHolder.MODE_THREADLOCAL in two ways. The first is to set a system property, the second is to call a static method on SecurityContextHolder. Most applications won’t need to change from the default, but if you do, take a look at the JavaDoc for SecurityContextHolder to learn more.

关于java - 何时使用不同的 SecurityContextHolder 策略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55423919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com