Class SpringSecurityContextHandler
- All Implemented Interfaces:
SecurityContextHandler<org.springframework.security.core.Authentication>
SecurityContextHandler.
Manages the thread-local SecurityContext via
SecurityContextHolder for multi-user test isolation.
The SecurityContextHandler.setupAuthentication(Object) method expects an
Authentication instance as the credentials parameter, or null
for an anonymous user — in which case an AnonymousAuthenticationToken
is installed (mirroring the behaviour of @WithAnonymousUser).
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidClears the security context from the current thread.org.springframework.security.core.AuthenticationcreateCredentials(String username, String... roles) Builds anAuthenticationfor the given username and roles, in the same shape produced by Spring Security's@WithMockUser: aUsernamePasswordAuthenticationTokencarrying aUserprincipal whose authorities are the given roles, prefixed withROLE_when not already prefixed.voidrestoreContext(Object snapshot) Restores a previously saved security context snapshot onto the current thread.org.springframework.security.core.context.SecurityContextCaptures the current thread's security context as an opaque snapshot.voidsetupAuthentication(org.springframework.security.core.Authentication credentials) Sets up authentication for a new user from the given credentials.
-
Constructor Details
-
SpringSecurityContextHandler
public SpringSecurityContextHandler()
-
-
Method Details
-
setupAuthentication
public void setupAuthentication(org.springframework.security.core.Authentication credentials) Description copied from interface:SecurityContextHandlerSets up authentication for a new user from the given credentials.The type parameter
Cis determined by the framework implementation. For example, Spring usesorg.springframework.security.core.Authenticationand Quarkus usesio.quarkus.security.identity.SecurityIdentity.Implementations must accept
nullcredentials and produce an anonymous-equivalent state — e.g. Spring sets anAnonymousAuthenticationToken, mirroring@WithAnonymousUser.SecurityContextHandler.clearContext()is invoked immediately before this method so that earlier state cannot leak through.- Specified by:
setupAuthenticationin interfaceSecurityContextHandler<org.springframework.security.core.Authentication>- Parameters:
credentials- framework-specific credentials object, ornullfor an anonymous user
-
saveContext
public org.springframework.security.core.context.SecurityContext saveContext()Description copied from interface:SecurityContextHandlerCaptures the current thread's security context as an opaque snapshot.Called automatically when switching away from a user context to preserve its security state.
- Specified by:
saveContextin interfaceSecurityContextHandler<org.springframework.security.core.Authentication>- Returns:
- an opaque snapshot of the current security context, or
nullif no security context is active
-
restoreContext
Description copied from interface:SecurityContextHandlerRestores a previously saved security context snapshot onto the current thread.- Specified by:
restoreContextin interfaceSecurityContextHandler<org.springframework.security.core.Authentication>- Parameters:
snapshot- a snapshot previously returned bySecurityContextHandler.saveContext(), ornullto clear the context
-
clearContext
public void clearContext()Description copied from interface:SecurityContextHandlerClears the security context from the current thread.- Specified by:
clearContextin interfaceSecurityContextHandler<org.springframework.security.core.Authentication>
-
createCredentials
public org.springframework.security.core.Authentication createCredentials(String username, String... roles) Builds anAuthenticationfor the given username and roles, in the same shape produced by Spring Security's@WithMockUser: aUsernamePasswordAuthenticationTokencarrying aUserprincipal whose authorities are the given roles, prefixed withROLE_when not already prefixed.- Specified by:
createCredentialsin interfaceSecurityContextHandler<org.springframework.security.core.Authentication>- Parameters:
username- the usernameroles- the roles for the user; nevernull, may be empty- Returns:
- the credentials, never
null
-