Spring Boot 支持 HTTPS 如此简单,So easy!
这里讲的是 Spring Boot 内嵌式 Server 打 jar 包运行的方式,打 WAR 包部署的就不存在要 Spring Boot 支持 HTTPS 了,需要去外部对应的 Server 配置。
你所需具备的基础
更多请在Java技术栈微信公众号后台回复关键字:boot。
支持 HTTPS
Spring Boot 配置 SSL 很简单,只需要通过一系列的 server.ssl.* 参数即可完成配置,如下所示。
application.properties 配置文件参考配置:
server.port=8443server.ssl.protocol=TLSserver.ssl.key-store=classpath:javastack.keystoreserver.ssl.key-store-password=javastackserver.ssl.key-store-type=JKS8443
server.ssl.protocol=TLS
server.ssl.key-store=classpath:javastack.keystore
server.ssl.key-store-password=javastack
server.ssl.key-store-type=JKS
如何在本地测试创建证书请参考这篇文章《一分钟开启Tomcat https支持》,把生成完的证书复制到 Spring Boot 项目中的 resources 目录即可。
这边只是提供了一个 SSL 单向验证的演示,更多 SSL 参数配置如下。
server.ssl.ciphers= # Supported SSL ciphers.server.ssl.client-auth= # Whether client authentication is wanted ("want") or needed ("need"). Requires a trust store.server.ssl.enabled= # Enable SSL support.server.ssl.enabled-protocols= # Enabled SSL protocols.server.ssl.key-alias= # Alias that identifies the key in the key store.server.ssl.key-password= # Password used to access the key in the key store.server.ssl.key-store= # Path to the key store that holds the SSL certificate (typically a jks file).server.ssl.key-store-password= # Password used to access the key store.server.ssl.key-store-provider= # Provider for the key store.server.ssl.key-store-type= # Type of the key store.server.ssl.protocol=TLS # SSL protocol to use.server.ssl.trust-store= # Trust store that holds SSL certificates.server.ssl.trust-store-password= # Password used to access the trust store.server.ssl.trust-store-provider= # Provider for the trust store.server.ssl.trust-store-type= # Type of the trust store.
server.ssl.client-auth= # Whether client authentication is wanted ("want") or needed ("need"). Requires a trust store.
server.ssl.enabled= # Enable SSL support.
server.ssl.enabled-protocols= # Enabled SSL protocols.
server.ssl.key-alias= # Alias that identifies the key in the key store.
server.ssl.key-password= # Password used to access the key in the key store.
server.ssl.key-store= # Path to the key store that holds the SSL certificate (typically a jks file).
server.ssl.key-store-password= # Password used to access the key store.
server.ssl.key-store-provider= # Provider for the key store.
server.ssl.key-store-type= # Type of the key store.
server.ssl.protocol=TLS # SSL protocol to use.
server.ssl.trust-store= # Trust store that holds SSL certificates.
server.ssl.trust-store-password= # Password used to access the trust store.
server.ssl.trust-store-provider= # Provider for the trust store.
server.ssl.trust-store-type= # Type of the trust store.
参数对应的类:org.springframework.boot.web.server.Ssl
上面的例子配置后就能开启 HTTPS 了,默认的 HTTP 协议就不再支持了,Spring Boot 不支持以配置文件配置的方式同时支持 HTTP 和 HTTPS。
如何同时支持?
如果你需要同时支持 HTTP 和 HTTPS 这两个协议,就需要把另外一个协议用程序化的方式来配置。
因为通过程序的方式配置 HTTP 协议更加简单一点,所以,Spring Boot 推荐的做法是把 HTTPS 配置在配置文件,HTTP 通过程序来配置。
来,下面示例就是通过程序的方式来额外支持 HTTP 协议。
@SpringBootApplicationpublic class JavastackApplication { @Bean public ServletWebServerFactory servletContainer() { TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(); tomcat.addAdditionalTomcatConnectors(createStandardConnector()); return tomcat; } private Connector createStandardConnector() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setPort(8080); return connector; } public static void main(String[] args) { SpringApplication.run(JavastackApplication.class, args); }}
public class JavastackApplication {
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createStandardConnector());
return tomcat;
}
private Connector createStandardConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(8080);
return connector;
}
public static void main(String[] args) {
SpringApplication.run(JavastackApplication.class, args);
}
}
启动 Spring Boot 之后就会看到下面的同时支持两个协议日志。
Tomcat started on port(s): 8443 (https) 8080 (http) with context path '/'on port(s): 8443 (https) 8080 (http) with context path '/'
Spring Boot 支持 HTTPS 如此简单,开发现在把运维的事都做了……
好了,今天的分享就到这里,更多 Spring Boot 文章正在撰写中,关注Java技术栈微信公众号获取第一时间推送。
在Java技术栈微信公众号后台回复:boot,还能获取栈长整理的往期 Spring Boot 教程,都是实战干货,以下仅为部分预览。
最近干货分享
点击「阅读原文」一起搞技术,爽~
Spring Boot 支持 HTTPS 如此简单,So easy!的更多相关文章
- Spring Boot 支持 HTTPS 如此简单,So easy!
这里讲的是 Spring Boot 内嵌式 Server 打 jar 包运行的方式,打 WAR 包部署的就不存在要 Spring Boot 支持 HTTPS 了,需要去外部对应的 Server 配置. ...
- Spring Boot 支持 Https 有那么难吗?
https 现在已经越来越普及了,特别是做一些小程序或者公众号开发的时候,https 基本上都是刚需了. 不过一个 https 证书还是挺费钱的,个人开发者可以在各个云服务提供商那里申请一个免费的证书 ...
- Spring Boot 支持https
1. 生成key JDK下 keytool -genkeypair -alias mySSL -keyalg RSA -keystore E:\tomcat.key 其中-alias是证书的别名,RS ...
- Spring Boot 支持多种外部配置方式
Spring Boot 支持多种外部配置方式 http://blog.csdn.net/isea533/article/details/50281151 这些方式优先级如下: 命令行参数 来自java ...
- Spring Boot开发HTTPS协议的REST接口
Spring Boot开发HTTP的REST接口流程在前文中已经描述过,见<SpringBoot开发REST接口>. 如需要支持HTTPS,只需要在如上基础上进行设置.修改/resourc ...
- spring boot: 支持jsp,支持freemarker
spring boot: 支持jsp,支持freemarker 支持jsp: 加入依赖 <!--jsp--> <dependency> <groupId>org.a ...
- spring boot: @Entity @Repository一个简单的数据读存储读取
spring boot: @Entity @Repository一个简单的数据读存储读取 创建了一个实体类. 如何持久化呢?1.使用@Entity进行实体类的持久化操作,当JPA检测到我们的实体类当中 ...
- Spring Boot SSL [https]配置例子
前言 本文主要介绍Spring Boot HTTPS相关配置,基于自签证书实现: 通过本例子,同样可以了解创建SSL数字证书的过程: 本文概述 Spring boot HTTPS 配置 server. ...
- 程序配置的原则和实践以及 Spring Boot 支持方式
原则 软件需要在不同的环境中部署,代码是保持不变的,但是不同的运行环境存在差异,所以需要使用配置适应不同的环境.比如: 数据库,Redis,以及其他 后端服务 的配置: 第三方服务的证书,如 oAut ...
随机推荐
- JS 函数 学习笔记
函数是一段可以反复调用的代码块.函数还能接受输入的参数,不同的参数会返回不同的值 声明函数的 5 种方式 具名函数 (function 命令) function f(x, y){ return x + ...
- BUUCTF--reverse2
测试文件:https://buuoj.cn/files/ef0881fc76e5bcd756b554874ef99bec/e8722e94-93d7-45d5-aa06-a7aa26ce01a1.ra ...
- docker运行模式图
docker运行模式图:
- MySQL--11 备份的原因
目录 一.备份的原因 二.备份的类型 三.备份的方式 四.备份策略 五.备份工具 六.企业故障恢复案例 1.模拟环境 2.模拟恢复数据过程: 一.备份的原因 运维工作的核心简单概括就两件事: 1)第一 ...
- windows10安装nodejs 10和express 4
最进做一个个人博客系统,前端用到了semanticUI,但是要使用npm工具包,所以需要安装nodejs,nodejs自带npm 下载 去官网下载自己系统对应的版本,我的是windows:下载 可以在 ...
- go语言从例子开始之Example37.Go 状态协程
在前面的例子中,我们用互斥锁进行了明确的锁定来让共享的state 跨多个 Go 协程同步访问.另一个选择是使用内置的 Go协程和通道的的同步特性来达到同样的效果.这个基于通道的方法和 Go 通过通信以 ...
- mysql的锁
前言 mysql锁的概念参考如下连接: 1.http://blog.csdn.net/u013063153/article/details/53432468 2.http://www.yesky.co ...
- Linux设备驱动详解 宋宝华 硬件基础
处理器 存储器 接口与总线 I2C时序 SPI总线时序 以太网
- Flutter第三方選擇器組件
调用Flutter的第三方时间选择器组件 上面我介绍了系统给我们提供的日期时间选择器,但是有时候系统提供的选择器并不符合我们的要求,这时我们就可以到pub.dev上去寻找符合我们要求的日期选择器. 这 ...
- Ubunu12.04 vncserver xstartup配置文件
以下有效: #!/bin/sh # Uncomment the following two lines for normal desktop: unset SESSION_MANAGER #exec ...