Skip to content

Commit

Permalink
clear MDC
Browse files Browse the repository at this point in the history
  • Loading branch information
tendestad committed Nov 13, 2024
1 parent bb1cc0e commit 683ee82
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions web/src/main/java/no/nav/ung/sak/web/server/jetty/JettyServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
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;
import org.eclipse.jetty.util.thread.VirtualThreadPool;
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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}

}

0 comments on commit 683ee82

Please sign in to comment.