我有一个已经运行的简单的Spring Boot应用程序:我可以卷曲以击中端点,等等。
我正在尝试根据Spring Boot参考指南(第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
我验证了属性是否正确输入该方法,并尝试按照此问题的建议添加/删除@WebAppConfiguration
我的gradle文件正在使用spring-boot-starter-web:1.0.0.RELEASE。
问题是:如何使用Spring Boot正确配置https?当我不尝试配置tomcat时,为什么我的应用程序能工作,而当我尝试配置tomcat时却中断了?
谢谢
发布此问题后,我发现了答案:
有关如何使用Spring Boot正确配置https的方法:上面列出的代码最初是添加到我的@Configuration SecurityConfig中的,它扩展了WebSecurityConfigurerAdapter。当我将tomcat定制移至其自己的@Configuration类时,它起作用了。
原因:大概在应用程序上下文生命周期中,WebSecurityConfigurerAdapter的调用早于@Configurations的其余部分。