Software testing skills, JMeter stress testing tutorial, request header automatic signature with X-sign parameter (22)

I. Introduction

The interface request body has a sign signature parameter, and the sign signature is generated by splicing the request parameters and finally md5 encryption after removing the sign parameter from the request body

The previous article put the parameters in front of the sign into the body of the request, and this article continues to talk about the situation of putting the signature parameters in the request header

2. Implementation method

The http request needs to perform signature verification X-Sign, put it in the request header

Content-Type: application/json; charset=UTF-8
X-Sign: 2a76a7d2ec34760afb7f4f3ff5a02ef3

body传参:{“username”: “am”, “password”: “123456”, “mail”: “”}

The sign is encrypted by md5 of the value generated by the request body after splicing and sorting + "key"

For specific signature rules, please refer to the previous article: https://blog.csdn.net/x2waiwai/article/details/122843324

Let’s talk about the overall idea and implementation method. In the BeanShell preprocessor, first obtain the header of the request, get the signature value and add it to the header of the request, and then send a new request header

The overall implementation idea is as follows
1. First obtain the request headers value

2. Obtain the sign value of the signature (refer to the previous one for details, and the implementation of the signature will not be discussed here)

3. Add signature parameters and corresponding values ​​to the request header

4. Send a new request header

3. Add BeanShell preprocessor

Http header manager, you don't need to add X-sign parameters

Add-preprocessor-BeanShell preprocessor gets the sign value and adds it to the request header according to the signature rules. The implementation code is as follows

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.control.Header;
import org.apache.jmeter.testelement.property.CollectionProperty;


//sampler 里面有个getHeaderManager 可以获得请求头
HeaderManager headers =sampler.getHeaderManager();

// 打印全部的头部内容
log.info(headers.getHeaders().getStringValue());

// 中间省略签名拿到的sign值,这里给固定的"aaaaaaaaaaaaaaaaaa"

// new 一个Header对象
signHeader = new Header("X-sign","aaaaaaaaaaaaaaaaaa");

// 添加 Header 到请求头管理器
headers.add(signHeader);

// 打印全部的头部内容
log.info(headers.getHeaders().getStringValue());

The sign value obtained by omitting the signature in the middle of the code

Here is a fixed "aaaaaaaaaaaaaaaaaaa", the signature value is implemented according to the development document, or you can find a jar package for development

In the printed log content, you can see the comparison before and after adding X-sign

INFO o.a.j.u.BeanShellTestElement: [Content-Type    application/json]
INFO o.a.j.u.BeanShellTestElement: [Content-Type    application/json, X-sign    aaaaaaaaaaaaaaaaaa]

 View the number of results and see that the request header has already brought X-sign

Guess you like

Origin blog.csdn.net/NHB456789/article/details/131534737