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
ConstructorDescriptionListeningSecurityContextHolderStrategy
(SecurityContextHolderStrategy delegate, Collection<SecurityContextChangedListener> listeners) Construct aListeningSecurityContextHolderStrategy
ListeningSecurityContextHolderStrategy
(SecurityContextHolderStrategy delegate, SecurityContextChangedListener... listeners) Construct aListeningSecurityContextHolderStrategy
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Clears 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.void
setContext
(SecurityContext context) Sets the current context.
-
Constructor Details
-
ListeningSecurityContextHolderStrategy
public ListeningSecurityContextHolderStrategy(SecurityContextHolderStrategy delegate, Collection<SecurityContextChangedListener> listeners) Construct aListeningSecurityContextHolderStrategy
- Parameters:
listeners
- the listeners that should be notified when theSecurityContext
isset
orcleared
delegate
- the underlyingSecurityContextHolderStrategy
-
ListeningSecurityContextHolderStrategy
public ListeningSecurityContextHolderStrategy(SecurityContextHolderStrategy delegate, SecurityContextChangedListener... listeners) Construct aListeningSecurityContextHolderStrategy
- Parameters:
listeners
- the listeners that should be notified when theSecurityContext
isset
orcleared
delegate
- the underlyingSecurityContextHolderStrategy
-
-
Method Details
-
clearContext
public void clearContext()Clears the current context.- Specified by:
clearContext
in interfaceSecurityContextHolderStrategy
-
getContext
Obtains the current context.- Specified by:
getContext
in interfaceSecurityContextHolderStrategy
- Returns:
- a context (never
null
- create a default implementation if necessary)
-
setContext
Sets the current context.- Specified by:
setContext
in interfaceSecurityContextHolderStrategy
- Parameters:
context
- to the new argument (should never benull
, although implementations must check ifnull
has been passed and throw anIllegalArgumentException
in such cases)
-
createEmptyContext
Creates a new, empty context implementation, for use by SecurityContextRepository implementations, when creating a new context for the first time.- Specified by:
createEmptyContext
in interfaceSecurityContextHolderStrategy
- Returns:
- the empty context.
-