我需要在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)
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)
希望能帮助到你!
我需要这个功能,但在我新安装的 10.0.2 中找不到这个“脚本映射器”。事实证明默认情况下它没有启用,如此处的文档所示:https : //www.keycloak.org/docs/latest/server_installation/#profiles
要启用它,您可以:
standalone/configuration/profile.properties
与feature.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
应同样能够
我希望它会帮助某人