如何通过API创建Keycloak的客户端密钥(clientSecret)

huangapple go评论59阅读模式
英文:

How to create keyclock clientSecret via API

问题

我正在使用Keycloak版本20.0.2,并想知道是否可以通过POST请求(/admin/realms/:realm/clients/:id/client-secret)创建clientSecret。

我想要通过POST请求发送包含我自己生成的client secret值的请求体。

请求URL:
https://{host}/admin/realms/{realm}/clients/{id}/client-secret

请求体:
{
"clientSecret": "我自己生成的值在这里"
}

响应:
{
type: "secret",
value: "我自己生成的值在这里"
}

英文:

I’m using keycloak version 20.0.2, and I want to know if it possible to create clientSecret through a post request (/admin/realms/:realm/clients/:id/client-secret).

I want to send body to post request with my own generated client secret value

https://{host}/admin/realms/{realm}/clients/{id}/client-secret

Body: {  
   "clientSecret":"my own generated value here"  
}

Response: {  
  type: "secret",  
  value: "my own generated value here" 
}

答案1

得分: 1

您有两个选项。

#1 创建一个带有自己密钥的新客户端 API

#2 可以通过 Keycloak 在现有客户端上生成随机密钥

但没有选项可以使用您自己的方式更新客户端密钥。

#1 创建一个带有自己密钥的新客户端 API

POST {Keycloak URL}/admin/realms/test/clients

请求体

{
    "clientId": "<新客户端ID>",
    "name": "<客户端名称>",
    "enabled": true,
    "clientAuthenticatorType": "client-secret",
    "secret": "<我的自己的密钥>"
}

Postman 示例
如何通过API创建Keycloak的客户端密钥(clientSecret)

请求体

{
    "clientId": "Test-Client",
    "name": "Test-Client-New",
    "enabled": true,
    "clientAuthenticatorType": "client-secret",
    "secret": "my-new-own-secret"
}

结果
如何通过API创建Keycloak的客户端密钥(clientSecret)
如何通过API创建Keycloak的客户端密钥(clientSecret)

#2 通过 Keycloak 随机生成密钥

POST {Keycloak URL}/admin/realms/{我的领域}/clients/{客户端UUID}/client-secret

输入请求体

None

响应体

{
    "type": "secret",
    "value": "<由 Keycloak 随机创建的新密钥>"
}

如何通过API创建Keycloak的客户端密钥(clientSecret)
如何通过API创建Keycloak的客户端密钥(clientSecret)

在 Keycloak 用户界面中的结果
如何通过API创建Keycloak的客户端密钥(clientSecret)

英文:

You have two options.

#1 One is create new client API with own secret

#2 Second is can random generated secret on existing client by Keycloak

But no option update the client secrete with your own.

#1 One is create new client API with own secret

POST {Keycloak URL}/admin/realms/test/clients

Body

{
    &quot;clientId&quot;: &lt;new client ID&gt;,
    &quot;name&quot;: &lt;Cleint Name&gt;,
    &quot;enabled&quot;: true,
    &quot;clientAuthenticatorType&quot;: &quot;client-secret&quot;,
    &quot;secret&quot;: &lt;my-own-secret&gt;
}

Example by Postman
如何通过API创建Keycloak的客户端密钥(clientSecret)

In Body

{
    &quot;clientId&quot;: &quot;Test-Client&quot;,
    &quot;name&quot;: &quot;Test-Client-New&quot;,
    &quot;enabled&quot;: true,
    &quot;clientAuthenticatorType&quot;: &quot;client-secret&quot;,
    &quot;secret&quot;: &quot;my-new-own-secret&quot;
}

Result
如何通过API创建Keycloak的客户端密钥(clientSecret)
如何通过API创建Keycloak的客户端密钥(clientSecret)

#2 Second is can random generated secret by Keycloak

POST {Keycloak URL}/admin/realms/{my-realm}/clients/{client-uuid}/client-secret

Input Body

None

Response Body

{
    &quot;type&quot;: &quot;secret&quot;,
    &quot;value&quot;: &lt;Random created new secret by Keycloak&gt;
}

如何通过API创建Keycloak的客户端密钥(clientSecret)
如何通过API创建Keycloak的客户端密钥(clientSecret)

Result in Keycloak UI
如何通过API创建Keycloak的客户端密钥(clientSecret)

huangapple
  • 本文由 发表于 2023年2月26日 19:42:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定