Bor*_*lov 15 java authentication login keycloak
我在本地计算机上运行了独立的keycloak。
我创建了一个名为“ spring-test”的新领域,然后创建了一个名为“ login-app”的新客户端
根据其余文档:
POST: http://localhost:8080/auth/realms/spring-test/protocol/openid-connect/token
{
"client_id": "login-app",
"username": "user123",
"password": "pass123",
"grant_type": "password"
}
Run Code Online (Sandbox Code Playgroud)
应该给我jwt令牌,但是我收到了错误的请求并做出了回应
{
"error": "invalid_request",
"error_description": "Missing form parameter: grant_type"
}
Run Code Online (Sandbox Code Playgroud)
我假设我的配置中缺少某些内容。
编辑:我正在使用json正文,但它应该是形式url编码:以下正文有效:
token_type_hint:access_token&token:{token}&client_id:{client_id}&client_secret:{client_secret}
Run Code Online (Sandbox Code Playgroud)
ipa*_*ave 22
您应该在POST请求中发送数据,并将Content-Type标头值设置为application/x-www-form-urlencoded,而不是json。
小智 14
带卷曲
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'
Run Code Online (Sandbox Code Playgroud)
邮递员(为参数选择 x-www-form-urlencoded 选项)
Mar*_*ton 10
对于那些从搜索中找到JavaScript解决方案的人。
交换时下面是一个例子code为access_token有keycloak权限使用axios。
本示例中使用了查询字符串:
npm install querystring
Run Code Online (Sandbox Code Playgroud)
或者
yarn add querystring
Run Code Online (Sandbox Code Playgroud)
发送请求:
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);
});
Run Code Online (Sandbox Code Playgroud)
您需要在请求正文中以 URL 编码字符串的形式发送带有参数的 POST 请求。
FormData 对象不起作用。
对于卷曲有问题的人,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"
Run Code Online (Sandbox Code Playgroud)
curl命令在没有Content-Type标题的情况下有效。
| 归档时间: |
|
| 查看次数: |
7423 次 |
| 最近记录: |