Class RedactingLoggingInterceptor
- All Implemented Interfaces:
RequestInterceptor
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 Summary
FieldsModifier and TypeFieldDescriptionThe field names masked by default:password,token,secret,apiKey,ssn, andauthorization. -
Constructor Summary
ConstructorsConstructorDescriptionLogs toSystem.out, maskingDEFAULT_SENSITIVE_FIELD_NAMES.RedactingLoggingInterceptor(Set<String> sensitiveFieldNames) Logs toSystem.out, masking a custom set of field names instead ofDEFAULT_SENSITIVE_FIELD_NAMES.RedactingLoggingInterceptor(Set<String> sensitiveFieldNames, Consumer<String> sink) Logs to a custom sink instead ofSystem.out, masking a custom set of field names. -
Method Summary
Modifier and TypeMethodDescriptionvoidafterResponse(RequestContext context, int status, Object body) Called once the response is back.voidbeforeRequest(RequestContext context) Called before the request is sent.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.shri.restinpeace.interceptor.RequestInterceptor
shortCircuit
-
Field Details
-
DEFAULT_SENSITIVE_FIELD_NAMES
The field names masked by default:password,token,secret,apiKey,ssn, andauthorization.
-
-
Constructor Details
-
RedactingLoggingInterceptor
public RedactingLoggingInterceptor()Logs toSystem.out, maskingDEFAULT_SENSITIVE_FIELD_NAMES. -
RedactingLoggingInterceptor
Logs toSystem.out, masking a custom set of field names instead ofDEFAULT_SENSITIVE_FIELD_NAMES.- Parameters:
sensitiveFieldNames- the field names to mask, matched case-insensitively; replaces the default set entirely rather than adding to it
-
RedactingLoggingInterceptor
Logs to a custom sink instead ofSystem.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 itsink- receives each log line, e.g. a logger method reference
-
-
Method Details
-
beforeRequest
Description copied from interface:RequestInterceptorCalled before the request is sent. Can add headers viaRequestContext.addHeader(java.lang.String, java.lang.String), or abort the call by throwing.- Specified by:
beforeRequestin interfaceRequestInterceptor- Parameters:
context- the request being made
-
afterResponse
Description copied from interface:RequestInterceptorCalled once the response is back.- Specified by:
afterResponsein interfaceRequestInterceptor- Parameters:
context- the request that was madestatus- the HTTP response status codebody- the response body - aString, a deserialized object, ornullfor avoid-returning method
-