1

有没有办法通过 CLI 为 Keycloak 独立安装配置 reCAPTCHA?更准确地说,是否可以在 Keycloak 文档的帮助下执行此处描述的所有步骤kcadm.sh

4

1 回答 1

2

您可以通过使用Keycloak Admin REST API来实现。

第一步是获取管理员令牌,以便调用 Rest API:

curl    -d "client_id=admin-cli" \
        -d "username=$ADMIN_NAME" \
        -d "password=$ADMIN_PASSWORD" \
        -d "grant_type=password" \
        https://$KEYCLOAK_IP/auth/realms/master/protocol/openid-connect/token

您将获得一个json response带有管理员令牌的。从该响应中提取访问令牌(让我们调用$ACCESS_TOKEN)。

现在,我们需要获取链接到注册流程的所有执行列表:

curl  -X GET https://$KEYCLOAK_IP/auth/admin/realms/$REALM_NAME/authentication/flows/registration/executions \
                -H "Content-Type: application/json" \
                -H "Authorization: bearer $ACCESS_TOKEN"

从中json response提取id"providerId=registration-recaptcha-action"。让我们称之为id, $ID_RECAPTCHA

接下来制作注册时所需的reCaptcha:

CAPTCHA_DATA='{"id":"$ID_RECAPTCHA","requirement":"REQUIRED","providerId":"registration-recaptcha-action"}'

curl -X PUT https://$KEYCLOAK_IP/auth/admin/realms/$REALM_NAME/authentication/flows/registration/executions \
                            -H "Content-Type: application/json" \
                            -H "Authorization: bearer $ACCESS_TOKEN"\
                            -d "$JSON_DATA"

最后,配置您自己的验证码:

CONFIG_DATA='{"config":{"site.key":"<YOUR SITE KEY>","secret":"<YOUR SECRET>","useRecaptchaNet":"<True or False>"},"alias":"<The CAPTCHA ALIAS>"}'

curl -X POST https://$KEYCLOAK_IP/auth/admin/realms/$REALM_NAME/authentication/executions/$ID_RECAPTCHA/config \
                -H "Content-Type: application/json" \
                -H "Authorization: bearer $ACCESS_TOKEN"\

接下来,最好的办法是使用例如一些 bash 脚本来自动化这个过程。

于 2020-11-17T12:06:23.200 回答