使用 AWS 签名的 RestAssured 身份验证

RestAssured authentication with AWS Signature

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

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

我找到了一个 link,它提供了签名部分的代码 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());
}

希望对您有所帮助。