The simple use of jmeter and the solution to the problem that the Http Cookie manager cannot obtain cookies under cross-domain

jmeter download address: https://jmeter.apache.org/download_jmeter.cgi

Unzip the compressed file and enter the bin directory. Double-click ApacheJMeter.jar (the client is Chinese) or jmeter.bat to start jmeter (jmeter depends on java8)

jmeter supports many test types, the following only briefly introduces the web http test

After opening jmeter, add a thread group under the test plan

As shown below

Add an http request under the thread group

As follows (login interface)

Add an observation tree under the login interface

as follows

Execute (you will be prompted to save first if not saved)

View the observation tree

Some interfaces can only be accessed after logging in to obtain the JSESSIONID in the cookie.

jmeter can manage cookies through Http Cookie Manager, so that other visits will automatically bring cookies without manual setting

Add a cookie manager under the thread group

as follows

 

Add an interface under the thread group that needs to be logged in to access

Note that the server name or ip above must be the same as the server or name of the login interface, not one is localhost and the other is 127.0.0.1. Otherwise, getting the cookie will fail

Add an observation result tree under the interface after login

For the convenience of testing, I return a custom cookie under the login interface. as follows

@RequestMapping("/login")
    public Result login(HttpServletRequest request , HttpServletResponse response, @Validated UserDto dto, BindingResult result) throws IOException {
        if(result.getErrorCount() >= 1){
            return Result.fail(result.getAllErrors().get(0).getDefaultMessage());
        }
        UserVo userVo = dto.toUserVo();
        UserVo queryResult = iUserMicroService.findUserByUserName(userVo.getUserName());
        if(queryResult == null){
            return Result.fail("该用户没有注册");
        }
        String password = DigestUtils.md5Hex(userVo.getPassword());
        if(!queryResult.getPassword().equals(password)){
            return Result.fail("密码不正确");
        }
        Cookie cookie = new Cookie("wl","hello");
        cookie.setMaxAge(3600);
        cookie.setPath("/");
        response.addCookie(cookie);
        sessionService.setUserSession(request,queryResult);
        return Result.success(queryResult);
    }

Performed at this time. View the request header information of the observation result tree of the interface after login

You can see that there are two cookies on the request header

Http Cookie Manager cannot obtain cookies in single sign-on or cross-domain situations

In order to facilitate the test, we simulate cross-domain, directly modify the server or ip of the login interface to: localhost. The server or ip value of the interface after login is still 127.0.0.1

Execute again

Successful login interface

But the result after login failed

The request header information is as follows (no cookie)

To solve this problem, you can use the jemeter BeanShell post processor

Add a post processor under the login interface

Write script (log can be removed)

import org.apache.jmeter.samplers.SampleResult;
log.info("=====================");
SampleResult result = ctx.getPreviousResult();
String headers = result.getResponseHeaders();
String[] array = headers.split("\n");
        for(int i=0;i<array.length;i++){
            if(array[i].startsWith("Set-Cookie")){
                String params = array[i].replace("Set-Cookie: ","");
                String[] cookieArray = params.trim().split(";");
                String[] cookieKeyValue =  cookieArray[0].split("=");
                String cookieKey = cookieKeyValue[0];
                String cookieValue = cookieKeyValue[1];
               if(cookieKey.equals("JSESSIONID")){
               	log.info(cookieValue);
               	props.put("JSESSIONID",cookieValue);
               }
            }
        }
log.info(headers);

As shown below

The above script gets the value of JSESSIONID in Cookie and assigns it to the global variable JSESSIONID (name can be customized) in Jemeter

Add a JSESSIONID cookie in the Http Cookie Manager and use ${__P(JSESSIONID,)} to get the global variables assigned above.

As follows (please pay attention to safety and do not check)

Note that the domain must be the same as the value of the server or ip of the interface that needs to be logged in

Execute again

The login interface request header is as follows (the domain name is localhost)

The request header after login is as follows (the domain name is 127.0.0.1 cookie has value)

The jemeter directory is as follows

BeanShell built-in object ctx javadoc  https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html

                              vars javadoc  https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html

                            prev - (SampleResult)  javadoc https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html

                            sampler javadoc https://jmeter.apache.org/api/org/apache/jmeter/samplers/Sampler.html

 

 

Guess you like

Origin blog.csdn.net/name_is_wl/article/details/115023987