Class AuthenticationHandler
java.lang.Object
com.loomcache.server.security.AuthenticationHandler
Server-side authorization handler.
Architecture
Authentication can be handled externally by a Spring Cloud Gateway (or similar). LoomCache then performs authorization based on trusted headers:X-Auth-User— the authenticated usernameX-Auth-Roles— comma-separated roles (e.g., "ROLE_READ,ROLE_WRITE")
AUTH message before role-based authorization is applied.
When a client connects via the gateway, the AUTH message carries:
- Key = username (from X-Auth-User header)
- Value = roles string (from X-Auth-Roles header)
LoomCache resolves roles to permission sets via AuthConfig and caches
the resolved permissions per connection for the lifetime of the session.
Node-to-Node Communication
Inter-node connections are secured by mTLS and automatically trusted — no AUTH handshake is required for cluster-internal traffic.Thread-safe: uses ConcurrentHashMap for session state.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classCached authorization state for a connection. -
Constructor Summary
ConstructorsConstructorDescriptionAuthenticationHandler(@Nullable AuthConfig config) Create an AuthenticationHandler with the given configuration.AuthenticationHandler(@Nullable AuthConfig config, Set<String> trustedGatewayAddresses, long sessionTimeoutMs) Create an AuthenticationHandler with gateway trust verification and session timeout.AuthenticationHandler(@Nullable AuthConfig config, Set<String> trustedGatewayAddresses, long sessionTimeoutMs, @Nullable CertPermissionMapper certMapper) Create an AuthenticationHandler with gateway trust, session timeout, and optional cert mapper. -
Method Summary
Modifier and TypeMethodDescriptionGet the role for a connection.@Nullable AuthenticationHandler.AuthSessiongetSession(String connectionId) Get the session for a connection (contains username, permissions, and role).getUsername(String connectionId) Get the username for a connection.handleAuth(Message authMsg, ConnectionContext ctx) Handle an AUTH message from a gateway-authenticated client.booleanisAuthenticated(String connectionId) Check if a connection has been authorized.booleanisAuthorized(String connectionId, AuthConfig.PermissionRequest request) Check if a connection is authorized for a scoped operation.booleanisAuthorized(String connectionId, String command) Check if a connection is authorized for a specific command.booleanCheck if auth is enabled.voidremoveSession(String connectionId) Remove session state when a connection closes.@Nullable CertPermissionMapper.PermissionLevelResolve permission level from a connection's TLS client certificate.voidshutdown()Shutdown the session cleanup scheduler.voidtrustClusterPeer(String connectionId, String peerId) Mark a connection as a trusted cluster peer (mTLS authenticated).
-
Constructor Details
-
AuthenticationHandler
Create an AuthenticationHandler with the given configuration.- Parameters:
config- the authentication configuration (if null, auth is disabled)
-
AuthenticationHandler
public AuthenticationHandler(@Nullable AuthConfig config, Set<String> trustedGatewayAddresses, long sessionTimeoutMs) Create an AuthenticationHandler with gateway trust verification and session timeout.- Parameters:
config- the authentication configuration (if null, auth is disabled)trustedGatewayAddresses- set of trusted gateway IP addresses/hostnames (may be empty)sessionTimeoutMs- session timeout in milliseconds
-
AuthenticationHandler
public AuthenticationHandler(@Nullable AuthConfig config, Set<String> trustedGatewayAddresses, long sessionTimeoutMs, @Nullable CertPermissionMapper certMapper) Create an AuthenticationHandler with gateway trust, session timeout, and optional cert mapper.- Parameters:
config- the authentication configuration (if null, auth is disabled)trustedGatewayAddresses- set of trusted gateway IP addresses/hostnames (may be empty)sessionTimeoutMs- session timeout in millisecondscertMapper- optional certificate-to-permission mapper (may be null)
-
-
Method Details
-
handleAuth
Handle an AUTH message from a gateway-authenticated client.Key = username (from gateway's X-Auth-User header). Value = roles string (from gateway's X-Auth-Roles header).
Resolves roles to permissions using the configured role definitions and caches the result for the connection's lifetime.
Audit logging: All authorization decisions are logged for compliance.
- Parameters:
authMsg- the authentication message (must not be null)ctx- the connection context (must not be null)- Returns:
- AUTH_RESPONSE message with result
-
isAuthenticated
Check if a connection has been authorized. Returns true if auth is disabled or if the connection has a valid session. -
isAuthorized
Check if a connection is authorized for a specific command. Logs all authorization decisions for audit trail.- Parameters:
connectionId- the connection identifiercommand- the command name (e.g., "MAP_PUT", "QUEUE_PUSH")- Returns:
- true if authorized
-
isAuthorized
Check if a connection is authorized for a scoped operation. Logs all authorization decisions for audit trail.- Parameters:
connectionId- the connection identifierrequest- request authorization metadata- Returns:
- true if authorized
-
getUsername
-
getRole
-
getSession
Get the session for a connection (contains username, permissions, and role). Used for comprehensive audit logging. -
removeSession
Remove session state when a connection closes. -
isEnabled
public boolean isEnabled()Check if auth is enabled. -
trustClusterPeer
-
resolveFromCertificate
Resolve permission level from a connection's TLS client certificate. Returns null if no certificate is available or cert mapper is not configured.- Parameters:
ctx- the connection context (must not be null)- Returns:
- the resolved permission level, or null if cert-based auth is not applicable
-
shutdown
public void shutdown()Shutdown the session cleanup scheduler. Should be called when the handler is being destroyed.
-