Ribeye
Ribeye

Reputation: 2177

Jhipster microservice Java client api call error

We have deployed a JHipster app in microservice architecture. We are using the OAuth option, in which I am a newbee. The front end works fine. But now we dont know how to debug the java client that accesses the restful endpoints. We are using nginx to implement ssh and hide the port, in case that is relevant.

Code is:

    String plainCreds = "myuser:mypassword";
    byte[] plainCredsBytes = plainCreds.getBytes();
    byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
    String base64Creds = new String(base64CredsBytes);

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic " + base64Creds);

    RestTemplate restTemplate = new RestTemplate();
    HttpEntity<String> request = new HttpEntity<String>(headers);
    String url = "https://mysite/api/my-entity/1111";
    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
    String response = response.getBody();

The response is

org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:63)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)

Upvotes: 0

Views: 701

Answers (2)

Ribeye
Ribeye

Reputation: 2177

This is what I got to work.

When authenticating against the UAA version of the microservices architecture - end point is https://myAddress.../myApp.../oauth/token - Content-Type in header = application/x-www-form-urlencoded - Body has grant_type = password, username and password.

I left my use of UAA out of the question - did not see that made important in the docs.

With thanks to Jon Ruddell.

Upvotes: 0

Ga&#235;l Marziou
Ga&#235;l Marziou

Reputation: 16284

Assuming you use uaa and a gateway.

Your code must authenticate against /api/authenticate using a POST and JSON payload (see UserJWTControllerIntTest.testAuthorize()) to get a token.

Once you have a token, you must send it with each request using Authorization HTTP header with Bearer prefix: Authorization : Bearer c123aaacr

Upvotes: 1

Related Questions