我有一个简单的弹簧启动应用程序已经运行:我可以卷曲到终点等等。
我尝试按照spring boot reference guide (pg 115)将其配置为使用HTTPS,并将此类代码添加到我的@Configuration类中:
@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;
}
然而,只有这个代码添加,我以前工作的应用程序现在获得以下堆栈跟踪:
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
我验证了属性是否正确进入该方法,并尝试按照this question的建议添加/删除@WebAppConfiguration
我的gradle文件使用的是spring-boot-starter-web:1.0.0.RELEASE。
问题是:如何使用spring boot正确配置https?为什么我的应用程序在我不尝试配置tomcat时工作,并在我尝试配置tomcat时中断?
由于
答案 0 :(得分:11)
发布此问题后,我发现了答案:
关于如何使用spring boot正确配置https:上面列出的代码最初添加到我的@Configuration SecurityConfig扩展WebSecurityConfigurerAdapter。当我将tomcat自定义移动到它自己的@Configuration类时,它可以工作。
原因:大概是WebSecurityConfigurerAdapter在应用程序上下文生命周期中比其他@Configurations更早被调用。