代码之家  ›  专栏  ›  技术社区  ›  Stimpson Cat

HttpClient post从keyposet获取承载令牌

  •  0
  • Stimpson Cat  · 技术社区  · 4 年前

    我已经在独立模式下设置了keydepose11.0.2。它运转良好。我可以与邮递员一起创建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]}}
    

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

          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"}
    

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

    0 回复  |  直到 4 年前
        1
  •  1
  •   dreamcrash    4 年前

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