【问题标题】:Jmeter with basic authentication具有基本身份验证的 Jmeter
【发布时间】:2017-09-11 11:22:59
【问题描述】:

我想在 RestAPI 上运行负载测试,我可以通过下面的 java 访问它

        name = "username";
        String password = "password";
        String authString = name + ":" + password;
        byte[] authEncBytes = Base64.getEncoder().encode(authString.getBytes()); //
        String authStringEnc = new String(authEncBytes);
        URL url = new URL(urlStr);
        URLConnection urlConnection = url.openConnection();
        urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);

但是在 jmeter 中我无法添加这个,jmeter 新手请帮忙。

【问题讨论】:

    标签: jmeter jmeter-plugins


    【解决方案1】:

    您可以使用相同的代码来生成编码凭据:

    1. HTTP Header Manager 添加为相关 HTTP 请求采样器的子项
    2. JSR223 PreProcessor 添加为相关 HTTP 请求采样器的子项
    3. 将以下代码放入“脚本”区域

      name = "username";
      String password = "password";
      String authString = name + ":" + password;
      byte[] authEncBytes = Base64.getEncoder().encode(authString.getBytes()); //
      String authStringEnc = new String(authEncBytes);
      sampler.getHeaderManager().add(new org.apache.jmeter.protocol.http.control.Header("Authorization", "Basic " + authStringEnc));
      
    4. 勾选Cache compiled script if available

    JMeter 现在应该在您的请求中添加必要的“授权”标头。

    【讨论】:

    • 为什么这被否决了?当官方 JMeter 身份验证经理没有时为我工作。添加 "vars.get("appPassword");"如果您不想硬编码密码。
    • 解释清楚的解决方案。谢谢。
    【解决方案2】:

    自 JMeter 3.2 起,Basic Auth 使用 HttpClient4 实现开箱即用提供并默认配置,请参阅:

    HTTP HC4 实现现在提供默认启用的抢先基本身份验证

    只需添加HTTP_Authorization_Manager并填写所需信息。

    例子:

    • 配置将是:

    此配置将匹配任何以“http://localhost:8081”开头的请求

    另请参阅此问题:

    虽然我的回答因我不知道是什么原因而被否决,但这是设置基本身份验证的最佳方式。自 3.2 起,不再需要编写脚本。

    【讨论】:

      猜你喜欢
      • 2020-06-15
      • 1970-01-01
      • 2014-04-24
      • 2019-09-27
      • 2013-03-03
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多