自定义 Tomcat 时:需要 ServletContext 来配置默认的 servlet 处理 [英] When customizing Tomcat: A ServletContext is required to configure default servlet handling

查看:62
本文介绍了自定义 Tomcat 时:需要 ServletContext 来配置默认的 servlet 处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已经运行的简单 spring boot 应用程序:我可以卷曲到端点等.

I have a simple spring boot application that already runs: I can curl to hit the endpoints, etc.

我正在尝试根据 spring boot 参考指南(第 115 页) 并将这样的代码添加到我的 @Configuration 类中:

I'm trying to configure it for use with HTTPS per the spring boot reference guide (pg 115) and added code like so to 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, with only this code addition, my previously working app now gets 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 verified that the properties are coming in correctly to the method, and tried adding/removing @WebAppConfiguration as suggested by this question

我的 gradle 文件使用的是 spring-boot-starter-web:1.0.0.RELEASE.

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

问题是:如何使用 spring boot 正确配置 https?为什么我的应用程序在我不尝试配置 tomcat 时可以工作,而当我尝试配置 tomcat 时却崩溃了?

The questions are: How do I correctly configure https with spring boot? Why does my the app work when I don't try to configure tomcat, and break when I do try to configure tomcat?

谢谢

推荐答案

刚发完这个问题,我就找到了答案:

Just after posting this question, I discovered the answer:

关于如何使用 spring boot 正确配置 https:上面列出的代码最初是添加到我的@Configuration SecurityConfig extends WebSecurityConfigurerAdapter 中的.当我将 tomcat 自定义移动到它自己的 @Configuration 类时,它起作用了.

For how to correctly configure https with spring boot: The code listed above was originally added to my @Configuration SecurityConfig extends WebSecurityConfigurerAdapter. When I moved the tomcat customization to its own @Configuration class, it worked.

原因:大概 WebSecurityConfigurerAdapter 在应用程序上下文生命周期中比 @Configurations 的其余部分更早地被调用.

For why: Presumably the WebSecurityConfigurerAdapter is called earlier in the application context lifecycle than the rest of the @Configurations.

这篇关于自定义 Tomcat 时:需要 ServletContext 来配置默认的 servlet 处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆