Class SpringSecurityClientInfoAdapter
- All Implemented Interfaces:
IClientInfoPort
IClientInfoPort implementation that resolves the caller as the
authenticated Spring Security principal when one is present, falling back
to RequestClientIpResolver otherwise.
Only the principal's name is captured, never any other detail carried
by the Authentication (authorities, credentials, session
attributes), consistent with the rest of this library's approach of
capturing coarse identifiers rather than arbitrary request or security
data.
This adapter is only active when both Spring Security and Spring Web
are present, since it needs Authentication. Services that do not
carry Spring Security at all (for example internal microservices reached
only through a gateway that already authenticates the caller) still get
IP-based identity through HttpRequestClientInfoAdapter instead;
see that class for details, since IP resolution itself has nothing to do
with whether Spring Security is present.
How the principal's name is written is controlled by a
CallerMode: a configured application-wide mode, which a single
@Logged method can override through @Logged(caller = ...).
CallerMode.NAME writes it as is (the default),
CallerMode.MASKED masks it through EmailMasker when it is an
e-mail address, and CallerMode.CUSTOM delegates to an
application-provided CallerIdentityResolver. If a method requests
CUSTOM but no resolver is configured, the name is masked rather
than written in full, so that a missing bean can never leak personal data.
-
Constructor Summary
ConstructorsConstructorDescriptionSpringSecurityClientInfoAdapter(boolean trustForwardedHeaders) SpringSecurityClientInfoAdapter(boolean trustForwardedHeaders, CallerMode configuredMode, CallerIdentityResolver callerIdentityResolver) -
Method Summary
Modifier and TypeMethodDescriptionResolves a coarse description of the caller's device from the current request'sUser-Agentheader, never the raw header itself; seeDeviceInfoResolver.Resolves an identifier describing who triggered the current invocation.Resolves the caller identity for an invocation whose@Loggedannotation requests a specificCallerMode.Resolves the caller's IP address independent ofresolveCallerIdentity()'s outcome, unlike that method's authenticated-principal-first fallback chain.
-
Constructor Details
-
SpringSecurityClientInfoAdapter
public SpringSecurityClientInfoAdapter(boolean trustForwardedHeaders) -
SpringSecurityClientInfoAdapter
public SpringSecurityClientInfoAdapter(boolean trustForwardedHeaders, CallerMode configuredMode, CallerIdentityResolver callerIdentityResolver) - Parameters:
trustForwardedHeaders- whetherX-Forwarded-Foris trusted for the IP fallbackconfiguredMode- the application-wide caller mode, used for methods that do not override it;nullorCallerMode.DEFAULTmeanCallerMode.NAMEcallerIdentityResolver- resolver used forCallerMode.CUSTOM, may benull
-
-
Method Details
-
resolveCallerIdentity
Description copied from interface:IClientInfoPortResolves an identifier describing who triggered the current invocation.- Specified by:
resolveCallerIdentityin interfaceIClientInfoPort- Returns:
- a caller identity such as
"user:42"or"ip:203.0.113.10", or an implementation-defined placeholder such as"unknown"if no identity can be resolved
-
resolveCallerIdentity
Description copied from interface:IClientInfoPortResolves the caller identity for an invocation whose@Loggedannotation requests a specificCallerMode.This is a default method so that existing implementations remain source-compatible; the default ignores
modeand delegates toIClientInfoPort.resolveCallerIdentity(). Implementations that support caller modes override it, treatingCallerMode.DEFAULTas "use the configured application-wide mode".- Specified by:
resolveCallerIdentityin interfaceIClientInfoPort- Parameters:
mode- the caller mode requested by the invoked method, nevernull- Returns:
- a caller identity, as described on
IClientInfoPort.resolveCallerIdentity()
-
resolveCallerIp
Resolves the caller's IP address independent ofresolveCallerIdentity()'s outcome, unlike that method's authenticated-principal-first fallback chain. This has no dependency onSecurityContextHolderstate, so it resolves the same way whether or not the current invocation also resolves to an authenticated user.- Specified by:
resolveCallerIpin interfaceIClientInfoPort- Returns:
- the caller's IP address, or
nullif it cannot be resolved (for example, no HTTP request is available on the current thread, or this adapter does not implement IP resolution)
-
resolveCallerDevice
Resolves a coarse description of the caller's device from the current request'sUser-Agentheader, never the raw header itself; seeDeviceInfoResolver.- Specified by:
resolveCallerDevicein interfaceIClientInfoPort- Returns:
- a coarse device description, a placeholder such as
"unknown"when it cannot be resolved, ornullif this adapter does not implement device resolution
-