-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Logg infomelding dersom path/querystring matcher fnr-regex
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/java/no/nav/veilarbperson/config/FnrUsageLoggerInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |