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 theSecurityContext
changes. 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 theSecurityContextHolder
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 allowYourListener
and its members to be garbage collected.- Since:
- 5.6
-
-
Constructor Summary
Constructors Constructor Description ListeningSecurityContextHolderStrategy(java.util.Collection<SecurityContextChangedListener> listeners)
Construct aListeningSecurityContextHolderStrategy
based onThreadLocalSecurityContextHolderStrategy
ListeningSecurityContextHolderStrategy(SecurityContextChangedListener... listeners)
Construct aListeningSecurityContextHolderStrategy
based onThreadLocalSecurityContextHolderStrategy
ListeningSecurityContextHolderStrategy(SecurityContextHolderStrategy delegate, java.util.Collection<SecurityContextChangedListener> listeners)
Construct aListeningSecurityContextHolderStrategy
ListeningSecurityContextHolderStrategy(SecurityContextHolderStrategy delegate, SecurityContextChangedListener... listeners)
Construct aListeningSecurityContextHolderStrategy
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clearContext()
Clears the current context.SecurityContext
createEmptyContext()
Creates a new, empty context implementation, for use by SecurityContextRepository implementations, when creating a new context for the first time.SecurityContext
getContext()
Obtains the current context.java.util.function.Supplier<SecurityContext>
getDeferredContext()
Obtains aSupplier
that returns the current context.void
setContext(SecurityContext context)
Sets the current context.void
setDeferredContext(java.util.function.Supplier<SecurityContext> deferredContext)
Sets aSupplier
that will return the current context.
-
-
-
Constructor Detail
-
ListeningSecurityContextHolderStrategy
public ListeningSecurityContextHolderStrategy(java.util.Collection<SecurityContextChangedListener> listeners)
Construct aListeningSecurityContextHolderStrategy
based onThreadLocalSecurityContextHolderStrategy
- Parameters:
listeners
- the listeners that should be notified when theSecurityContext
isset
orcleared
- Since:
- 5.7
-
ListeningSecurityContextHolderStrategy
public ListeningSecurityContextHolderStrategy(SecurityContextChangedListener... listeners)
Construct aListeningSecurityContextHolderStrategy
based onThreadLocalSecurityContextHolderStrategy
- Parameters:
listeners
- the listeners that should be notified when theSecurityContext
isset
orcleared
- Since:
- 5.7
-
ListeningSecurityContextHolderStrategy
public ListeningSecurityContextHolderStrategy(SecurityContextHolderStrategy delegate, java.util.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 Detail
-
clearContext
public void clearContext()
Clears the current context.- Specified by:
clearContext
in interfaceSecurityContextHolderStrategy
-
getContext
public SecurityContext getContext()
Obtains the current context.- Specified by:
getContext
in interfaceSecurityContextHolderStrategy
- Returns:
- a context (never
null
- create a default implementation if necessary)
-
getDeferredContext
public java.util.function.Supplier<SecurityContext> getDeferredContext()
Obtains aSupplier
that returns the current context.- Specified by:
getDeferredContext
in interfaceSecurityContextHolderStrategy
- Returns:
- a
Supplier
that returns the current context (nevernull
- create a default implementation if necessary)
-
setContext
public void setContext(SecurityContext context)
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)
-
setDeferredContext
public void setDeferredContext(java.util.function.Supplier<SecurityContext> deferredContext)
Sets aSupplier
that will return the current context. Implementations can override the default to avoid invokingSupplier.get()
.- Specified by:
setDeferredContext
in interfaceSecurityContextHolderStrategy
- Parameters:
deferredContext
- aSupplier
that returns theSecurityContext
-
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:
createEmptyContext
in interfaceSecurityContextHolderStrategy
- Returns:
- the empty context.
-
-