在Keycloak里添加飞书IdP

先在Keycloak的管理员控制台的Identity Providers中添加OAuth v2 idp,Alias比如设为feishu。具体参数参考:
https://open.feishu.cn/document/sso/web-application-sso/login-overview

添加后,登陆中使用feishu第三方登陆,Keycloak后台报异常:No identifier provider for identity.
原因是标准的OAuth2流程在第三步取user_info,返回的json是在根结点有sub字段表示登陆者id,但飞书的user_info返回字段不标准,比如需要我们改代码取data子节点的email作为登陆者id。

具体在keycloak-26.3.0\services\src\main\java\org\keycloak\broker\oauth\OAuth2IdentityProvider.java
doGetFederatedIdentity函数返回identity之前,加入代码:

1
2
3
4
5
            if (userInfo.has("data")) {
                JsonNode dataNode = userInfo.get("data");
                id = dataNode.get("email") != null ? dataNode.get("email").asText() : null;
                identity.setId(id);
            }