简体   繁体   English

Jhipster微服务Java客户端api调用错误

[英]Jhipster microservice Java client api call error

We have deployed a JHipster app in microservice architecture. 我们已经在微服务架构中部署了JHipster应用。 We are using the OAuth option, in which I am a newbee. 我们使用的是OAuth选项,我是新手。 The front end works fine. 前端工作正常。 But now we dont know how to debug the java client that accesses the restful endpoints. 但是现在我们不知道如何调试访问宁静端点的Java客户端。 We are using nginx to implement ssh and hide the port, in case that is relevant. 我们将使用nginx来实现ssh并隐藏端口(如果相关)。

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)

Assuming you use uaa and a gateway. 假设您使用uaa和网关。

Your code must authenticate against /api/authenticate using a POST and JSON payload (see UserJWTControllerIntTest.testAuthorize() ) to get a token. 您的代码必须使用POST和JSON负载针对/api/authenticate进行/api/authenticate (请参阅UserJWTControllerIntTest.testAuthorize() )以获取令牌。

Once you have a token, you must send it with each request using Authorization HTTP header with Bearer prefix: Authorization : Bearer c123aaacr 拥有令牌后,您必须使用带有Bearer前缀的Authorization HTTP标头将每个请求与令牌一起发送: Authorization : Bearer c123aaacr

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. 针对微服务架构的UAA版本进行身份验证时-端点为https://myAddress.../myApp.../oauth/token-标头中的Content-Type = application / x-www-form-urlencoded-主体具有grant_type =密码,用户名和密码。

I left my use of UAA out of the question - did not see that made important in the docs. 我完全不考虑使用UAA-在文档中没有发现这一点很重要。

With thanks to Jon Ruddell. 感谢Jon Ruddell。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM