Skip to content

Instantly share code, notes, and snippets.

@abatkin
Created July 31, 2015 01:47
Show Gist options
  • Save abatkin/2a0b58b67457d4f263ef to your computer and use it in GitHub Desktop.
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)
@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