Class ListeningSecurityContextHolderStrategy
- java.lang.Object
-
- org.springframework.security.core.context.ListeningSecurityContextHolderStrategy
-
- All Implemented Interfaces:
SecurityContextHolderStrategy
public final class ListeningSecurityContextHolderStrategy extends java.lang.Object implements SecurityContextHolderStrategy
An API for notifying when theSecurityContextchanges. Note that this does not notify when the underlying authentication changes. To get notified about authentication changes, ensure that you are usingsetContext(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 existingSecurityContextHolder, 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 theSecurityContextHolderis now part of the static context and as such will not get garbage collected. To remove the reference,reset the strategylike so:SecurityContextHolder.setContextHolderStrategy(original);
This will then allowYourListenerand its members to be garbage collected.- Since:
- 5.6
-
-
Constructor Summary
Constructors Constructor Description ListeningSecurityContextHolderStrategy(java.util.Collection<SecurityContextChangedListener> listeners)Construct aListeningSecurityContextHolderStrategybased onThreadLocalSecurityContextHolderStrategyListeningSecurityContextHolderStrategy(SecurityContextChangedListener... listeners)Construct aListeningSecurityContextHolderStrategybased onThreadLocalSecurityContextHolderStrategyListeningSecurityContextHolderStrategy(SecurityContextHolderStrategy delegate, java.util.Collection<SecurityContextChangedListener> listeners)Construct aListeningSecurityContextHolderStrategyListeningSecurityContextHolderStrategy(SecurityContextHolderStrategy delegate, SecurityContextChangedListener... listeners)Construct aListeningSecurityContextHolderStrategy
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclearContext()Clears the current context.SecurityContextcreateEmptyContext()Creates a new, empty context implementation, for use by SecurityContextRepository implementations, when creating a new context for the first time.SecurityContextgetContext()Obtains the current context.voidsetContext(SecurityContext context)Sets the current context.
-
-
-
Constructor Detail
-
ListeningSecurityContextHolderStrategy
public ListeningSecurityContextHolderStrategy(java.util.Collection<SecurityContextChangedListener> listeners)
Construct aListeningSecurityContextHolderStrategybased onThreadLocalSecurityContextHolderStrategy- Parameters:
listeners- the listeners that should be notified when theSecurityContextissetorcleared- Since:
- 5.7
-
ListeningSecurityContextHolderStrategy
public ListeningSecurityContextHolderStrategy(SecurityContextChangedListener... listeners)
Construct aListeningSecurityContextHolderStrategybased onThreadLocalSecurityContextHolderStrategy- Parameters:
listeners- the listeners that should be notified when theSecurityContextissetorcleared- Since:
- 5.7
-
ListeningSecurityContextHolderStrategy
public ListeningSecurityContextHolderStrategy(SecurityContextHolderStrategy delegate, java.util.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 Detail
-
clearContext
public void clearContext()
Clears the current context.- Specified by:
clearContextin interfaceSecurityContextHolderStrategy
-
getContext
public SecurityContext getContext()
Obtains the current context.- Specified by:
getContextin interfaceSecurityContextHolderStrategy- Returns:
- a context (never
null- create a default implementation if necessary)
-
setContext
public void setContext(SecurityContext context)
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)
-
createEmptyContext
public SecurityContext 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.
-
-