Springboot 项目配置 HTTPS

发布时间 2023-05-09 11:36:46作者: 杯酒-故人

生成证书

输入命令

keytool -genkeypair -alias "boot" -keyalg "RSA" -keystore "boot.keystore"

生成完成后会提示

Warning:
JKS 密钥库使用专用格式。建议使用 "keytool -importkeystore -srckeystore boot.keystore -destkeystore boot.keystore -deststoretype pkcs12" 迁移到行业标准格式 PKCS12

继续输入命令

keytool -importkeystore -srckeystore boot.keystore -destkeystore boot.keystore -deststoretype pkcs12

查看密钥

注意看密钥库类型是否是 PKCS12

keytool -list -keystore boot.keystore

Springboot 配置

yml

注意是 key-store-password 不是key-password

server:
  ssl:
    key-store: classpath:boot.keystore
    key-store-password: 123456
    key-store-type: PKCS12
    key-alias: boot

pom

如果不配置这里证书文件会找不到

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <encoding>UTF-8</encoding>
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>keystore</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
    </configuration>
</plugin>

至此可以尝试用 https 访问项目