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