使用AWS Signature进行RestAssured身份验证

时间:2018-06-28 22:19:38

标签: java amazon-web-services rest-assured aws-cognito

我想知道如何将AWS身份验证签名传递给RestAssured。 RestAssured似乎没有内置AWS Signature身份验证。对于如何使它正常工作的任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

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

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

希望有帮助。