httpclient post从keyposet获取承载令牌

dvtswwa3  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(365)

我已经在独立模式下设置了keydepose11.0.2。它运转良好。我可以与 Postman 一起创建post请求,以获取不记名令牌。现在我想从带有apache httpclient的key斗篷服务器获得一个令牌。我不知道怎么做。
这是我的代码,但它返回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"}

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

yv5phkfx

yv5phkfx1#

您是否可以尝试使用此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"));

相关问题