使用 AWS 签名进行 RestAssured 身份验证

问题描述 投票:0回答:1

我想知道如何将 AWS 身份验证签名传递给 RestAssured。 AWS Signature 身份验证似乎没有内置于 RestAssured 中。任何让它发挥作用的帮助将不胜感激。

java amazon-web-services rest-assured amazon-cognito
1个回答
2
投票

问题的解决方案比我希望的要复杂得多。

我找到了一个提供签名部分代码的链接https://www.javaquery.com/2016/01/aws-version-4-signing-process-complete.html

就我而言,我还必须提供会话令牌,因此我必须在签名者中添加该信息。

public static void main(String[] args) {
    String url = "xxxxx-yyyyy-r6nvlhpscgdwms5.ap-northeast-1.es.amazonaws.com/inventory/simple/123";

    RequestSpecification restAssuredRequest;
    restAssuredRequest = RestAssured.given();

    JSONObject restAssuredRequestParams = new JSONObject();
    restAssuredRequestParams.put("AccessToken", accessToken);
    restAssuredRequestParams.put("YourParam1", object1);
    restAssuredRequestParams.put("YourParam2", object2);

    /**
     * Add host without http or https protocol.
     * You can also add other parameters based on your amazon service requirement.
     */
    TreeMap<String, String> awsHeaders = new TreeMap<String, String>();
    awsHeaders.put("host", "xxxxx-yyyyy-r6nvlhpscgdwms5.ap-northeast-1.es.amazonaws.com");

    AWSV4Auth aWSV4Auth = new AWSV4Auth.Builder("exampleKey", "exampleSecret", "exampleSessionToken")
                                       .regionName("xx-yy-zzz")
                                       .serviceName("es") // es - elastic search. use your service name
                                       .httpMethodName("GET") //GET, PUT, POST, DELETE, etc...
                                       .canonicalURI("/inventory/simple/123") //end point
                                       .queryParametes(null) //query parameters if any
                                       .awsHeaders(awsHeaders) //aws header parameters
                                       .payload(restAssuredRequestParams) // payload if any
                                       .debug() // turn on the debug mode
                                       .build();

    /* Get header calculated for request */
    Map<String, String> header = aWSV4Auth.getHeaders();
    for (Map.Entry<String, String> entrySet : header.entrySet()) {
        String key = entrySet.getKey();
        String value = entrySet.getValue();
        restAssuredRequest.header(key, value);

        /* Attach header in your request */
        /* Simple get request */
        //HttpGet httpGet = new HttpGet(url);
        //httpGet.addHeader(key, value);
    }
    restAssuredRequest.body(restAssuredRequestParams);
    Response restAssuredResponse = restAssuredRequest.post(endpoint path);
    System.out.println(restAssuredResponse.asString());
    System.out.println(restAssuredResponse.getStatusCode());
}

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.