0

I'm trying to enable vault's userpass authentication mode using Spring's rest template but getting 403 - Forbidden but the same request created in Postman works. I tried with both PUT and POST, with X-Vault-Token(value is the the root token) in header and type(value is userpass) in body. Can anyone provide any fix.

Please find the below code for my rest call :

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);

}

Also the headers that I have added to my request:

            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);
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120

0 Answers0