结合这4个博文
【1】https://blog.csdn.net/lluozh2015/article/details/79133892
【2】https://blog.csdn.net/pj598707063/article/details/81746222
【3】https://blog.csdn.net/yx0628/article/details/78421017
【4】https://blog.csdn.net/yu829/article/details/20240401
【温馨提示】建议小伙伴们自己先看下keytool工具使用,看懂了,下面的配置就相当容易好理解了。上面4篇博文也都是很好的总结。
自己根据实操,重新梳理了下逻辑
moco 环境搭建完毕,只能使用http请求,无法使用https的请求。下面讲解https证书的生成和使用,让moco能够支持https请求的mock
1.https服务启动命令:
java -jar moco-runner-1.0.0-standalone.jar https -p 443 -c api_config.json --https C:\Users\chengpeisheng\.keystore\chengpeisheng.cer --cert mocohttps --keystore mocohttps
参数讲解:
C:\Users\chengpeisheng\.keystore\chengpeisheng.cer【https的证书路径】
--cer mocohttps【密钥库密码】
--keystore mocohttps【密钥密码】
通过上面的https命令我们可以知道,我们没有https的证书,因此首要任务是生成https的证书。
2.生成.keystore文件夹
在C:\Users\chengpeisheng路径下,进入cmd命令,命令行输入:md .keystore (注意:密钥库一般默认放在这个地方,所以在这个一定要有个.keystore文件夹,没有就自己新建)
3.生成https证书文件(可以是cer或者jks格式)
keytool -genkey -alias cps.cer -keyalg RSA -keystore C:\Users\chengpeisheng\.keystore\chengpeisheng.cer -keysize 1024 -validity 99999
操作是在命令行里完成的,命令解释:
-alias 别名,每个证书都要起自己独有的名字
-keyalg 如果是RSA算法,那么就是RSA
-keystore 填写完整的keystore文件保存路径已经文件名
-keysize 密钥长度,一般1024
-validity 有效天数
之后会让输入 密钥库的密码,连续输入两遍,这里自行设置即可,然后会要求填写一些项目,姓名,组织机构,城市等等,最后会要求输入 证书 的密码,也是连续输入两遍,与证书库密码一样的话可以直接回车确
JKS密钥库使用专用命令再次尝试:
keytool -importkeystore -srckeystore C:\Users\chengpeisheng\.keystore\chengpeisheng.cer -destkeystore C:\Users\chengpeisheng\.keystore\chengpeisheng.cer -deststoretype pkcs12
操作如下:
这样证书就生成成功,可以去目录查看。操作如下:
4.启动https服务
进入moco-runner-1.0.0-standalone.jar所在目录,然后进入cmd 界面,输入启动https服务的命令
java -jar moco-runner-1.0.0-standalone.jar https -p 443 -c api_config.json --https C:\Users\chengpeisheng\.keystore\chengpeisheng.cer --cert 528938 --keystore 528938
命令解释:
【443】https服务默认开启的是443端口
【C:\Users\chengpeisheng\.keystore\chengpeisheng.cer】cer证书所在路径
【 --cert 528938】密钥库的密码
【--keystore 528938】证书/密钥的密码
5.配置本地hosts文件
然后在本地的hosts文件里设置本地ip对应的域名
6.创建mock接口的json文件
定义自己的api接口
7.最后在postman中输入https的请求,
至此,https证书生成成功,并且能够支持https请求!