Class ListeningSecurityContextHolderStrategy
java.lang.Object
org.springframework.security.core.context.ListeningSecurityContextHolderStrategy
- All Implemented Interfaces:
SecurityContextHolderStrategy
public final class ListeningSecurityContextHolderStrategy
extends Object
implements SecurityContextHolderStrategy
An API for notifying when the
SecurityContext changes.
Note that this does not notify when the underlying authentication changes. To get
notified about authentication changes, ensure that you are using setContext(org.springframework.security.core.context.SecurityContext)
when changing the authentication like so:
SecurityContext context = SecurityContextHolder.createEmptyContext();
context.setAuthentication(authentication);
SecurityContextHolder.setContext(context);
To add a listener to the existing SecurityContextHolder, you can do:
SecurityContextHolderStrategy original = SecurityContextHolder.getContextHolderStrategy(); SecurityContextChangedListener listener = new YourListener(); SecurityContextHolderStrategy strategy = new ListeningSecurityContextHolderStrategy(original, listener); SecurityContextHolder.setContextHolderStrategy(strategy);NOTE: Any object that you supply to the
SecurityContextHolder is now part of
the static context and as such will not get garbage collected. To remove the reference,
reset the strategy like so:
SecurityContextHolder.setContextHolderStrategy(original);This will then allow
YourListener and its members to be garbage collected.- Since:
- 5.6
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct aListeningSecurityContextHolderStrategybased onThreadLocalSecurityContextHolderStrategyConstruct aListeningSecurityContextHolderStrategybased onThreadLocalSecurityContextHolderStrategyListeningSecurityContextHolderStrategy(SecurityContextHolderStrategy delegate, Collection<SecurityContextChangedListener> listeners) Construct aListeningSecurityContextHolderStrategyListeningSecurityContextHolderStrategy(SecurityContextHolderStrategy delegate, SecurityContextChangedListener... listeners) Construct aListeningSecurityContextHolderStrategy -
Method Summary
Modifier and TypeMethodDescriptionvoidClears the current context.Creates a new, empty context implementation, for use by SecurityContextRepository implementations, when creating a new context for the first time.Obtains the current context.Obtains aSupplierthat returns the current context.voidsetContext(SecurityContext context) Sets the current context.voidsetDeferredContext(Supplier<SecurityContext> deferredContext) Sets aSupplierthat will return the current context.
-
Constructor Details
-
ListeningSecurityContextHolderStrategy
Construct aListeningSecurityContextHolderStrategybased onThreadLocalSecurityContextHolderStrategy- Parameters:
listeners- the listeners that should be notified when theSecurityContextissetorcleared- Since:
- 5.7
-
ListeningSecurityContextHolderStrategy
Construct aListeningSecurityContextHolderStrategybased onThreadLocalSecurityContextHolderStrategy- Parameters:
listeners- the listeners that should be notified when theSecurityContextissetorcleared- Since:
- 5.7
-
ListeningSecurityContextHolderStrategy
public ListeningSecurityContextHolderStrategy(SecurityContextHolderStrategy delegate, Collection<SecurityContextChangedListener> listeners) Construct aListeningSecurityContextHolderStrategy- Parameters:
listeners- the listeners that should be notified when theSecurityContextissetorcleareddelegate- the underlyingSecurityContextHolderStrategy
-
ListeningSecurityContextHolderStrategy
public ListeningSecurityContextHolderStrategy(SecurityContextHolderStrategy delegate, SecurityContextChangedListener... listeners) Construct aListeningSecurityContextHolderStrategy- Parameters:
listeners- the listeners that should be notified when theSecurityContextissetorcleareddelegate- the underlyingSecurityContextHolderStrategy
-
-
Method Details
-
clearContext
public void clearContext()Clears the current context.- Specified by:
clearContextin interfaceSecurityContextHolderStrategy
-
getContext
Obtains the current context.- Specified by:
getContextin interfaceSecurityContextHolderStrategy- Returns:
- a context (never
null- create a default implementation if necessary)
-
getDeferredContext
Obtains aSupplierthat returns the current context.- Specified by:
getDeferredContextin interfaceSecurityContextHolderStrategy- Returns:
- a
Supplierthat returns the current context (nevernull- create a default implementation if necessary)
-
setContext
Sets the current context.- Specified by:
setContextin interfaceSecurityContextHolderStrategy- Parameters:
context- to the new argument (should never benull, although implementations must check ifnullhas been passed and throw anIllegalArgumentExceptionin such cases)
-
setDeferredContext
Sets aSupplierthat will return the current context. Implementations can override the default to avoid invokingSupplier.get().- Specified by:
setDeferredContextin interfaceSecurityContextHolderStrategy- Parameters:
deferredContext- aSupplierthat returns theSecurityContext
-
createEmptyContext
Creates a new, empty context implementation, for use by SecurityContextRepository implementations, when creating a new context for the first time.- Specified by:
createEmptyContextin interfaceSecurityContextHolderStrategy- Returns:
- the empty context.
-