后端接口请求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调用后端项目的接口

相关推荐
小吴-斌3 小时前
本地请求接口报SSL错误解决办法(Could not verify * SSL certificate)
网络·网络协议·ssl
草莓熊Lotso4 小时前
基于容器适配器模式的 Stack 与 Queue 实现:复用底层容器的优雅设计
c++·网络协议·rpc·适配器模式
RollingPin6 小时前
iOS八股文之 网络
网络·网络协议·ios·https·udp·tcp·ios面试
小毛驴85014 小时前
HTTP方法GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,TRACE,RESTful API设计的核心详解
http·github·restful
代码AI弗森18 小时前
无状态的智慧:从 HTTP 到大模型的系统进化论
网络·网络协议·http
酷熊代理19 小时前
安卓手机 IP 切换指南:告别卡顿,轻松换 IP
网络·网络协议·tcp/ip·socks5
不做菜鸟的网工19 小时前
PIM SM +MSDP 组播跨域配置案例
网络协议
大面积秃头21 小时前
Http基础协议和解析
网络·网络协议·http
软件技术员1 天前
使用ACME自动签发SSL 证书
服务器·网络协议·ssl
我也要当昏君1 天前
6.4 电子邮件 (答案见原书 P284)
网络协议