我正在尝试使用 Spring的休息模板启用 vault的userpass身份验证模式,但是获取 403 - 禁止但是在邮差有效。我尝试了PUT和POST,标题中的X-Vault-Token(值是根令牌)和body中的type(value是userpass)。任何人都可以提供任何修复。
请在以下代码中找到我的休息电话:
public static ResponseEntity<?> httpLoginRequest(String serverUrl,
HttpMethod method, HttpHeaders headers, JsonObject request, Class<?> responseType) {
RestTemplate restTemplate = null;
HttpEntity<?> requestEntity = null;
try {
restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
/*messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());*/
if (request != null) {
requestEntity = new HttpEntity<Object>(request.toString(), headers);
} else {
requestEntity = new HttpEntity<Object>(headers);
}
} catch (Exception e) {
//throw new HolmesRuntimeException(e);
}
/*
ClientHttpRequestInterceptor interceptor = new HttpRequestInterceptor();
restTemplate.setInterceptors(Arrays.asList(interceptor));
*/
return restTemplate.exchange(serverUrl, method, requestEntity, responseType);
}
我添加到请求中的标题:
headers = new HttpHeaders();
headers.add("X-Vault-Token", headerValue);
headers.add(HttpHeaders.CACHE_CONTROL, "no-cache");
headers.add(HttpHeaders.ACCEPT, "*/*");
headers.add(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate");
headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "POST, GET, PUT, DELETE");
logger.info("Value of header object is {}",headers);
request = new JsonObject();
request.addProperty("type", "userpass");
logger.info("Value of request object for authentication request is {}", request);
ResponseEntity<?> responseAuth = CommonUtils.httpLoginRequest(url, HttpMethod.PUT, headers, request, String.class);