如何在假装客户端中传递标头,尤其是HTTP标头



我尝试使用Rest Template传递http标头及其按预期工作,但有些它无法与假装客户端一起使用。

其余客户端代码:-

import org.springframework.http.HttpHeaders;
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeader = new HttpHeaders();
httpHeader.set("appsecret-proof", header);
HttpEntity<?> request = new HttpEntity<>(httpHeader);
String url = "https://localhost/groups/{pathVariable}/members/";
Map<String, String> map = new HashMap<>();
map.put("pathVariable", pathVariable);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
                                                   .queryParam("token", token);
String uriBuilder = builder.build().toString();
ResponseEntity<MemberObject> responseEntity = restTemplate.exchange(uriBuilder, HttpMethod.POST, request,MemberObject.class, map);
MemberObject memberObject = null;
if (responseEntity != null && responseEntity.getBody() != null) {
    memberObject = responseEntity.getBody();
}

费金客户端代码:-

import org.springframework.web.bind.annotation.RequestHeader;
@PostMapping(value = "/groups/?token=xyz")
MemberObject getMemberGroup(@RequestHeader("appsecret-proof") String appsecretProof);

但是 fegin 客户端给出的错误是:

{"error":{"message":"Authentication credentials could not be found.","type":"Authentication Error","code":404,"sub_code":0}}.

在假装客户端中对令牌进行硬编码,仍然给出相同的错误。标头可能是错误。任何如何在假装客户端中设置标头的建议。

如果您使用 Feign,这就是您应该如何构建调用:

import feign.Headers;
import feign.Param;
import feign.RequestLine;
...
@RequestLine("POST /groups/?token={appSecretProof}")
@Headers("Content-Type: application/json")
MemberObject getMemberGroup(@Param("appSecretProof") String appSecretProof);

在上面的代码片段中,appSecretProof 的值在执行时作为令牌的相应值替换到 URL 中。

相关内容

  • 没有找到相关文章

最新更新