Jmeter signs HTTP messages through BeanShell

Time:2019-10-9

1. Increase the pre-beanshell script

Jmeter signs HTTP messages through BeanShell

2. Beanshell script points

  • By introducing external jar packages and referring to relevant signature algorithms, such as rsa, ecdsa, etc.
  • Get the JSON body to send the request through getArgument (0). getValue () of the config. Arguments class. Calling external jar package method to perform Sha and other operations
  • RMV and add operations of HTTP header

3. Beanshell code sample

import com.matrix.common.service.SignatureService;
import org.apache.jmeter.protocol.http.control.Header;
import org.apache.jmeter.config.Arguments;

// Get the signature header
public static String getSign(String contentSHA, String date,String nonce ){
    String privateKey = vars.get("privateKey");
    String method = "GET";
    String accept = vars.get("accept");
    String contentType = vars.get("contentType");
    String nonceHeader = "x-bfs-signature-nonce:"+nonce;
    String url = vars.get("createRfqOrderUrl");
    // Call the signature method of SignatureService in the jar package for signature
    String signature = SignatureService.sign(privateKey,method,accept,contentSHA, contentType, date, nonceHeader, url);
    return  signature;
}

// Adding signature-related header parameters before sending requests
public static void addHeaders( ){
    // Increased Date Head
    String date = (new Date()).toString();
    sampler.getHeaderManager().removeHeaderNamed("Date");
    sampler.getHeaderManager().add(new Header("Date",date));
    // Increase nonce head
    String nonce = "550e8400e29b41d4a716446655440000";
    
    sampler.getHeaderManager().removeHeaderNamed("x-bfs-signature-nonce");
    sampler.getHeaderManager().add(new Header("x-bfs-signature-nonce",nonce));
    // Getting JSON body by getArgument method
    Arguments arguments = sampler.getArguments();
    String content = arguments.getArgument(0).getValue();
    log.info("content="+content);
    // Increase Content-SHA256 Heads
    String contentSHA =      SignatureService.getContentSHA256(content);
    log.info("contentSHA="+contentSHA);
    sampler.getHeaderManager().removeHeaderNamed("Content-SHA256");
    sampler.getHeaderManager().add(new Header("Content-SHA256",contentSHA));
    // Adding signature header
    String signature = getSign(contentSHA,date,nonce);
    sampler.getHeaderManager().removeHeaderNamed("Authorization");
    sampler.getHeaderManager().add(new Header("Authorization","bfs "+ vars.get("accessKeyId") +":"+signature));
    return;
}

addHeaders();

Recommended Today

Chinese NLP processing method to do list

Chinese natural language processing flow Acquisition corpus Corpus is language material. Corpus is the content of linguistic research. Corpus is the basic unit of corpus. Therefore, people simply use the text as a substitute, and take the context in the text as a substitute of the context in the real world. We call a text […]