[英]PACT - Modify header to include oAuth2 token
我在提供者端使用PACT JVM,我需要在發出請求之前向頭部添加oAuth2令牌。 我已按照本FAQ中的建議進行操作
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!
}
}
但是,我不知道如何在進行之前攔截請求以修改標頭並包含令牌。
顯然@TargetRequestFilter可以完成工作。 這是文檔https://static.javadoc.io/au.com.dius/pact-jvm-provider-junit_2.11/3.2.3/au/com/dius/pact/provider/junit/TargetRequestFilter.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.