1.TomcatEmbeddedServletContainerFactory类被移除
EmbeddedServletContainerAutoConfiguration是嵌入式Servlet容器的自动配置类,该类在spring-boot-autoconfigure-xxx.jar中的web模块可以找到
2.0x后通过下面的类来实现 ServletWebServerFactory
2.https的操作改变
1.5的配置如下:
[Java] 纯文本查看 复制代码 [url=home.php?mod=space&uid=237509]@Bean[/url] 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
*
* [url=home.php?mod=space&uid=155549]@Return[/url] 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的代码如下:
[Java] 纯文本查看 复制代码 @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);
}
} |