当前位置: 首页 > news >正文

Postman轻松签名,让SHA256withRSA保驾护航!

前言

在接口测试中,我们经常需要对请求进行签名,以保证数据的安全性。而SHA256withRSA是一种较为常见的签名算法,它可以使用私钥对数据进行签名,使用公钥进行验签。

但是,实现该算法签名可能会涉及到一些繁琐的操作,给我们的工作带来不小的困扰。

今天,我要向大家介绍一个神器——Postman,它可以轻松完成SHA256withRSA签名的实现,让您的API请求得到更加完善的保护。

接下来,我将简单介绍如何使用Postman实现SHA256withRSA签名,并且分享一些注意事项和技巧,希望能让大家轻松掌握这个技能。

获取pmlib

引入依赖bundle.js,有以下两种方式:
  1. 从github下载postman collection ,并导入进你的集合里
    官方使用介绍

 

  1. 将所需js所需js所需js全部复制保存成一个全局变量如:pmlib_code
    在这里插入图片描述
  2. 把自己的私钥设置成环境变量如: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" +requestBodyRawconsole.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}}"
});

 使用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" +requestBodyRawconsole.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签名已完成
在这里插入图片描述

 感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

 

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取 

 

http://www.lryc.cn/news/301683.html

相关文章:

  • C#面:简述装箱和拆箱
  • 【Kubernetes in Action笔记】1.快速开始
  • 踩坑实录(Fourth Day)
  • 【python】网络爬虫与信息提取--requests库
  • 洛谷 P8627 [蓝桥杯 2015 省 A] 饮料换购
  • Academic Inquiry|投稿状态分享(ACS,Wiley,RSC,Elsevier,MDPI,Springer Nature出版社)
  • 1+X运维试题样卷C卷(初级)
  • Spring学习笔记(二)Spring的控制反转(设计原则)与依赖注入(设计模式)
  • MySQL 基础知识(四)之表操作
  • 计算机网络——10FTP
  • javascript中的this指向
  • WebServer 之 http连接处理(下)
  • Android电量相关知识
  • 【Java多线程】线程中几个常见的属性以及状态
  • 鸿蒙OS跨进程IPC与RPC通信
  • Effective Objective-C 学习(三)
  • 蓝桥杯备赛攻略
  • react反向代理
  • 债券专题二:可转债估值-二叉树模型
  • 【闲谈】开源软件的崛起与影响
  • 【教程】Linux使用aria2c多线程满速下载
  • 【漏洞复现】蓝网科技临床浏览系统信息泄露漏洞
  • JSON转换List<Map<String, Object>>、Map<String, Object>
  • 单主模式和多主模式切换
  • petalinux2018.3安装步骤
  • ubuntu22.04下使用conda安装pytorch(cpu及gpu版本)
  • 突破编程_C++_高级教程(模板编程的基础知识)
  • 胆小勿入!AI创作恐怖电影宣传片《生化危机:重生》
  • HTTP 超文本传送协议
  • MySQL导入/导出数据