Created
July 31, 2015 01:47
-
-
Save abatkin/2a0b58b67457d4f263ef to your computer and use it in GitHub Desktop.
Tomcat Access valve for populating User Principal from remote header (from Spring Boot)
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
@Bean | |
public EmbeddedServletContainerCustomizer containerCustomizer() { | |
return new EmbeddedServletContainerCustomizer() { | |
public void customize(ConfigurableEmbeddedServletContainer factory) { | |
if (factory instanceof TomcatEmbeddedServletContainerFactory) { | |
TomcatEmbeddedServletContainerFactory containerFactory = (TomcatEmbeddedServletContainerFactory) factory; | |
containerFactory.addContextValves(new ValveBase() { | |
@Override | |
public void invoke(Request request, Response response) throws IOException, ServletException { | |
String username = request.getHeader("X-Remote-User"); | |
if (username != null) { | |
Principal p = new GenericPrincipal(username, null, null); | |
request.setUserPrincipal(p); | |
} | |
getNext().invoke(request, response); | |
} | |
} | |
); | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment