我创建了一个Postman集合,以帮助我们开始使用Keycloak API。任何人都可以保存以下JSON并在Postman中导入:
{
"info": {
"_postman_id": "07a9d691-5b1c-4869-990b-551da29590fe",
"name": "Keycloak",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "GET REALM",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{KEYCLOAK_URL}}admin/realms/{{KEYCLOAK_REALM}}",
"host": [
"{{KEYCLOAK_URL}}admin"
],
"path": [
"realms",
"{{KEYCLOAK_REALM}}"
]
}
},
"response": []
},
{
"name": "GET USERS",
"event": [
{
"listen": "prerequest",
"script": {
"id": "dfda403a-35b8-4704-840d-102eddac32e6",
"exec": [
""
],
"type": "text/javascript"
}
}
],
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "urlencoded",
"urlencoded": []
},
"url": {
"raw": "{{KEYCLOAK_URL}}admin/realms/{{KEYCLOAK_REALM}}/users",
"host": [
"{{KEYCLOAK_URL}}admin"
],
"path": [
"realms",
"{{KEYCLOAK_REALM}}",
"users"
]
}
},
"response": []
}
],
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{KEYCLOAK_TOKEN}}",
"type": "string"
}
]
},
"event": [
{
"listen": "prerequest",
"script": {
"id": "c3ae5df7-b1e0-4af1-988b-c592df3fd98e",
"type": "text/javascript",
"exec": [
"const echoPostRequest = {",
" url: pm.environment.get('KEYCLOAK_URL') + 'realms/master/protocol/openid-connect/token',",
" method: 'POST',",
" header: 'Content-Type:application/x-www-form-urlencoded',",
" body: {",
" mode: 'urlencoded',",
" urlencoded: [",
" {key:'username', value:pm.environment.get('KEYCLOAK_USER')}, ",
" {key:'password', value:pm.environment.get('KEYCLOAK_PASSWORD')}, ",
" {key:'client_id', value:'admin-cli'}, ",
" {key:'grant_type', value:'password'}",
" ]",
" }",
"};",
"",
"var getToken = true;",
"",
"if (!pm.environment.get('KEYCLOAK_TOKEN_EXPIRY') || ",
" !pm.environment.get('KEYCLOAK_TOKEN')) {",
" console.log('Token or expiry date are missing')",
"} else if (pm.environment.get('KEYCLOAK_TOKEN_EXPIRY') <= (new Date()).getTime()) {",
" console.log('Token is expired')",
"} else {",
" getToken = false;",
" console.log('Token and expiry date are all good');",
"}",
"",
"if (getToken === true) {",
" pm.sendRequest(echoPostRequest, function (err, res) {",
" console.log(err ? err : res.json());",
" if (err === null) {",
" console.log('Saving the token and expiry date')",
" var responseJson = res.json();",
" pm.environment.set('KEYCLOAK_TOKEN', responseJson.access_token)",
" ",
" var expiryDate = new Date();",
" expiryDate.setSeconds(expiryDate.getSeconds() + responseJson.expires_in);",
" pm.environment.set('KEYCLOAK_TOKEN_EXPIRY', expiryDate.getTime());",
" }",
" });",
"}"
]
}
},
{
"listen": "test",
"script": {
"id": "fdb69bb4-14a5-43b4-97e2-af866643e390",
"type": "text/javascript",
"exec": [
""
]
}
}
],
"variable": [
{
"id": "698bbb41-d3f9-47f8-9848-4a1c32f9cca4",
"key": "token",
"value": ""
}
],
"protocolProfileBehavior": {}}
我创建了一个预设脚本来获取令牌并设置请求,如下图所示:
![enter image description here](https://istack.dev59.com/TKoKP.webp)
您应该创建以下环境变量:KEYCLOAK_USER、KEYCLOAK_PASSWORD和KEYCLOAK_URL,其中URL必须是https://{您的keycloak安装}/auth/。