When setting up Tomcat: ServletContext is required to configure default servlet handling

I have a simple spring boot application that is already running: I can curl to get to endpoints, etc.

I am trying to configure it for use with HTTPS in the spring download help guide (pg 115) and the code is added, for example therefore in my @Configuration class:

@Bean
@Inject
public EmbeddedServletContainerCustomizer tomcatCustomizer(@Value("${keystore.file}") String keystoreFile,
                                                           @Value("${keystore.password}") String keystorePassword,
                                                           @Value("${keystore.type}") String keystoreType,
                                                           @Value("${keystore.alias}") String keystoreAlias) throws FileNotFoundException
{
    final String absoluteKeystoreFile = ResourceUtils.getFile(keystoreFile).getAbsolutePath();
    EmbeddedServletContainerCustomizer tomcatCustomizer = (ConfigurableEmbeddedServletContainer factory) -> {
        TomcatEmbeddedServletContainerFactory containerFactory = (TomcatEmbeddedServletContainerFactory) factory;
        containerFactory.addConnectorCustomizers((TomcatConnectorCustomizer) (Connector connector) -> {
            connector.setSecure(true);
            connector.setScheme("https");
            connector.setAttribute("keystoreFile", absoluteKeystoreFile);
            connector.setAttribute("keystorePass", keystorePassword);
            connector.setAttribute("keystoreType", keystoreType);
            connector.setAttribute("keyAlias", keystoreAlias);
            connector.setAttribute("clientAuth", "false");
            connector.setAttribute("sslProtocol", "TLS");
            connector.setAttribute("SSLEnabled", true);
        });
    };

    return tomcatCustomizer;
}

However, only with the addition of this code, my previously running application now receives the following stack trace:

Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.<init>(DefaultServletHandlerConfigurer.java:54)
at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:346)
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$f697b5e2.CGLIB$defaultServletHandlerMapping$23(<generated>)
at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$f697b5e2$$FastClassBySpringCGLIB$$972d2616.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
at org.springfr.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$f697b5e2.defaultServletHandlerMapping(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
... 16 more

I checked that the properties are coming correctly to the method, and tried to add / remove @WebAppConfiguration, as suggested by this question

My gradle file uses spring-boot-starter-web: 1.0.0.RELEASE.

: https spring? , tomcat , tomcat?

+4
1

:

https spring boot: @Configuration. SecurityConfig WebSecurityConfigurerAdapter. tomcat @Configuration, .

: , WebSecurityConfigurerAdapter , @Configurations.

+8

Source: https://habr.com/ru/post/1536069/


All Articles