-1

I wanted to know how to pass Basic Auth username and password to the resttemplate so that other application allow me to access the end points

Suppose I have Basic auth in my secondary application
username:randomSecureKeyUsername!
password:randomSecureKeyPassword!

And here is my restTemplate

@GetMapping
public Train[] getTrains() {
    return restTemplate.getForObject("http://train-detail-service/api/rail", Train[].class);
}

I am trying to retrieve the list of trains from the other microservice application.

Note: These all application are running on eureka server that's why I am using application name

tion name

Nitesh Singh
  • 45
  • 1
  • 9

1 Answers1

0
String username = "randomSecureKeyUsername!";
    String password = "randomSecureKeyPassword!";
    HttpHeaders headers = new HttpHeaders();
    headers.setBasicAuth(username, password);
    HttpEntity<String> request = new HttpEntity<String>(headers);
    ResponseEntity<Train[]> response = restTemplate.exchange("http://train-detail-service/api/rail", HttpMethod.GET,
            request, Train[].class);
    // return restTemplate.getForObject("http://train-detail-service/api/rail",
    // request,Train[].class);
    Train[] train = response.getBody();
    return train;

This one is working fine for me

Nitesh Singh
  • 45
  • 1
  • 9