环境说明:
Spring Boot 搭建服务器环境
Spring Boot 嵌套的tomcat
需要准备工作:
SSL证书, https中必备证书
springboot 中启动https
获取SSL证书
主要有两种方式
1 自己通过keytool 生成, 本次使用该方式.
2 通过证书授权机构购买
采用 keytool 生成
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
操作如下:
Enter keystore password:
Re-enter new password:
What is your first and last name?
[Unknown]:
What is the name of your organizational unit?
[Unknown]:
What is the name of your organization?
[Unknown]:
What is the name of your City or Locality?
[Unknown]:
What is the name of your State or Province?
[Unknown]:
What is the two-letter country code for this unit?
[Unknown]:
Is CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct?
[no]: yes
这个时候, 就生成了一个 keystore.p12的证书, 这个放到对应项目中的资源文件中, 后面会使用到
上面的密码为: mypassword
Spring boot 中开启Https
需要注意的是: http 和 https 不能同事配置到配置文件中, 所以这里先处理https.
在配置文件里面配置
server.port: 8443
server.ssl.key-store: classpath:keystore.p12
server.ssl.key-store-password: mypassword
server.ssl.keyStoreType: PKCS12
server.ssl.keyAlias: tomcat
配置完成, 运行没有错误, 就代表着https已经可以使用.
使用 https://localhost:8443 默认会404