后端接口请求http改为https
1、使用 OpenSSL 生成自签名证书
在Linxu服务器上执行如下命令:
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
运行此命令后,会提示输入一些信息(如国家、省份、城市、组织名称等),自动生成的 cert.pem 和 key.pem 文件,可以用于配置你的本地服务器。
通常,这些文件需要转换成 Java 可识别的格式(如 PKCS12 格式),因为 Spring Boot 默认支持这种格式
2、转换证书格式,使用 OpenSSL 将 PEM 格式转换为 PKCS12 格式
在Linxu服务器上执行如下命令:
openssl pkcs12 -export -in cert.pem -inkey key.pem -out keystore.p12 -name lzpcert -CAfile cert.pem -caname root
3、将证书文件从服务器传回到本地
在PowerShell或者IDEA的终端窗口执行如下命令:
scp root@11.22.33.44:/root/cert.pem E:\LzpWorkspaces\lzp-records
scp root@11.22.33.44:/root/key.pem E:\LzpWorkspaces\lzp-records
scp root@11.22.33.44:/root/keystore.p12 E:\LzpWorkspaces\lzp-records
4、将证书文件移动到后端项目的src/main/resources目录
将证书文件(.pem或.crt)和私钥文件(.key)和keystore.p12文件,移动到后端项目的资源目录(src/main/resources)
5、后端项目新增ssl证书配置
server:port: 9090ssl:key-store-type: PKCS12key-store: classpath:keystore.p12 # classpath: 是一个前缀,用于指示 Spring 从类路径(src/main/resources)中加载资源key-store-password: 123456key-alias: youralias