HttpClient post 从 keycloak 获取 Bearer token

HttpClient post to obtain a Bearer token from keycloak

我已经在独立模式下设置了 keycloak 11.0.2。它已经启动并且 运行 很好。我能够创建一个 POST-请求 Postman 来获取不记名令牌。现在我想使用 Apache HttpClient 从 keycloak 服务器获取令牌。我不知道怎么做。

这是我的代码,但是它 returns 400 错误,我也有 415:

            CloseableHttpClient client = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost("http://localhost:8180/auth/realms/Demo-Realm/protocol/openid-connect/token");
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
            httpPost.addHeader("grant_type","password");
            httpPost.addHeader("client_secret","30e8ebdf-7fbb-449d-9d94-709166b879b0");
            httpPost.addHeader("client_id","springboot-microservice");
            httpPost.addHeader("username","employee1");
            httpPost.addHeader("password","mypassword");
            CloseableHttpResponse response = client.execute(httpPost);
            System.out.println("response: " + response.toString());
            client.close();

这是回复:

response: HttpResponseProxy{HTTP/1.1 400 Bad Request [Cache-Control: no-store, X-XSS-Protection: 1; mode=block, Pragma: no-cache, X-Frame-Options: SAMEORIGIN, Referrer-Policy: no-referrer, Date: Tue, 17 Nov 2020 14:31:31 GMT, Connection: keep-alive, Strict-Transport-Security: max-age=31536000; includeSubDomains, X-Content-Type-Options: nosniff, Content-Type: application/json, Content-Length: 84] ResponseEntityProxy{[Content-Type: application/json,Content-Length: 84,Chunked: false]}}

我想知道如何做对。我尝试像 Postman 那样做,但我遗漏了一些东西

更新/编辑:现在我更进了一步,但我仍然无法理解这种行为。这是我的代码:

      String result = "";
            HttpPost post = new HttpPost("http://localhost:8180/auth/realms/Demo-Realm/protocol/openid-connect/token");
            post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            StringBuilder json = new StringBuilder();
            json.append("{");
            json.append("\"grant_type\":\"password\"");
            json.append("\"client_id\":\"springboot-microservice\",");
            json.append("\"username\":\"employee1\"");
            json.append("\"password\":\"mypassword\"");
            json.append("}");
            post.setEntity(new StringEntity(json.toString()));
            try (CloseableHttpClient httpClient = HttpClients.createDefault();
                 CloseableHttpResponse response = httpClient.execute(post)) {
                result = EntityUtils.toString(response.getEntity());
            }
            System.out.println("result: " + result.toString());

现在我收到回复:

result: {"error":"invalid_request","error_description":"Missing form parameter: grant_type"}

但这就是我发送的?我做错了什么?

你能尝试使用这个 BasicNameValuePair 而不是作为 json:

发送吗
ArrayList<NameValuePair> parameters;
parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("grant_type", "password"));
parameters.add(new BasicNameValuePair("client_id", "springboot-microservice"));
parameters.add(new BasicNameValuePair("username", "employee1"));
parameters.add(new BasicNameValuePair("password", "mypassword"));
parameters.add(new BasicNameValuePair("client_secret", "30e8ebdf-7fbb-449d-9d94-709166b879b0"));
post.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));