JMeter-BeanShell之处理签名接口校验


前言

很多HTTP接口在传参时,需要先对接口的参数进行数据签名加密

如pinter项目的中的签名接口 http://localhost:8080/pinter/com/userInfo

参数为: {"phoneNum":"123434","optCode":"testfan","timestamp":"1211212","sign":"fdsfdsaafsasfas"} 其中,sign字段是按照特定算法进行加密后的数据

本接口的签名算法为 sign=Md5(phoneNum+ optCode+ timestamp)

对于这样的接口,小伙伴们怎么利用Jmeter工具进行测试呢?

一:添加签名接口http,写入JSON格式参数,添加HTTP信息头管理器,phoneNum和timestamp的值需要参数化

二:调用加密的jar包或java文件

三:添加后置处理器-BeanShell PreProcessor,写入以下代码

import com.lee.util.Md5Util;
// 生成随机手机号 
String phone = "135${__Random(10000000,99999999,phone)}";
String code = "testfan"; 
// 生成时间戳 
String time = "${__time(,myTime)}"; 
// 调用外部函数进行加密 
String md5 = Md5Util.getMd5Hex(phone+code+time); 
// 将数据另存为新的变量 
vars.put("md5",md5);

四:添加监听器-察看结果树,点击运行,查看响应结果

附:加密的java代码如下:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
 * Created by li on 16/5/25.
 */
public class Md5Util {
    public static String getMd5Hex(String str)
    {
        MessageDigest md5;
        try
        {
            md5 = MessageDigest.getInstance("md5");
            byte[] by = md5.digest(str.getBytes());

            return byteToHex(by).toLowerCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace(); }
        return null;
    }

    private static String byteToHex(byte[] by)
    {
        String[] hexs = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
        StringBuffer hexMd5 = new StringBuffer();
        for (int i = 0; i < by.length; ++i) {
            int b = by[i];
            if (b < 0)
            {
                b += 256;
            }
            int h1 = b / 16;
            int h2 = b % 16;
            hexMd5.append(hexs[h1]);
            hexMd5.append(hexs[h2]);
        }
        return hexMd5.toString();
    }
    public static void main(String[] args){
        System.out.println(getMd5Hex("abc"));
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM