【问题标题】:How to do a OAuth 2.0 authentication in JMeter?如何在 JMeter 中进行 OAuth 2.0 身份验证?
【发布时间】:2017-06-08 16:43:26
【问题描述】:

我正在尝试对一些需要进行身份验证 (OAuth 2.0) 的 API 进行功能测试并在 JMeter 中进行模拟。

我正在尝试对 Azure 云的 OAuth 服务进行身份验证。有没有人能够成功创建 JMeter HTTP 请求来针对 OAuth 2.0 进行身份验证?

【问题讨论】:

标签: azure authentication oauth-2.0 jmeter jmeter-plugins


【解决方案1】:

基本上,您需要添加HTTP Header Manager 以发送值为Bearer ${ACCESS_TOKEN}Authorization 标头,以便进行经过身份验证的OAuth API 调用。

Access Token 获取方式主要有两种:

  1. 以某种方式获取它(请求它,使用嗅探器工具和您需要模拟的应用程序捕获它等),但请注意 OAuth 访问令牌有 limited life span(默认为 1 小时,这适用于第 2 点)好)
  2. 在您的测试中实施 OAuth2 流程,即:

    • 验证(提供客户端 ID 和租户 ID)
    • 授权(使用客户端 ID 和上一步中的代码)
    • 获取访问令牌(提供上一步的授权码、第一步的代码和客户端 ID

关于实施选项 2 - 它需要 3 个单独的 JMeter 采样器(或者您可以通过 JSR223 Sampler 以编程方式获取访问令牌)

参考资料:

【讨论】:

  • 首先感谢您整理这些信息。我试图避免不得不手动执行此操作的整个麻烦。无论如何,我已经成功地通过了身份验证、授权,现在在尝试获取访问令牌时,我收到了这个误导性错误:“AADSTS70002:验证凭据时出错。AADSTS50012:提供了无效的客户端密码..” error_codes=70002, 50012 从表面上看,我认为这将是一个不正确的 client_secret,但是在检查了多次(前导或尾随空格)后,我没有成功。有什么线索吗?
  • 如何使用 jmeter 获取认证令牌?
  • 我的意思是多个用户
【解决方案2】:

作为 API 测试自动化的一部分,我们确实创建了原生客户端 ID,将所需资源分配给原生客户端。

所有你需要的 adal4j-1.6.X.jar

public static AuthenticationResult getAuthToken(String username, String password, 
String clientId, String authority, String tenant, String urii) throws Throwable {

    AuthenticationContext context = null;
    AuthenticationResult result = null;
    ExecutorService service = null;

    crypToUtil td= new crypToUtil();
    crypToUtil cryptoUtil = new crypToUtil(); 

    password = cryptoUtil.decrypt(password);

    try {

        service = Executors.newFixedThreadPool(1);
        context = new AuthenticationContext(authority + tenant + "/", true,service);
        Future<AuthenticationResult> future = context.acquireToken(urii, clientId, username, password,null);
        result = future.get();                

    } catch (ExecutionException | MalformedURLException e) {

        throw e.getCause();

    } finally {

        service.shutdown();

    }

    if (result == null) {

        throw new ServiceUnavailableException("authentication result was null, could be your input data were wrong ...");

    }

    return result;

}

【讨论】:

    猜你喜欢
    • 2012-11-27
    • 2015-01-25
    • 2012-11-08
    • 2014-05-03
    • 1970-01-01
    • 2016-03-28
    • 2023-03-07
    • 1970-01-01
    • 2011-09-09
    相关资源
    最近更新 更多