From 683ee828dd9e7c2ff6c6e81bf440e1981575078e Mon Sep 17 00:00:00 2001 From: Tore Langedal Endestad Date: Wed, 13 Nov 2024 16:49:02 +0100 Subject: [PATCH] clear MDC --- .../ung/sak/web/server/jetty/JettyServer.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/web/src/main/java/no/nav/ung/sak/web/server/jetty/JettyServer.java b/web/src/main/java/no/nav/ung/sak/web/server/jetty/JettyServer.java index d731b016c4..8495336deb 100644 --- a/web/src/main/java/no/nav/ung/sak/web/server/jetty/JettyServer.java +++ b/web/src/main/java/no/nav/ung/sak/web/server/jetty/JettyServer.java @@ -23,6 +23,7 @@ import org.eclipse.jetty.security.DefaultIdentityService; import org.eclipse.jetty.security.jaas.JAASLoginService; import org.eclipse.jetty.server.*; +import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.ResourceFactory; import org.eclipse.jetty.util.thread.QueuedThreadPool; @@ -30,6 +31,7 @@ import org.glassfish.jersey.servlet.init.JerseyServletContainerInitializer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; import java.io.IOException; import java.util.ArrayList; @@ -93,16 +95,17 @@ private static void taNedApplikasjonVedError() { } private void start(AppKonfigurasjon appKonfigurasjon) throws Exception { - // setter opp jetty til å bruke virtuelle tråder. - // hver request får sin egen (virtuelle) tråd, så det er ingen risiko for å lekke informasjon (for eksempel: innlogginstilstand,token,MDC-context) ved at tråder gjenbrukes - // https://jetty.org/docs/jetty/12/programming-guide/arch/threads.html QueuedThreadPool threadPool = new QueuedThreadPool(); Server server = new Server(threadPool); server.setConnectors(createConnectors(appKonfigurasjon, server).toArray(new Connector[]{})); - server.setHandler(createContext(appKonfigurasjon, server)); + WebAppContext webAppContext = createContext(appKonfigurasjon, server); + server.setHandler(new Handler.Sequence( + new ClearMdcHandler(), + webAppContext.get() + )); server.addEventListener(new JettyServerLifeCyleListener()); server.start(); @@ -248,4 +251,17 @@ protected Resource createResourceCollection(Server server) { ResourceFactory.of(server).newClassLoaderResource("/web")); } + + + /** + * brukes for å slette tilstand i MDC på starten av en request + */ + private static class ClearMdcHandler extends Handler.Abstract{ + @Override + public boolean handle(Request request, Response response, Callback callback) { + MDC.clear(); + return false; + } + } + }