如何在Keycloak中创建脚本映射器? [英] How to create a Script Mapper in Keycloak?
本文介绍了如何在Keycloak中创建脚本映射器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在Keycloak中创建脚本映射器"类型的协议映射器.该脚本应获取用户属性,检查其大小,并将其放在令牌上.我没有找到有关如何创建脚本的文档或示例.从我可以收集的点点滴滴来看,我想我的脚本需要看起来像这样:
I need to create a Protocol Mapper of type Script Mapper in Keycloak. The script should get a user attribute, check its size, and put it on the token. I found no documentation or examples of how a script should be created. From the bits and pieces I could gather, I guess I the script would need to look something like:
var value = user.getAttribute("myAttribute");
if (value.length > LIMIT) {
value = value.substring(0,LIMIT);
}
token.setOtherClaims("myAttribute",value);
- 这是对的吗?我组成了user.getAttribute("myAttribute").是否有文档来源可以找到如何获取Keycloak用户属性?
- 脚本是否需要返回任何内容? 任何帮助都将受到欢迎.
- Is this right? I made up user.getAttribute("myAttribute"). Is there a source of documentation where I can find how to get a Keycloak user attribute?
- does the script need to return anything? Any help would be mostly welcome.
推荐答案
通过查看此处的keycloak来源,可以理解脚本映射器的魔力:
The magic of Script Mappers can be understood by looking at the keycloak sources here: Source
脚本可以通过使用此类exports变量返回某些内容
The script can return something by using the exports variable like this
exports = "Claim Value"
不同的类型:
- user: Source JavaDoc
- realm: Source JavaDoc
- token: Source JavaDoc
- userSession: Source JavaDoc
- keycloakSession: Source JavaDoc
这是一个示例脚本:
// 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;
希望有帮助!
这篇关于如何在Keycloak中创建脚本映射器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文