记坑----springboot1.5和springboot2.0的坑(2)tomcat相关
1.TomcatEmbeddedServletContainerFactory类被移除EmbeddedServletContainerAutoConfiguration是嵌入式Servlet容器的自动配置类,该类在spring-boot-autoconfigure-xxx.jar中的web模块可以找到
2.0x后通过下面的类来实现 ServletWebServerFactory
2.https的操作改变
1.5的配置如下:
@Bean public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
return tomcat;
}
/**
* 让我们的应用支持HTTP是个好想法,但是需要重定向到HTTPS,
* 但是不能同时在application.properties中同时配置两个connector, 所以要以编程的方式配置HTTP
* connector,然后重定向到HTTPS connector
*
* @Return Connector
*/
private Connector initiateHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080); // http端口
connector.setSecure(false);
connector.setRedirectPort(7443); // application.properties中配置的https端口
return connector;
}
2.0的代码如下:
@Bean public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createSslConnector());
return tomcat;
}
private Connector createSslConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
try {
File truststore = new File("/Users/liaokailin/software/ca1/keystore");
connector.setScheme("https");
protocol.setSSLEnabled(true);
connector.setSecure(true);
connector.setPort(8443);
protocol.setKeystoreFile(truststore.getAbsolutePath());
protocol.setKeystorePass("123456");
protocol.setKeyAlias("springboot");
return connector;
} catch (Exception ex) {
throw new IllegalStateException("cant access keystore: [" + "keystore" + "]", ex);
}
}
页:
[1]