From d116828d13692b9c32c26cebc3881aa9e90f1fa3 Mon Sep 17 00:00:00 2001 From: Sondre Larsen Ovrid Date: Sun, 15 Oct 2023 15:33:48 +0200 Subject: [PATCH] Logg infomelding dersom path/querystring matcher fnr-regex --- .../config/FnrUsageLoggerInterceptor.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/main/java/no/nav/veilarbperson/config/FnrUsageLoggerInterceptor.java diff --git a/src/main/java/no/nav/veilarbperson/config/FnrUsageLoggerInterceptor.java b/src/main/java/no/nav/veilarbperson/config/FnrUsageLoggerInterceptor.java new file mode 100644 index 00000000..b92e3b16 --- /dev/null +++ b/src/main/java/no/nav/veilarbperson/config/FnrUsageLoggerInterceptor.java @@ -0,0 +1,40 @@ +package no.nav.veilarbperson.config; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; +import no.nav.common.utils.StringUtils; +import org.jetbrains.annotations.NotNull; +import org.slf4j.MDC; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +import java.util.Optional; + +@Slf4j +@Component +public class FnrUsageLoggerInterceptor implements HandlerInterceptor { + + private static final String NAV_CONSUMER_ID_HEADER_NAME = "Nav-Consumer-Id"; + private static final String MDC_ENDPOINT_KEY = "endpoint"; + + @Override + public boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) { + String requestURI = request.getRequestURI(); + String queryString = request.getQueryString(); + String fnrPattern = "\\d{11}"; + + boolean hasFnrInRequestURI = StringUtils.notNullOrEmpty(requestURI) && requestURI.matches(fnrPattern); + boolean hasFnrInQueryString = StringUtils.notNullOrEmpty(queryString) && requestURI.matches(fnrPattern); + + if (hasFnrInRequestURI || hasFnrInQueryString) { + String consumerId = Optional.ofNullable(request.getHeader(NAV_CONSUMER_ID_HEADER_NAME)).orElse("unknown"); + + MDC.put(MDC_ENDPOINT_KEY, requestURI); + log.debug("Konsument {} forespurte endepunkt {} som matcher fnr-regex.", consumerId, requestURI); + MDC.remove(MDC_ENDPOINT_KEY); + } + + return true; + } +}