Class SecurityContextHolder
- java.lang.Object
-
- org.springframework.security.core.context.SecurityContextHolder
-
public class SecurityContextHolder extends java.lang.ObjectAssociates a givenSecurityContextwith the current execution thread.This class provides a series of static methods that delegate to an instance of
SecurityContextHolderStrategy. The purpose of the class is to provide a convenient way to specify the strategy that should be used for a given JVM. This is a JVM-wide setting, since everything in this class isstaticto facilitate ease of use in calling code.To specify which strategy should be used, you must provide a mode setting. A mode setting is one of the three valid
MODE_settings defined asstatic finalfields, or a fully qualified classname to a concrete implementation ofSecurityContextHolderStrategythat provides a public no-argument constructor.There are two ways to specify the desired strategy mode
String. The first is to specify it via the system property keyed onSYSTEM_PROPERTY. The second is to callsetStrategyName(String)before using the class. If neither approach is used, the class will default to usingMODE_THREADLOCAL, which is backwards compatible, has fewer JVM incompatibilities and is appropriate on servers (whereasMODE_GLOBALis definitely inappropriate for server use).
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringMODE_GLOBALstatic java.lang.StringMODE_INHERITABLETHREADLOCALstatic java.lang.StringMODE_THREADLOCALstatic java.lang.StringSYSTEM_PROPERTY
-
Constructor Summary
Constructors Constructor Description SecurityContextHolder()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidclearContext()Explicitly clears the context value from the current thread.static SecurityContextcreateEmptyContext()Delegates the creation of a new, empty context to the configured strategy.static SecurityContextgetContext()Obtain the currentSecurityContext.static SecurityContextHolderStrategygetContextHolderStrategy()Allows retrieval of the context strategy.static intgetInitializeCount()Primarily for troubleshooting purposes, this method shows how many times the class has re-initialized itsSecurityContextHolderStrategy.static voidsetContext(SecurityContext context)Associates a newSecurityContextwith the current thread of execution.static voidsetStrategyName(java.lang.String strategyName)Changes the preferred strategy.java.lang.StringtoString()
-
-
-
Field Detail
-
MODE_THREADLOCAL
public static final java.lang.String MODE_THREADLOCAL
- See Also:
- Constant Field Values
-
MODE_INHERITABLETHREADLOCAL
public static final java.lang.String MODE_INHERITABLETHREADLOCAL
- See Also:
- Constant Field Values
-
MODE_GLOBAL
public static final java.lang.String MODE_GLOBAL
- See Also:
- Constant Field Values
-
SYSTEM_PROPERTY
public static final java.lang.String SYSTEM_PROPERTY
- See Also:
- Constant Field Values
-
-
Method Detail
-
clearContext
public static void clearContext()
Explicitly clears the context value from the current thread.
-
getContext
public static SecurityContext getContext()
Obtain the currentSecurityContext.- Returns:
- the security context (never
null)
-
getInitializeCount
public static int getInitializeCount()
Primarily for troubleshooting purposes, this method shows how many times the class has re-initialized itsSecurityContextHolderStrategy.- Returns:
- the count (should be one unless you've called
setStrategyName(String)to switch to an alternate strategy.
-
setContext
public static void setContext(SecurityContext context)
Associates a newSecurityContextwith the current thread of execution.- Parameters:
context- the newSecurityContext(may not benull)
-
setStrategyName
public static void setStrategyName(java.lang.String strategyName)
Changes the preferred strategy. Do NOT call this method more than once for a given JVM, as it will re-initialize the strategy and adversely affect any existing threads using the old strategy.- Parameters:
strategyName- the fully qualified class name of the strategy that should be used.
-
getContextHolderStrategy
public static SecurityContextHolderStrategy getContextHolderStrategy()
Allows retrieval of the context strategy. See SEC-1188.- Returns:
- the configured strategy for storing the security context.
-
createEmptyContext
public static SecurityContext createEmptyContext()
Delegates the creation of a new, empty context to the configured strategy.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-