Interface SecurityContextHandler<C>
- All Known Implementing Classes:
QuarkusSecurityContextHandler,SpringSecurityContextHandler
Framework modules (Spring, Quarkus) provide implementations that bridge their
security infrastructure (e.g. Spring's SecurityContextHolder,
Quarkus's CurrentIdentityAssociation) with the browserless multi-user
context hierarchy.
Implementations must be thread-safe with respect to the thread-local security state they manage.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidClears the security context from the current thread.default CcreateCredentials(String username, String... roles) Builds framework-specific credentials for the given username and roles.voidrestoreContext(Object snapshot) Restores a previously saved security context snapshot onto the current thread.Captures the current thread's security context as an opaque snapshot.voidsetupAuthentication(C credentials) Sets up authentication for a new user from the given credentials.
-
Method Details
-
setupAuthentication
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
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
Restores a previously saved security context snapshot onto the current thread.- Parameters:
snapshot- a snapshot previously returned bysaveContext(), ornullto clear the context
-
clearContext
void clearContext()Clears the security context from the current thread. -
createCredentials
Builds framework-specific credentials for the given username and roles.Used by
SecuredBrowserlessApplicationContext.newUser(String, String...)so tests can authenticate a user without writing the framework-specific boilerplate. Spring's implementation produces aUsernamePasswordAuthenticationTokencarrying aUserprincipal (mirroring@WithMockUser); Quarkus's implementation produces aQuarkusSecurityIdentity.The default implementation throws
UnsupportedOperationException— handlers that don't have a natural mapping from username + roles toCcan simply leave it unimplemented; callers must then usenewUser(C credentials)directly.- Parameters:
username- the usernameroles- the roles for the user; nevernull, may be empty- Returns:
- the credentials, never
null
-