如何在Keycloak中创建脚本映射器?

Jac*_*000 6 java keycloak

我需要在Keycloak中创建脚本映射器类型的协议映射器。该脚本应获取用户属性,检查其大小,并将其放在令牌上。我没有找到有关如何创建脚本的文档或示例。从我可以收集的点点滴滴中,我想我的脚本需要看起来像:

var value = user.getAttribute("myAttribute");
if (value.length > LIMIT) {
    value = value.substring(0,LIMIT);
}
token.setOtherClaims("myAttribute",value);
Run Code Online (Sandbox Code Playgroud)
  • 这是正确的吗?我组成了user.getAttribute(“ myAttribute”)。有文档来源可以找到如何获取Keycloak用户属性吗?
  • 脚本需要返回什么吗?任何帮助都将受到欢迎。

And*_* B. 12

脚本映射器的魔力可以通过查看这里的keycloak来源可以理解:来源

脚本可以通过使用exports变量返回某些内容,例如

exports = "Claim Value"
Run Code Online (Sandbox Code Playgroud)

不同的类型:

这是一个示例脚本:

// you can set standard fields in token
token.setAcr("test value");

// you can set claims in the token
token.getOtherClaims().put("claimName", "claim value");

// work with variables and return multivalued token value
var ArrayList = Java.type("java.util.ArrayList");
var roles = new ArrayList();
var client = keycloakSession.getContext().getClient();
var forEach = Array.prototype.forEach;
forEach.call(user.getClientRoleMappings(client).toArray(), function(roleModel) {
  roles.add(roleModel.getName());
});

exports = roles;
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!

  • 您的意思是这样吗:/sf/ask/3306360431/? (2认同)
  • 我正是这样做的(在 10.0.2 中),但是我添加的映射器没有显示在 GUI 中。它应该在内置映射器下吗? (2认同)

ulk*_*200 6

我需要这个功能,但在我新安装的 10.0.2 中找不到这个“脚本映射器”。事实证明默认情况下它没有启用,如此处的文档所示:https : //www.keycloak.org/docs/latest/server_installation/#profiles

要启用它,您可以:

  • 创建一个文件standalone/configuration/profile.propertiesfeature.scripts=enabled

或者

  • 启动服务器 bin/standalone.sh|bat -Dkeycloak.profile.feature.scripts=enabled

源代码看来

public boolean isSupported() {
    return Profile.isFeatureEnabled(Profile.Feature.SCRIPTS) && Profile.isFeatureEnabled(Profile.Feature.UPLOAD_SCRIPTS);
}
Run Code Online (Sandbox Code Playgroud)

upload_scripts应同样能够

我希望它会帮助某人

  • 感谢您的评论,我想它也找到了您的帖子:-)。文档说我们不应该在生产中启用脚本上传,但我们应该在服务器上部署脚本:https://www.keycloak.org/docs/latest/server_development/#using-keycloak-administration-console-to-upload-scripts (2认同)