refactor(api): switch to Spring logging filter (#195)

* refactor(api): switch to Spring logging filter

instead of making our own logging filter via a OncePerRequest,
we can instead use the more fully featured CommonsRequestLoggingFilter
with a bit of customization sprinkled on to ignore websocket requests
and health check requests

* fix(api): check sensitive headers & move up path

* fix: invert path check
This commit is contained in:
James Ward
2026-04-22 17:26:53 -04:00
committed by Zach Yale
parent 74c7ad5502
commit 5f06ab92e9
3 changed files with 64 additions and 66 deletions
@@ -1,66 +0,0 @@
package org.booklore.config;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.io.IOException;
import java.util.Locale;
import java.util.Set;
@Slf4j
@Component
@Profile({"dev"})
public class LoggingFilter extends OncePerRequestFilter {
private static final Set<String> SENSITIVE_HEADERS = Set.of(
"authorization", "cookie", "set-cookie", "x-api-key", "x-forwarded-access-token"
);
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if (request.getRequestURI().startsWith("/ws")) {
filterChain.doFilter(request, response);
return;
}
long start = System.currentTimeMillis();
log.info("Incoming request: {} {} from IP {}",
request.getMethod(),
request.getRequestURI(),
request.getRemoteAddr());
ServletUriComponentsBuilder servletUriComponentsBuilder = ServletUriComponentsBuilder
.fromCurrentContextPath();
log.info("servletUriComponentsBuilder.toUriString(): {}", servletUriComponentsBuilder.toUriString());
var headerNames = request.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String headerValue = SENSITIVE_HEADERS.contains(headerName.toLowerCase(Locale.ROOT))
? "[REDACTED]" : request.getHeader(headerName);
log.info("Header: {}={}", headerName, headerValue);
}
}
filterChain.doFilter(request, response);
long duration = System.currentTimeMillis() - start;
log.info("Completed {} {} with status {} in {} ms",
request.getMethod(),
request.getRequestURI(),
response.getStatus(),
duration);
}
}
@@ -0,0 +1,39 @@
package org.booklore.config.logging;
import org.booklore.config.logging.filter.RequestLoggingFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.GenericFilterBean;
import java.util.Set;
@Configuration
public class RequestLoggingFilterConfig {
private static final Set<String> EXCLUDED_PATHS = Set.of(
"/api/v1/healthcheck",
"/ws"
);
private static final Set<String> EXCLUDED_HEADERS = Set.of(
"authorization",
"cookie",
"set-cookie",
"x-api-key",
"x-forwarded-access-token"
);
@Bean
public GenericFilterBean logFilter() {
RequestLoggingFilter filter = new RequestLoggingFilter();
filter.setIncludeQueryString(true);
filter.setIncludePayload(true);
filter.setMaxPayloadLength(10000);
filter.setIncludeHeaders(true);
filter.setHeaderPredicate((header) -> !EXCLUDED_HEADERS.contains(header.toLowerCase()));
filter.setPathPredicate((path) -> !EXCLUDED_PATHS.contains(path));
return filter;
}
}
@@ -0,0 +1,25 @@
package org.booklore.config.logging.filter;
import jakarta.servlet.http.HttpServletRequest;
import org.jspecify.annotations.Nullable;
import org.springframework.web.filter.CommonsRequestLoggingFilter;
import java.util.Set;
import java.util.function.Predicate;
public class RequestLoggingFilter extends CommonsRequestLoggingFilter {
private Predicate<String> pathPredicate = null;
public void setPathPredicate(@Nullable Predicate<String> pathPredicate) {
this.pathPredicate = pathPredicate;
}
@Override
protected boolean shouldLog(HttpServletRequest request) {
if (pathPredicate != null && !pathPredicate.test(request.getRequestURI())) {
return false;
}
return super.shouldLog(request);
}
}