Class InMemoryClientInfoPort
- All Implemented Interfaces:
IClientInfoPort
IClientInfoPort that always returns a fixed, test-controlled
caller identity, instead of resolving one from Spring Security or the
current HTTP request.
A test asserting on MethodInvocationEvent.callerIdentity()
would otherwise need a real authenticated SecurityContext or a
mock HTTP request just to control this one field. This class lets the
caller identity be set directly instead.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an instance that reports the caller identity as"unknown"untilsetCallerIdentity(String)is called.InMemoryClientInfoPort(String callerIdentity) Creates an instance that reports the given caller identity. -
Method Summary
Modifier and TypeMethodDescriptionResolves an identifier describing who triggered the current invocation.Resolves the IP address of the caller that triggered the current invocation, independent of whateverIClientInfoPort.resolveCallerIdentity()itself resolves to.voidsetCallerIdentity(String callerIdentity) Changes the caller identity reported by subsequent calls toresolveCallerIdentity().voidsetCallerIp(String callerIp) Changes the IP address reported by subsequent calls toresolveCallerIp(), for testing a@Logged(includeIp = true)method'sMethodInvocationEvent.callerIp()without wiring a real HTTP request.
-
Constructor Details
-
InMemoryClientInfoPort
public InMemoryClientInfoPort()Creates an instance that reports the caller identity as"unknown"untilsetCallerIdentity(String)is called. -
InMemoryClientInfoPort
Creates an instance that reports the given caller identity.- Parameters:
callerIdentity- the caller identity to report
-
-
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
-
setCallerIdentity
Changes the caller identity reported by subsequent calls toresolveCallerIdentity().- Parameters:
callerIdentity- the caller identity to report from now on
-
resolveCallerIp
Description copied from interface:IClientInfoPortResolves the IP address of the caller that triggered the current invocation, independent of whateverIClientInfoPort.resolveCallerIdentity()itself resolves to.Unlike
IClientInfoPort.resolveCallerIdentity(), which resolves a single identity from an either/or chain of tiers (authenticated principal, then IP, then a placeholder), this method exists so that a caller's IP can be recorded alongside an authenticated identity rather than only as a fallback used when no identity is available — valuable for security-sensitive operations (login, password reset, admin actions) where the IP remains useful for audit and rate-limiting purposes even when the call also resolves to an authenticated user.This is a default method, not an abstract one, so that adding it does not break existing implementations of this interface compiled against an earlier version of this library. The default returns
null, meaning "this adapter does not support resolving an IP independent of caller identity" — consistent with this library's convention of usingnullfor "not available" on structured fields, rather than a placeholder string.- 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)
-
setCallerIp
Changes the IP address reported by subsequent calls toresolveCallerIp(), for testing a@Logged(includeIp = true)method'sMethodInvocationEvent.callerIp()without wiring a real HTTP request. Defaults tonull(not resolved) until this is called.- Parameters:
callerIp- the IP address to report from now on
-