From 1d5744a51937665b35658dc00541f0cd21608b57 Mon Sep 17 00:00:00 2001 From: lskublik Date: Wed, 3 Jun 2020 07:41:32 +0200 Subject: [PATCH] adding of customization for TomcatWebServerFactoryCustomizer (MID-6283, MID-6284) --- .../web/boot/MidPointSpringApplication.java | 94 +++++++------------ 1 file changed, 35 insertions(+), 59 deletions(-) diff --git a/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/boot/MidPointSpringApplication.java b/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/boot/MidPointSpringApplication.java index c68c824a9ad..601f5a66d01 100644 --- a/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/boot/MidPointSpringApplication.java +++ b/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/boot/MidPointSpringApplication.java @@ -24,9 +24,16 @@ import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringBootConfiguration; +import org.springframework.boot.actuate.autoconfigure.web.server.ManagementWebServerFactoryCustomizer; import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.autoconfigure.web.embedded.JettyWebServerFactoryCustomizer; +import org.springframework.boot.autoconfigure.web.embedded.NettyWebServerFactoryCustomizer; +import org.springframework.boot.autoconfigure.web.embedded.TomcatWebServerFactoryCustomizer; +import org.springframework.boot.autoconfigure.web.embedded.UndertowWebServerFactoryCustomizer; +import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryCustomizer; import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryCustomizer; import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; @@ -39,6 +46,7 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ImportResource; import org.springframework.context.annotation.Profile; +import org.springframework.core.env.Environment; import org.springframework.http.HttpStatus; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.annotation.EnableScheduling; @@ -169,39 +177,30 @@ public void invalidExpiredSessions() { @Component @EnableConfigurationProperties(ServerProperties.class) - public class ServerCustomization implements WebServerFactoryCustomizer { + public class ServerCustomization implements WebServerFactoryCustomizer { @Value("${server.servlet.session.timeout}") private int sessionTimeout; @Value("${server.servlet.context-path}") private String servletPath; - @Value("${server.use-forward-headers:false}") - private Boolean useForwardHeaders; - - @Value("${server.tomcat.internal-proxies:@null}") - private String internalProxies; - - @Value("${server.tomcat.protocol-header:@null}") - private String protocolHeader; - - @Value("${server.tomcat.protocol-header-https-value:@null}") - private String protocolHeaderHttpsValue; - - @Value("${server.tomcat.port-header:@null}") - private String portHeader; - @Autowired private ServerProperties serverProperties; @Autowired private TaskManager taskManager; + @Autowired + private Environment env; + + @Override - public void customize(ConfigurableServletWebServerFactory serverFactory) { + public void customize(MidPointTomcatServletWebServerFactory serverFactory) { - ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer = new ServletWebServerFactoryCustomizer(serverProperties); - servletWebServerFactoryCustomizer.customize(serverFactory); + ServletWebServerFactoryCustomizer webServletWebServerFactoryCustomizer = new ServletWebServerFactoryCustomizer(serverProperties); + webServletWebServerFactoryCustomizer.customize(serverFactory); + TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer = new TomcatWebServerFactoryCustomizer(env, serverProperties); + tomcatWebServerFactoryCustomizer.customize(serverFactory); serverFactory.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED, "/error/401")); serverFactory.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/error/403")); @@ -214,47 +213,24 @@ public void customize(ConfigurableServletWebServerFactory serverFactory) { serverFactory.setSession(session); if (serverFactory instanceof TomcatServletWebServerFactory) { - customizeTomcat((TomcatServletWebServerFactory) serverFactory); + TomcatContextCustomizer contextCustomizer = new TomcatContextCustomizer() { + @Override + public void customize(Context context) { + setTomcatContext(context); + } + }; + List contextCustomizers = new ArrayList<>(); + contextCustomizers.add(contextCustomizer); + serverFactory.setTomcatContextCustomizers(contextCustomizers); + + // Tomcat valve used to redirect root URL (/) to real application URL (/midpoint/). + // See comments in TomcatRootValve + Valve rootValve = new TomcatRootValve(servletPath); + serverFactory.addEngineValves(rootValve); + + Valve nodeIdHeaderValve = new NodeIdHeaderValve(taskManager); + serverFactory.addEngineValves(nodeIdHeaderValve); } } - - private void customizeTomcat(TomcatServletWebServerFactory tomcatFactory) { - // set tomcat context. - TomcatContextCustomizer contextCustomizer = new TomcatContextCustomizer() { - @Override - public void customize(Context context) { - setTomcatContext(context); - } - }; - List contextCustomizers = new ArrayList<>(); - contextCustomizers.add(contextCustomizer); - tomcatFactory.setTomcatContextCustomizers(contextCustomizers); - - // Tomcat valve used to redirect root URL (/) to real application URL (/midpoint/). - // See comments in TomcatRootValve - Valve rootValve = new TomcatRootValve(servletPath); - tomcatFactory.addEngineValves(rootValve); - - Valve nodeIdHeaderValve = new NodeIdHeaderValve(taskManager); - tomcatFactory.addEngineValves(nodeIdHeaderValve); - - if (useForwardHeaders) { - RemoteIpValve remoteIpValve = new RemoteIpValve(); - if (StringUtils.isNotEmpty(internalProxies)) { - remoteIpValve.setInternalProxies(internalProxies); - } - if (StringUtils.isNotEmpty(protocolHeader)) { - remoteIpValve.setProtocolHeader(protocolHeader); - } - if (StringUtils.isNotEmpty(protocolHeaderHttpsValue)) { - remoteIpValve.setProtocolHeaderHttpsValue(protocolHeaderHttpsValue); - } - if (StringUtils.isNotEmpty(portHeader)) { - remoteIpValve.setPortHeader(portHeader); - } - tomcatFactory.addEngineValves(remoteIpValve); - } - } - } }