首页 > 解决方案 > JSR223 Sampler SSL 证书问题

问题描述

我在 JSR223 Sampler 中有以下代码,并且出现 SSL 证书错误。有什么办法可以禁用吗?

JSR223 脚本 JSR223 Sampler 中的问题,消息:javax.script.ScriptException:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX 路径构建失败

import org.apache.http.HttpHeaders;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.StringEntity;


List<String> sendRequest(String url, String method, String body) {


    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(2000)
            .setSocketTimeout(3000)
            .build();

    StringEntity entity = new StringEntity(body, "UTF-8");


    HttpUriRequest request = RequestBuilder.create(method)
            .setConfig(requestConfig)
            .setUri(url)
            .setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8")
            .setEntity(entity)
            .build();

   String req = "REQUEST:" + "\n" + request.getRequestLine() + "\n" + "Headers: " +
            request.getAllHeaders() + "\n" + EntityUtils.toString(entity) + "\n";


    HttpClientBuilder.create().build().withCloseable {httpClient ->

        httpClient.execute(request).withCloseable {response ->

            String res = "RESPONSE:" + "\n" + response.getStatusLine() + "\n" + "Headers: " +
                    response.getAllHeaders() + "\n" +
                    (response.getEntity() != null ? EntityUtils.toString(response.getEntity()) : "") + "\n";

            System.out.println(req + "\n"  + res );

            return Arrays.asList(req, res);
        }
    }
}

List test1 = sendRequest("https://testserver.com","POST", "");
log.info(Arrays.toString(test1));

标签: ssljmeterjsr223

解决方案


根据文档,只需使用普通的HTTP 请求采样器:

JMeter HTTP 采样器被配置为接受所有证书,无论是否受信任,无论有效期等。这是为了在测试服务器时提供最大的灵活性


但是,如果您正在做一些非常特别的事情并且需要在 Groovy 中做同样的事情 - 这是示例解决方案:

import org.apache.http.HttpHeaders
import org.apache.http.client.config.RequestConfig
import org.apache.http.client.methods.HttpUriRequest
import org.apache.http.client.methods.RequestBuilder
import org.apache.http.conn.ssl.NoopHostnameVerifier
import org.apache.http.conn.ssl.SSLConnectionSocketFactory
import org.apache.http.conn.ssl.TrustStrategy
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClients
import org.apache.http.ssl.SSLContextBuilder
import org.apache.http.util.EntityUtils

import java.security.cert.CertificateException
import java.security.cert.X509Certificate

List<String> sendRequest(String url, String method, String body) {


    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(2000)
            .setSocketTimeout(3000)
            .build();

    StringEntity entity = new StringEntity(body, "UTF-8");


    HttpUriRequest request = RequestBuilder.create(method)
            .setConfig(requestConfig)
            .setUri(url)
            .setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8")
            .setEntity(entity)
            .build();

    String req = "REQUEST:" + "\n" + request.getRequestLine() + "\n" + "Headers: " +
            request.getAllHeaders() + "\n" + EntityUtils.toString(entity) + "\n";

    def builder = new SSLContextBuilder();
    builder.loadTrustMaterial(null, new TrustStrategy() {
        @Override
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return true;
        }
    });
    def trustAllFactory = new SSLConnectionSocketFactory(builder.build(), new NoopHostnameVerifier());

    HttpClients.custom().setSSLSocketFactory(trustAllFactory).build().withCloseable { httpClient ->

        httpClient.execute(request).withCloseable { response ->

            String res = "RESPONSE:" + "\n" + response.getStatusLine() + "\n" + "Headers: " +
                    response.getAllHeaders() + "\n" +
                    (response.getEntity() != null ? EntityUtils.toString(response.getEntity()) : "") + "\n";

            System.out.println(req + "\n" + res);

            return Arrays.asList(req, res);
        }
    }
}

List test1 = sendRequest("https://testserver.com", "POST", "");
println(Arrays.toString(test1));

更多信息:


推荐阅读