springboot tomcat配置_Spring Boot项目如何同时支持HTTP和HTTPS协议

59eec3d2d3f077259b3473c5bb6f6c90.png
本文首发于个人网站:Spring Boot项目如何同时支持HTTP和HTTPS协议

如今,企业级应用程序的常见场景是同时支持HTTP和HTTPS两种协议,这篇文章考虑如何让Spring Boot应用程序同时支持HTTP和HTTPS两种协议。

准备

为了使用HTTPS连接器,需要生成一份Certificate keystore,用于加密和机密浏览器的SSL沟通。

如果你使用Unix或者Mac OS,可以通过下列命令:keytool -genkey -alias tomcat -keyalg RSA,在生成过程中可能需要你填入一些自己的信息,例如我的机器上反馈如下:

70d067bb2afd5f4a0aad0c8f671fa30f.png

可以看出,执行完上述命令后在home目录下多了一个新的.keystore文件。

实战

  • 首先在resources目录下新建一个配置文件tomcat.https.properties,用于存放HTTPS的配置信息;
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
  • 然后在WebConfiguration类中创建一个静态类TomcatSslConnectorProperties
@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);
        }
    }
}
  • 通过注解加载tomcat.https.properties配置文件,并与TomcatSslConnectorProperties绑定,用注解修饰WebConfiguration类;
@Configuration
@PropertySource("classpath:/tomcat.https.properties")
@EnableConfigurationProperties(WebConfiguration.TomcatSslConnectorProperties.class)
public class WebConfiguration extends WebMvcConfigurerAdapter {...}
  • 在WebConfiguration类中创建EmbeddedServletContainerFactory类型的Srping bean,并用它添加之前创建的HTTPS连接器。
@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启动应用程序;
  • 在浏览器中访问URLhttps://localhost:8443/internal/tomcat.https.properties

da25c43d87425117be51cddd71358d2e.png
  • 在浏览器中访问URLhttp://localhost:8080/internal/application.properties

0f0140c5743f0691f9bf6702ff7b1fa9.png

分析

根据之前的文章和官方文档,Spring Boot已经对外开放了很多服务器配置,这些配置信息通过Spring Boot内部的ServerProperties类完成绑定,若要参考Spring Boot的通用配置项,请点击这里

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文件中的属性。

使用@EnableConfigurationProperties(WebConfiguration.TomcatSslConnectorProperties.class)让Spring Boot自动创建一个属性对象,包含上述通过@PropertySource导入的属性。

在属性值导入内存,并构建好TomcatSslConnectorProperties实例后,需要创建一个EmbeddedServletContainerFactory类型的Spring bean,用于创建EmbeddedServletContainer。

通过createSslConnector方法可以构建一个包含了我们指定的属性值的连接器,然后通过tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));设置tomcat容器的HTTPS连接器。

参考资料

  1. 配置SSL

Spring Boot 1.x系列

  1. Spring Boot的自动配置、Command-line-Runner
  2. 了解Spring Boot的自动配置
  3. Spring Boot的@PropertySource注解在整合Redis中的使用
  4. Spring Boot项目中如何定制HTTP消息转换器
  5. Spring Boot整合Mongodb提供Restful接口
  6. Spring中bean的scope
  7. Spring Boot项目中使用事件派发器模式
  8. Spring Boot提供RESTful接口时的错误处理实践
  9. Spring Boot实战之定制自己的starter

本号(javaadu)专注于后端技术、JVM问题排查和优化、Java面试题、个人成长和自我管理等主题,为读者提供一线开发者的工作和成长经验,期待你能在这里有所收获。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot项目配置Tomcat可以通过以下步骤实现: 1. 在pom.xml文件中添加Tomcat依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> ``` 2. 在application.properties文件中配置Tomcat端口号: ``` server.port=808 ``` 3. 在启动类中添加@EnableAutoConfiguration注解: ``` @SpringBootApplication @EnableAutoConfiguration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 4. 打包项目并运行: ``` mvn clean package java -jar target/myproject.jar ``` 以上就是Spring Boot项目配置Tomcat的简单步骤。 ### 回答2: Spring Boot 是一个基于 Spring 框架的快速开发框架,能够非常简便地创建独立的、生产级别的、基于 Spring 的应用程序。当需要将 Spring Boot 应用程序部署到生产环境中时,我们可以将其打包成 war 包或 jar 包,再配置 Tomcat 作为应用程序的 Web 服务器。 首先,在 pom.xml 文件中引入 Spring Boot 的 web 运行时依赖项,即: ``` <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` 然后,在应用程序的主类上添加注解 `@EnableAutoConfiguration` 和 `@SpringBootApplication`。如下: ``` @SpringBootApplication @EnableAutoConfiguration public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 接着,我们可以在 `application.properties` 或 `application.yml` 中进行相关配置,例如: ``` server.port=8081 server.context-path=/myapp ``` 以上配置Tomcat 服务器端口号设置为 8081,同时将应用程序部署到 http://localhost:8081/myapp。 最后,使用 Maven 命令将应用程序打包成 war 包,例如: ``` mvn clean package ``` 打包完成后,将 war 包复制到 Tomcat 服务器的 webapps 目录下,并启动 Tomcat 即完成项目的部署。 总结:Spring Boot 配置 Tomcat 可以通过引入 web 运行时依赖项、添加启动注解、配置服务器相关属性等操作,最终通过 Maven 命令打包成 war 包部署到 Tomcat 服务器中。 ### 回答3: Spring Boot是一个非常流行的Java Web应用程序框架,它简化了开发者在创建和部署Web应用程序时所需的步骤。Spring Boot支持使用嵌入式web服务器(如Tomcat,Jetty,Undertow等)运行应用程序,也支持将应用程序打成war包并在外部web服务器上运行。本文将详细介绍如何在Spring Boot项目配置Tomcat服务器。 1.添加Tomcat依赖 在Spring Boot项目的pom.xml文件中添加Tomcat依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> ``` 2.配置Tomcat连接器 在application.properties文件中配置Tomcat连接器: ``` server.port=8080 server.tomcat.max-connections=200 server.tomcat.max-threads=100 server.tomcat.uri-encoding=UTF-8 ``` 上述配置中,我们指定了Tomcat服务器的端口、最大连接数、最大线程数、编码方式。 3.构建war包 如果想要将应用程序部署到外部Tomcat服务器上,则需要将应用程序打成war包。在pom.xml文件中添加如下配置: ``` <packaging>war</packaging> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 上述配置中,我们指定了打包方式为war,并添加了spring-boot-maven-plugin插件来打包应用程序。 4.部署war包 将打包好的war包复制到外部tomcat服务器的webapps目录下,并启动Tomcat服务器,即可部署应用程序。 最后,通过以上步骤我们已经成功配置Tomcat服务器并将应用程序部署到了外部Tomcat服务器上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值