后端接口请求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: 9090
  ssl:
    key-store-type: PKCS12
    key-store: classpath:keystore.p12 # classpath: 是一个前缀,用于指示 Spring 从类路径(src/main/resources)中加载资源
    key-store-password: 123456
    key-alias: youralias

6、启动后端项目,使用https调用后端项目的接口

相关推荐
汤愈韬18 小时前
NAT策略
网络协议·网络安全·security·huawei
汤愈韬18 小时前
Full Cone Nat
网络·网络协议·网络安全·security·huawei
今晚务必早点睡19 小时前
系统通信方式实战详解:HTTP、RPC、MQ、WebSocket 各用在什么场景?(附 SDK 示例)
websocket·http·rpc
松涛和鸣19 小时前
49、智能电源箱项目技术栈解析
服务器·c语言·开发语言·http·html·php
汤愈韬20 小时前
NAT ALG (应用层网关)
网络·网络协议·网络安全·security·huawei
ps酷教程1 天前
HttpPostRequestDecoder源码浅析
java·http·netty
汤愈韬1 天前
双向NAT
网络·网络协议·网络安全·security·huawei
*才华有限公司*1 天前
RTSP视频流播放系统
java·git·websocket·网络协议·信息与通信
寻星探路1 天前
【Python 全栈测开之路】Python 基础语法精讲(一):常量、变量与运算符
java·开发语言·c++·python·http·ai·c#
栗子叶1 天前
网页接收服务端消息的几种方式
前端·websocket·http·通信