【问题标题】:PACT - Modify header to include oAuth2 tokenPACT - 修改标头以包含 oAuth2 令牌
【发布时间】:2019-05-30 10:00:42
【问题描述】:

我在提供者端使用 PACT JVM,我需要在发出请求之前将 oAuth2 令牌添加到标头。 我已遵循此常见问题解答中的建议

https://docs.pact.io/faq#how-do-i-test-oauth-or-other-security-headers

我创建了以下类:

public class TransactionPact {

    @TestTarget
    public final Target target = new HttpTarget(8332);

    private static FinanceApiToken financeApiToken;

      @BeforeClass
      public static void getAuthorisationToken() {
        HttpHeaders header = new HttpHeaders();
        header.set("Content-Type", "application/x-www-form-urlencoded");
        header.set("Authorization", "Basic ZmluLWFwaTphcGktc2VjcmV0");
        header.set("Connection", "keep-alive");
        header.set("cache-control", "no-cache");

        HttpEntity<String> request = new HttpEntity<>("username=sap&password=password2&grant_type=password", header);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<FinanceApiToken> response = restTemplate.postForEntity("http://127.0.0.1:8080/oauth/token", request, FinanceApiToken.class);

        if (response.getStatusCode() != HttpStatus.OK) {
            throw new HTTPException(response.getStatusCodeValue());
        }
        financeApiToken = response.getBody();
      }

      @State("the consumer is authorised")
      public void authorise() {


        //MODIFY HEADER HERE!
      }

    }

但是,我不知道如何在发出请求之前拦截请求以修改标头并包含令牌。

【问题讨论】:

    标签: java pact pact-jvm


    【解决方案1】:
    猜你喜欢
    • 1970-01-01
    • 2017-01-18
    • 2016-05-17
    • 2018-01-11
    • 2019-06-04
    • 2022-08-19
    • 2014-12-03
    • 2021-09-20
    • 2022-07-09
    相关资源
    最近更新 更多