Class SpringSecurityClientInfoAdapter

java.lang.Object
com.fayupable.logged.spring.security.SpringSecurityClientInfoAdapter
All Implemented Interfaces:
IClientInfoPort

public class SpringSecurityClientInfoAdapter extends Object implements 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.

  • Constructor Details

    • SpringSecurityClientInfoAdapter

      public SpringSecurityClientInfoAdapter(boolean trustForwardedHeaders)
  • Method Details

    • resolveCallerIdentity

      public String resolveCallerIdentity()
      Description copied from interface: IClientInfoPort
      Resolves an identifier describing who triggered the current invocation.
      Specified by:
      resolveCallerIdentity in interface IClientInfoPort
      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
    • resolveCallerIp

      public String resolveCallerIp()
      Resolves the caller's IP address independent of resolveCallerIdentity()'s outcome, unlike that method's authenticated-principal-first fallback chain. This has no dependency on SecurityContextHolder state, so it resolves the same way whether or not the current invocation also resolves to an authenticated user.
      Specified by:
      resolveCallerIp in interface IClientInfoPort
      Returns:
      the caller's IP address, or null if it cannot be resolved (for example, no HTTP request is available on the current thread, or this adapter does not implement IP resolution)