Java Keycloak 缺少表单参数:grant_type

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53795179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:54:34  来源:igfitidea点击:

Keycloak Missing form parameter: grant_type

javaauthenticationloginkeycloak

提问by Borislav Stoilov

I have keycloak standalone running on my local machine.

我在本地机器上独立运行 keycloak。

I created new realm called 'spring-test', then new client called 'login-app'

我创建了名为“spring-test”的新领域,然后创建了名为“login-app”的新客户端

According to the rest documentation:

根据其余文档:

POST: http://localhost:8080/auth/realms/spring-test/protocol/openid-connect/token

{
    "client_id": "login-app",
    "username": "user123",
    "password": "pass123",
    "grant_type": "password"
}

should give me the jwt token but I get bad request with response

应该给我 jwt 令牌,但我收到了错误的响应请求

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

I am assuming that something is missing in my configuration.

我假设我的配置中缺少某些内容。

EDIT: I was using json body but it should be form url encoded: the following body works:

编辑:我正在使用 json body 但它应该是表单 url 编码:以下正文有效:

token_type_hint:access_token&token:{token}&client_id:{client_id}&client_secret:{client_secret}

采纳答案by ipave

You should send your data in a POST request with Content-Typeheader value set to application/x-www-form-urlencoded, not json.

您应该在 POST 请求中发送数据,并将Content-Type标头值设置为application/x-www-form-urlencoded,而不是 json。

回答by Adnan Khan

For those having problems with curl the curl command is as follows

对于那些有 curl 问题的 curl 命令如下

curl -d "client_secret=<client-secret>" -d "client_id=<client-id>" -d "username=<username>" -d "password=<password>" -d "grant_type=password" "http://localhost:8080/auth/realms/<realm-name>/protocol/openid-connect/token"

The curl command works without the Content-Typeheader.

curl 命令在没有Content-Type标题的情况下工作。

回答by Marc

For those who landed here from a search looking for JavaScriptsolution.

对于那些从搜索中找到JavaScript解决方案的人。

Here is an example when exchanging codefor access_tokenwith keycloakauthority using axios.

交换时下面是一个例子codeaccess_tokenkeycloak权限使用axios

querystringis used in this example:

本示例中使用了查询字符串

npm install querystring --save-dev

or

或者

yarn add querystring --dev

Sending the request:

发送请求:


import queryString from 'querystring'

const params = {

    grant_type: 'authorization_code,
    client_id: 'client-id-here',
    code: 'code-from-previous-redirect',
    redirect_uri: location.protocol + '//' + location.host

};

axios({

    method: 'post',
    url: 'https://my-keycloak.authority/token',
    data: queryString.stringify(params),
    config: {
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }

}).then(response => {

    console.log(response.data);

}).catch(error => {

    console.error(error);

});

You are required to send a POST request with the parameters as a URL encoded string in the request body.

FormData object does not work.

您需要在请求正文中以 URL 编码字符串的形式发送带有参数的 POST 请求。

FormData 对象不起作用。

回答by Lanil Marasinghe

Here's a sample CURL command

这是一个示例 CURL 命令

curl -X POST \
  http://localhost:8080/auth/realms/your_realm_name/protocol/openid-connect/token \
  -H 'Accept: */*' \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Length: 69' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Cookie: KC_RESTART=' \
  -H 'Host: localhost:8080' \
  -H 'Postman-Token: 88f38aa0-8659-4b37-a2aa-d6b92177bdc2,29c4e7db-51f4-48d1-b6d5-daab06b68ab4' \
  -H 'User-Agent: PostmanRuntime/7.20.1' \
  -H 'cache-control: no-cache' \
  -d 'client_id=my-app&username=admin&password=admin123&grant_type=password'

回答by Rohit Tewari

I had similar issue in SOAPUI testing. We should not POST a json. This got resolved when I cleared 'Media Type' and checked 'PostQueryString' checkbox. 'Media Type box' will set to 'www-form-urlencoded' by itself. Add attributes at Top by hitting plus sign.

我在 SOAPUI 测试中遇到了类似的问题。我们不应该发布一个 json。当我清除“媒体类型”并选中“PostQueryString”复选框时,这个问题得到了解决。“媒体类型框”将自行设置为“www-form-urlencoded”。通过点击加号在顶部添加属性。

enter image description here

在此处输入图片说明

回答by Robin Mathur

With Curl

带卷曲

curl -X POST \
http://localhost:8080/auth/realms/api-gateway/protocol/openid-connect/token \
-H 'Accept: */*' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Cache-Control: no-cache' \
-H 'Connection: keep-alive' \
-H 'Content-Length: 73' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Cookie: JSESSIONID=F8CD240FF046572F864DC37148E51128.a139df207ece;   JSESSIONID=65D31B82F8C5FCAA5B1577DA03B4647C' \
-H 'Host: localhost:8080' \
-H 'Postman-Token: debc4f90-f555-4769-b392-c1130726a027,d5087d9f-9253-48bd-bb71-fda1d4558e4d' \
-H 'User-Agent: PostmanRuntime/7.15.2' \
-H 'cache-control: no-cache' \
-d 'grant_type=password&client_id=api-gateway&username=admin&password=temp123'

By Postman (Select x-www-form-urlencoded option for parameters)

邮递员(为参数选择 x-www-form-urlencoded 选项)

enter image description here

在此处输入图片说明