Postman实现SHA256withRSA签名
2024-09-01 23:03:48
@
获取pmlib
引入依赖bundle.js,有以下两种方式:
- 从github下载postman collection ,并导入进你的集合里。
- 将所需js全部复制保存成一个全局变量如:pmlib_code
- 把自己的私钥设置成环境变量如:pri_key
使用Pre-request Script对请求进行加签(具体加签字段请看自己项目)
// 使用eval执行js
eval(pm.globals.get('pmlib_code'))
// 生成rfctime
let date = new Date()
let y = date.getFullYear()
let m = date.getMonth()+1<10?'0'+(date.getMonth()+1):(date.getMonth()+1)
let d = date.getDate()<10?'0'+date.getDate():date.getDate()
let hh = date.getHours()<10?'0'+date.getHours():date.getHours();
let mm = date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes()
let ss = date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds()
this.rfc_time = y +'-' + m + '-' + d + ' ' + hh + ':' + mm + ':' + ss
this.rfc_time = this.rfc_time.replace(/\s+/g, 'T')+'+08:00'
pm.variables.set('rfctime',this.rfc_time)
// console.log(pm.variables.get('rfctime'))
const privkey = pm.environment.get('pri_key').replace(/\\n/g, "\n")
// 随机字符串
const uuid = pm.variables.replaceIn('{{$randomUUID}}')
pm.variables.set('nonce_str', uuid)
const requestBodyRaw = pm.variables.replaceIn(pm.request.body == undefined ? '' : pm.request.body.raw)
const now = pm.variables.replaceIn('{{$timestamp}}')
pm.variables.set('req_time', now)
// 具体加密字段拼接请依据项目情况案例是:method+\n+url+\n+timestamp+\n+nonce_str+\n+body
var dataToSign = pm.request.method + "\n" +
pm.request.url.getPathWithQuery() + "\n" +
now + "\n" +
uuid + "\n" +
requestBodyRaw
console.log(dataToSign)
const sha256withrsa = new pmlib.rs.KJUR.crypto.Signature({"alg": "SHA256withRSA"});
sha256withrsa.init(privkey);
sha256withrsa.updateString(dataToSign);
const sign = pmlib.rs.hextob64(sha256withrsa.sign());
// console.log(sign);
pm.variables.set('sign', sign)
// 添加请求头
pm.request.headers.add({
key:"Authorization",
value:"SHA256-RSA nonce_str={{nonce_str}},timestamp={{req_time}},signature={{sign}}"
});
至此SHA256withRSA签名已完成
最新文章
- C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 角色成员功能的改进支持公司加入到角色
- 交叉编译mips平台上valgrind
- The Dragon of Loowater
- Java中HashMap的数据结构
- iOS开发——动画编程Swift篇&;(四)CABasicAnimation动画
- 【阿里云产品公测】开放搜索服务 opensearch java jdk 应用体验之 机器人聊天
- 【HDOJ】4504 威威猫系列故事——篮球梦
- 关于close和shutdown
- 春招实习面经分享(已拿到腾讯春招Offer)
- Gradle 1.12 翻译——第十二章 使用Gradle 图形用户界面
- 检查Json格式工具
- SQL server存储过程,触发器,游标相关实例
- .NET Core[MVC] 利用特性捕捉异常
- JAVA学习笔记 (okHttp3的用法)
- PyAutoGUI——让所有GUI都自动化
- QT心电图设计
- Ping--域名替换重要角色
- redis+thinkphp5的注册、登陆、关注基础例子
- Qt5 中文乱码问题
- VMware虚拟机配置端口转发(端口映射),实现远程访问【转】