本文首发于我的网站: Spring Boot项目如何同时支持HTTP和HTTPS协议
现在,企业级应用程序的常见场景是同时支持HTTP和HTTPS两种协议,这篇文章考虑如何让Spring Boot应用程序同时支持HTTP和HTTPS两种协议。html
为了使用HTTPS链接器,须要生成一份Certificate keystore,用于加密和机密浏览器的SSL沟通。java
若是你使用Unix或者Mac OS,能够经过下列命令:keytool -genkey -alias tomcat -keyalg RSA
,在生成过程当中可能须要你填入一些本身的信息,例如个人机器上反馈以下:git
能够看出,执行完上述命令后在home目录下多了一个新的.keystore文件。面试
custom.tomcat.https.port=8443 custom.tomcat.https.secure=true custom.tomcat.https.scheme=https custom.tomcat.https.ssl=true custom.tomcat.https.keystore=${user.home}/.keystore custom.tomcat.https.keystore-password=changeit
@ConfigurationProperties(prefix = "custom.tomcat.https") public static class TomcatSslConnectorProperties { private Integer port; private Boolean ssl = true; private Boolean secure = true; private String scheme = "https"; private File keystore; private String keystorePassword; //这里为了节省空间,省略了getters和setters,读者在实践的时候要加上 public void configureConnector(Connector connector) { if (port != null) { connector.setPort(port); } if (secure != null) { connector.setSecure(secure); } if (scheme != null) { connector.setScheme(scheme); } if (ssl != null) { connector.setProperty("SSLEnabled", ssl.toString()); } if (keystore != null && keystore.exists()) { connector.setProperty("keystoreFile", keystore.getAbsolutePath()); connector.setProperty("keystorePassword", keystorePassword); } } }
@Configuration @PropertySource("classpath:/tomcat.https.properties") @EnableConfigurationProperties(WebConfiguration.TomcatSslConnectorProperties.class) public class WebConfiguration extends WebMvcConfigurerAdapter {...}
@Bean public EmbeddedServletContainerFactory servletContainer(TomcatSslConnectorProperties properties) { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); tomcat.addAdditionalTomcatConnectors(createSslConnector(properties)); return tomcat; } private Connector createSslConnector(TomcatSslConnectorProperties properties) { Connector connector = new Connector(); properties.configureConnector(connector); return connector; }
mvn spring-boot:run
启动应用程序;https://localhost:8443/internal/tomcat.https.properties
http://localhost:8080/internal/application.properties
根据以前的文章和官方文档,Spring Boot已经对外开放了不少服务器配置,这些配置信息经过Spring Boot内部的ServerProperties类完成绑定,若要参考Spring Boot的通用配置项,请点击这里spring
Spring Boot不支持经过application.properties同时配置HTTP链接器和HTTPS链接器。在 官方文档70.8中提到一种方法,是将属性值硬编码在程序中。
所以咱们这里新建一个配置文件tomcat.https.properties来实现,可是这并不符合“Spring Boot风格”,后续有可能应该会支持“经过application.properties同时配置HTTP链接器和HTTPS链接器”。我添加的TomcatSslConnectorProperties是模仿Spring Boot中的ServerProperties的使用机制实现的,这里使用了自定义的属性前缀custom.tomcat而没有用现有的server.前缀,由于ServerProperties禁止在其余的配置文件中使用该命名空间。后端
@ConfigurationProperties(prefix = "custom.tomcat.https")这个注解会让Spring Boot自动将custom.tomcat.https开头的属性绑定到TomcatSslConnectorProperties这个类的成员上(确保该类的getters和setters存在)。值得一提的是,在绑定过程当中Spring Boot会自动将属性值转换成合适的数据类型,例如custom.tomcat.https.keystore的值会自动绑定到File对象keystore上。浏览器
使用@PropertySource("classpath:/tomcat.https.properties")来让Spring Boot加载tomcat.https.properties文件中的属性。tomcat
使用@EnableConfigurationProperties(WebConfiguration.TomcatSslConnectorProperties.class)让Spring Boot自动建立一个属性对象,包含上述经过@PropertySource导入的属性。服务器
在属性值导入内存,并构建好TomcatSslConnectorProperties实例后,须要建立一个EmbeddedServletContainerFactory类型的Spring bean,用于建立EmbeddedServletContainer。app
经过createSslConnector方法能够构建一个包含了咱们指定的属性值的链接器,而后经过tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));设置tomcat容器的HTTPS链接器。
本号专一于后端技术、JVM问题排查和优化、Java面试题、我的成长和自我管理等主题,为读者提供一线开发者的工做和成长经验,期待你能在这里有所收获。