【问题标题】:Jhipster microservice Java client api call errorJhipster微服务Java客户端api调用报错
【发布时间】:2017-12-29 13:38:49
【问题描述】:

我们在微服务架构中部署了一个 JHipster 应用程序。我们正在使用 OAuth 选项,我是其中的新手。前端工作正常。但是现在我们不知道如何调试访问 restful 端点的 java 客户端。我们正在使用 nginx 来实现 ssh 并隐藏端口,以防万一。

代码是:

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

回复是

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)

【问题讨论】:

    标签: spring-boot spring-security oauth-2.0 jhipster resttemplate


    【解决方案1】:

    假设您使用 uaa 和网关。

    您的代码必须使用 POST 和 JSON 有效负载(请参阅 UserJWTControllerIntTest.testAuthorize())针对 /api/authenticate 进行身份验证才能获取令牌。

    获得令牌后,您必须使用带有Bearer 前缀的Authorization HTTP 标头与每个请求一起发送它:Authorization : Bearer c123aaacr

    【讨论】:

    • 哦,谢谢。这说明我知道的太少了。试过邮递员 curl -X POST \myrul/api/authenticate \ -H '授权:基本 Z3JlZy5mY2E6aGVyIFRoZW1lMWJyYXllZDJjYW4zYTRkaXNocmFnJWphbSRhLWdleXNlcg==' \ -H '缓存控制:无缓存' \ -H '内容类型:应用程序/json' \ -H '邮递员-token: 8c2ad2e7-333b-9a56-6d61-c612070f4200' 并得到“不支持请求方法'POST'”,
    • 也尝试使用 OAuth2RestTemplate。收到相同的“不支持请求方法 POST”消息。 Google 建议这与我的请求有关,但我看不出有什么问题。
    • 您的有效负载不是 JSON,为什么所有这些标头,只需执行单元测试 JWTControllerIntTest.testAuthorize() 中所做的操作
    • Gael,对不起,我无法将 UserJWTControllerIntTest.testAuthorize() 转换为我需要的简单客户端 POJO,没有 Jhipster 和 Spring。我找不到关于我的测试卷曲应该是什么样子的文档。我的邮递员代码现在是: curl -X POST myurl/api/authenticate -H 'content-type: application/json' -d '{??}'。如果我知道什么??是的,那会给我一个令牌吗?
    • 正确。 UserJWTController.authorize() 映射到 /api/authenticate 并以 LoginVM 作为参数。 LoginVM 的 JSON 序列化为 {"username": "xxx", "password: "yyy", "rememberMe": false}
    【解决方案2】:

    这就是我要做的工作。

    针对 UAA 版本的微服务架构进行身份验证时 - 终点是https://myAddress.../myApp.../oauth/token - 标头中的内容类型 = application/x-www-form-urlencoded - 正文有grant_type = 密码、用户名和密码。

    我没有考虑使用 UAA - 在文档中没有看到这很重要。

    感谢 Jon Ruddell。

    【讨论】:

      猜你喜欢
      • 2017-04-16
      • 2014-09-04
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      相关资源
      最近更新 更多