Interface SecurityContextHandler
-
- All Implemented Interfaces:
public interface SecurityContextHandler<C>Abstracts per-user security context management for multi-user testing.
Framework modules (Spring, Quarkus) provide implementations that bridge their security infrastructure (e.g. Spring's
SecurityContextHolder, Quarkus'sCurrentIdentityAssociation) with the browserless multi-user context hierarchy.Implementations must be thread-safe with respect to the thread-local security state they manage.
- Since:
1.1
-
-
Method Summary
Modifier and Type Method Description abstract voidsetupAuthentication(C credentials)Sets up authentication for a new user from the given credentials. abstract ObjectsaveContext()Captures the current thread's security context as an opaque snapshot. abstract voidrestoreContext(Object snapshot)Restores a previously saved security context snapshot onto the current thread. abstract voidclearContext()Clears the security context from the current thread. CcreateCredentials(String username, Array<String> roles)Builds framework-specific credentials for the given username and roles. -
-
Method Detail
-
setupAuthentication
abstract void setupAuthentication(C credentials)
Sets 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. clearContext is invoked immediately before this method so that earlier state cannot leak through.- Parameters:
credentials- framework-specific credentials object, ornullfor an anonymous user
-
saveContext
abstract Object saveContext()
Captures the current thread's security context as an opaque snapshot.
Called automatically when switching away from a user context to preserve its security state.
- Returns:
an opaque snapshot of the current security context, or
nullif no security context is active
-
restoreContext
abstract void restoreContext(Object snapshot)
Restores a previously saved security context snapshot onto the current thread.
- Parameters:
snapshot- a snapshot previously returned by saveContext, ornullto clear the context
-
clearContext
abstract void clearContext()
Clears the security context from the current thread.
-
createCredentials
C createCredentials(String username, Array<String> roles)
Builds framework-specific credentials for the given username and roles.
Used by newUser so tests can authenticate a user without writing the framework-specific boilerplate. Spring's implementation produces a
UsernamePasswordAuthenticationTokencarrying aUserprincipal (mirroring@WithMockUser); Quarkus's implementation produces aQuarkusSecurityIdentity.The default implementation throws UnsupportedOperationException — handlers that don't have a natural mapping from username + roles to
Ccan simply leave it unimplemented; callers must then use newUser(C credentials) directly.- Parameters:
username- the usernameroles- the roles for the user; nevernull, may be empty- Returns:
the credentials, never
null
-
-
-
-