Class RedactingLoggingInterceptor

java.lang.Object
com.shri.restinpeace.interceptor.RedactingLoggingInterceptor
All Implemented Interfaces:
RequestInterceptor

public class RedactingLoggingInterceptor extends Object implements RequestInterceptor
A LoggingInterceptor-style pre-built interceptor that also logs the request and response bodies, masking a configured set of field names (case-insensitively) instead of printing them verbatim - so logging bodies is safe by default instead of a footgun. Sends to System.out::println by default; pass a Consumer to route lines to a real logger instead, without pulling a logging framework into this library's own dependencies.

Masking is a regex match over "fieldName": value-shaped text (a quoted key, a colon, then a quoted string or a bare token up to the next ,/}/]/whitespace) - a best-effort scan for JSON-shaped bodies, not a real JSON parser, so a sensitive field whose value is itself a nested object or array is only masked up to that value's first ,/}/]. The request body comes from RequestContext.getBody() (a String/POJO @Body only - null for @FormUrlEncoded/@Multipart, same limitation as that method); the response body is whatever afterResponse(com.shri.restinpeace.interceptor.RequestContext, int, java.lang.Object) receives, converted with String.valueOf(...) first - reliable for a String response, best-effort for a decoded POJO (masking then depends on its own toString() happening to render "fieldName": value pairs).

  • Field Details

    • DEFAULT_SENSITIVE_FIELD_NAMES

      public static final Set<String> DEFAULT_SENSITIVE_FIELD_NAMES
      The field names masked by default: password, token, secret, apiKey, ssn, and authorization.
  • Constructor Details

    • RedactingLoggingInterceptor

      public RedactingLoggingInterceptor()
      Logs to System.out, masking DEFAULT_SENSITIVE_FIELD_NAMES.
    • RedactingLoggingInterceptor

      public RedactingLoggingInterceptor(Set<String> sensitiveFieldNames)
      Logs to System.out, masking a custom set of field names instead of DEFAULT_SENSITIVE_FIELD_NAMES.
      Parameters:
      sensitiveFieldNames - the field names to mask, matched case-insensitively; replaces the default set entirely rather than adding to it
    • RedactingLoggingInterceptor

      public RedactingLoggingInterceptor(Set<String> sensitiveFieldNames, Consumer<String> sink)
      Logs to a custom sink instead of System.out, masking a custom set of field names.
      Parameters:
      sensitiveFieldNames - the field names to mask, matched case-insensitively; replaces the default set entirely rather than adding to it
      sink - receives each log line, e.g. a logger method reference
  • Method Details