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.

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 Details

    • SpringSecurityClientInfoAdapter

      public SpringSecurityClientInfoAdapter(boolean trustForwardedHeaders)
    • SpringSecurityClientInfoAdapter

      public SpringSecurityClientInfoAdapter(boolean trustForwardedHeaders, CallerMode configuredMode, CallerIdentityResolver callerIdentityResolver)
      Parameters:
      trustForwardedHeaders - whether X-Forwarded-For is trusted for the IP fallback
      configuredMode - the application-wide caller mode, used for methods that do not override it; null or CallerMode.DEFAULT mean CallerMode.NAME
      callerIdentityResolver - resolver used for CallerMode.CUSTOM, may be null
  • 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
    • resolveCallerIdentity

      public String resolveCallerIdentity(CallerMode mode)
      Description copied from interface: IClientInfoPort
      Resolves the caller identity for an invocation whose @Logged annotation requests a specific CallerMode.

      This is a default method so that existing implementations remain source-compatible; the default ignores mode and delegates to IClientInfoPort.resolveCallerIdentity(). Implementations that support caller modes override it, treating CallerMode.DEFAULT as "use the configured application-wide mode".

      Specified by:
      resolveCallerIdentity in interface IClientInfoPort
      Parameters:
      mode - the caller mode requested by the invoked method, never null
      Returns:
      a caller identity, as described on IClientInfoPort.resolveCallerIdentity()
    • 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)
    • resolveCallerDevice

      public String resolveCallerDevice()
      Resolves a coarse description of the caller's device from the current request's User-Agent header, never the raw header itself; see DeviceInfoResolver.
      Specified by:
      resolveCallerDevice in interface IClientInfoPort
      Returns:
      a coarse device description, a placeholder such as "unknown" when it cannot be resolved, or null if this adapter does not implement device resolution