Skip to content

Commit

Permalink
upgrading of Spring boot to 2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
skublik committed Jun 18, 2018
1 parent 38ae56a commit 74a8486
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 82 deletions.
2 changes: 1 addition & 1 deletion build-system/pom.xml
Expand Up @@ -63,7 +63,7 @@
<selenium.version>1.0.1</selenium.version>
<slf4j.version>1.7.25</slf4j.version>
<logback.version>1.2.3</logback.version>
<spring.boot.version>1.5.8.e1</spring.boot.version> <!-- can be replaced by a stock version;
<spring.boot.version>2.0.1.RELEASE</spring.boot.version> <!-- can be replaced by a stock version;
ideally after https://github.com/spring-projects/spring-boot/issues/11367 is resolved but it should be OK even before that -->
<spring.version>5.0.5.RELEASE</spring.version>
<spring.security.version>5.0.5.RELEASE</spring.security.version>
Expand Down
3 changes: 2 additions & 1 deletion gui/admin-gui/pom.xml
Expand Up @@ -122,6 +122,7 @@
</plugins>
</build>


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -210,7 +211,7 @@
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-core</artifactId>
<version>0.32</version>
<version>0.35</version>
</dependency>
<dependency>
<groupId>ro.isdc.wro4j</groupId>
Expand Down
Expand Up @@ -23,9 +23,10 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration.BeanPostProcessorsRegistrar;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
Expand All @@ -37,7 +38,7 @@
/**
* Custom configuration (factory) for embedded tomcat factory.
* This is necessary, as the tomcat factory is hacking tomcat setup.
* @see MidPointTomcatEmbeddedServletContainerFactory
* @see MidPointTomcatServletWebServerFactory
*
* @author semancik
*/
Expand All @@ -51,12 +52,12 @@ public class EmbeddedTomcatAutoConfiguration {

@Configuration
@ConditionalOnClass({ Servlet.class, Tomcat.class })
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
@ConditionalOnMissingBean(value = TomcatServletWebServerFactory.class, search = SearchStrategy.CURRENT)
public static class EmbeddedTomcat {

@Bean
public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
return new MidPointTomcatEmbeddedServletContainerFactory();
public TomcatServletWebServerFactory tomcatEmbeddedServletContainerFactory() {
return new MidPointTomcatServletWebServerFactory();
}

}
Expand Down
Expand Up @@ -24,6 +24,7 @@
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.util.MidPointProfilingServletFilter;


import org.apache.catalina.Valve;
import org.apache.commons.lang.StringUtils;
import org.apache.cxf.transport.servlet.CXFServlet;
Expand All @@ -36,22 +37,36 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.security.SecurityFilterAutoConfiguration;
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration;
import org.springframework.boot.autoconfigure.web.*;
import org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration;
import org.springframework.boot.autoconfigure.web.embedded.TomcatWebServerFactoryCustomizer;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryCustomizer;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.filter.DelegatingFilterProxy;
import ro.isdc.wro.http.WroFilter;
Expand All @@ -60,7 +75,7 @@

import java.io.File;
import java.lang.management.ManagementFactory;
import java.util.concurrent.TimeUnit;
import java.time.Duration;

/**
* Created by Viliam Repan (lazyman).
Expand Down Expand Up @@ -91,18 +106,20 @@
})
@ImportAutoConfiguration(classes = {
EmbeddedTomcatAutoConfiguration.class,
DispatcherServletAutoConfiguration.class,
WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
SecurityAutoConfiguration.class,
SecurityFilterAutoConfiguration.class,
ServerPropertiesAutoConfiguration.class,
MultipartAutoConfiguration.class
DispatcherServletAutoConfiguration.class,
WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
SecurityAutoConfiguration.class,
SecurityFilterAutoConfiguration.class,
EmbeddedWebServerFactoryCustomizerAutoConfiguration.class,
// ServerPropertiesAutoConfiguration.class,
MultipartAutoConfiguration.class
})
//@EnableConfigurationProperties(ServerProperties.class)
@SpringBootConfiguration
public class MidPointSpringApplication extends SpringBootServletInitializer {

private static final Trace LOGGER = TraceManager.getTrace(MidPointSpringApplication.class);

private static final String MIDPOINT_HOME_PROPERTY = "midpoint.home";
Expand Down Expand Up @@ -162,13 +179,14 @@ private static SpringApplicationBuilder configureApplication(SpringApplicationBu
System.setProperty(MIDPOINT_HOME_PROPERTY, mpHome);
}

System.setProperty("spring.config.location", "${midpoint.home}/");
//TODO Could not resolve placeholder
// System.setProperty("spring.config.location", "${midpoint.home}/");

application.bannerMode(Banner.Mode.LOG);

return application.sources(MidPointSpringApplication.class);
}

@Bean
public ServletListenerRegistrationBean requestContextListener() {
return new ServletListenerRegistrationBean(new RequestContextListener());
Expand All @@ -195,6 +213,11 @@ public FilterRegistrationBean wicket() {

return registration;
}

@Bean
public DelegatingFilterProxy delegatingFilterProxy() {
return new DelegatingFilterProxy();
}

@Bean
public FilterRegistrationBean springSecurityFilterChain() {
Expand Down Expand Up @@ -241,44 +264,116 @@ public ServletRegistrationBean staticWebServlet() {
registration.addUrlMappings("/static-web/*");
return registration;
}

@Bean
public ServerProperties serverProperties() {
return new ServerCustomization();
}

private static class ServerCustomization extends ServerProperties {

@Value("${server.session.timeout}")
private int sessionTimeout;

@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
super.customize(container);

container.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED,
"/error/401"));
container.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN,
"/error/403"));
container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,
"/error/404"));
container.addErrorPages(new ErrorPage(HttpStatus.GONE,
"/error/410"));
container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,
"/error"));

container.setSessionTimeout(sessionTimeout, TimeUnit.MINUTES);

if (container instanceof TomcatEmbeddedServletContainerFactory) {
customizeTomcat((TomcatEmbeddedServletContainerFactory) container);
}
}

private void customizeTomcat(TomcatEmbeddedServletContainerFactory tomcatFactory) {
// Tomcat valve used to redirect root URL (/) to real application URL (/midpoint/).
// See comments in TomcatRootValve
Valve rootValve = new TomcatRootValve();
tomcatFactory.addEngineValves(rootValve);
}
}

// @Value("${server.servlet.session.timeout}")
// private int sessionTimeout;
//
// @Bean
// public void serverProperties(ConfigurableServletWebServerFactory server) {
// server.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED,
// "/error/401"));
// server.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN,
// "/error/403"));
// server.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,
// "/error/404"));
// server.addErrorPages(new ErrorPage(HttpStatus.GONE,
// "/error/410"));
// server.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,
// "/error"));
//
// Session session = new Session();
// session.setTimeout(Duration.ofSeconds(sessionTimeout));
// server.setSession(session);//.setSessionTimeout(sessionTimeout, TimeUnit.MINUTES);
//
// if (server instanceof TomcatServletWebServerFactory) {
// customizeTomcat((TomcatServletWebServerFactory) server);
// }
// }
//
// private void customizeTomcat(TomcatServletWebServerFactory tomcatFactory) {
// // Tomcat valve used to redirect root URL (/) to real application URL (/midpoint/).
// // See comments in TomcatRootValve
// Valve rootValve = new TomcatRootValve();
// tomcatFactory.addEngineValves(rootValve);
// }

// private static class ServerCustomization implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
//
//
//
// @Value("${server.servlet.session.timeout}")
// private int sessionTimeout;
//
//
// @Override
// public void customize(ConfigurableServletWebServerFactory server) {
// LOGGER.info("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX sessionTimeout: {} ", sessionTimeout);
//
// server.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED,
// "/error/401"));
// server.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN,
// "/error/403"));
// server.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,
// "/error/404"));
// server.addErrorPages(new ErrorPage(HttpStatus.GONE,
// "/error/410"));
// server.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,
// "/error"));
//
// Session session = new Session();
// session.setTimeout(Duration.ofSeconds(sessionTimeout));
// server.setSession(session);//.setSessionTimeout(sessionTimeout, TimeUnit.MINUTES);
//
// if (server instanceof TomcatServletWebServerFactory) {
// customizeTomcat((TomcatServletWebServerFactory) server);
// }
// }
//
// private void customizeTomcat(TomcatServletWebServerFactory tomcatFactory) {
// // Tomcat valve used to redirect root URL (/) to real application URL (/midpoint/).
// // See comments in TomcatRootValve
// Valve rootValve = new TomcatRootValve();
// tomcatFactory.addEngineValves(rootValve);
// }
// }

// @Bean
// public ServerProperties serverProperties() {
// return new ServerCustomization();
// }
//
// private static class ServerCustomization extends ServerProperties {
//
// @Value("${server.session.timeout}")
// private int sessionTimeout;
//
// @Override
// public void customize(ConfigurableEmbeddedServletContainer container) {
// super.customize(container);
//
// container.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED,
// "/error/401"));
// container.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN,
// "/error/403"));
// container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,
// "/error/404"));
// container.addErrorPages(new ErrorPage(HttpStatus.GONE,
// "/error/410"));
// container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,
// "/error"));
//
// container.setSessionTimeout(sessionTimeout, TimeUnit.MINUTES);
//
// if (container instanceof TomcatEmbeddedServletContainerFactory) {
// customizeTomcat((TomcatEmbeddedServletContainerFactory) container);
// }
// }
//
// private void customizeTomcat(TomcatEmbeddedServletContainerFactory tomcatFactory) {
// // Tomcat valve used to redirect root URL (/) to real application URL (/midpoint/).
// // See comments in TomcatRootValve
// Valve rootValve = new TomcatRootValve();
// tomcatFactory.addEngineValves(rootValve);
// }
// }
}
Expand Up @@ -16,8 +16,8 @@
package com.evolveum.midpoint.web.boot;

import org.apache.catalina.startup.Tomcat;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;

import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
Expand All @@ -28,23 +28,24 @@
*
* @author semancik
*/
public class MidPointTomcatEmbeddedServletContainerFactory extends TomcatEmbeddedServletContainerFactory {
public class MidPointTomcatServletWebServerFactory extends TomcatServletWebServerFactory {

private static final Trace LOGGER = TraceManager.getTrace(MidPointTomcatEmbeddedServletContainerFactory.class);
private static final Trace LOGGER = TraceManager.getTrace(MidPointTomcatServletWebServerFactory.class);

@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {

protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {

// We are setting up fake context here. This context does not really do anything.
// But it is "mapped" to the root URL (/ ... or rather "" in Tomcat parlance).
// This fake context is necessary. If there is no context at all then
// CoyoteAdapter will not execute any Valves and returns 404 immediately.
// So without this the TomcatRootValve will not work.

RootRootContext rootRootContext = new RootRootContext();
tomcat.getHost().addChild(rootRootContext);
// RootRootContext rootRootContext = new RootRootContext();
// tomcat.getHost().addChild(rootRootContext);

return super.getTomcatEmbeddedServletContainer(tomcat);
return super.getTomcatWebServer(tomcat);
}


Expand Down
Expand Up @@ -31,7 +31,7 @@ public class RootRootContext extends StandardContext {

public RootRootContext() {
super();
setPath(""); // this means "/"
setPath("/ "); // this means "/"
setDisplayName("RootRoot");
}

Expand Down

0 comments on commit 74a8486

Please sign in to comment.