从Liferay DXP 7.2升级到7.4:OpenIdConnectUserInfoProcessor接口无法识别求助
Liferay DXP 7.4 替代 OpenIdConnectUserInfoProcessor 的用户数据映射方案
核心替代方案:自定义 OIDCUserMapper
Liferay DXP 7.4中已移除OpenIdConnectUserInfoProcessor接口,官方改用OIDCUserMapper接口处理OpenID Connect用户数据映射逻辑,实现步骤如下:
- 实现
com.liferay.portal.security.sso.openid.connect.internal.mapper.OIDCUserMapper接口,核心重写mapUserInfo方法,完成第三方用户信息到LiferayUser对象的映射。 - 示例代码片段:
import com.liferay.portal.security.sso.openid.connect.internal.mapper.OIDCUserMapper; import com.liferay.portal.security.sso.openid.connect.OpenIdConnectUserInfo; import com.liferay.portal.kernel.model.User; import com.liferay.portal.kernel.service.UserLocalService; import org.osgi.service.component.annotations.Reference; public class CustomOIDCUserMapper implements OIDCUserMapper { @Reference private UserLocalService _userLocalService; @Override public User mapUserInfo(OpenIdConnectUserInfo openIdConnectUserInfo, long companyId, long creatorUserId) throws Exception { // 提取OIDC返回的用户数据 String email = openIdConnectUserInfo.getEmail(); String fullName = openIdConnectUserInfo.getFullName(); // 自定义属性可通过getAttributes获取 String customAttr = openIdConnectUserInfo.getAttributes().get("custom_attr"); // 检查用户是否已存在,按需创建或更新 User existingUser = _userLocalService.fetchUserByEmailAddress(companyId, email); if (existingUser == null) { // 创建新用户逻辑 return _userLocalService.addUser( creatorUserId, companyId, false, "temp_pwd", "temp_pwd", false, openIdConnectUserInfo.getSubject(), email, 0, "", Locale.getDefault(), fullName.split(" ")[0], "", fullName.split(" ")[1], 0, 0, true, 1, 1, 1970, null, null, null, null, false, null ); } else { // 更新现有用户逻辑 existingUser.setFullName(fullName); existingUser.setExpandoBridgeAttribute("customAttr", customAttr); return _userLocalService.updateUser(existingUser); } } }
注册自定义Mapper
将自定义Mapper注册为OSGi服务,通过设置service.ranking优先级覆盖默认实现:
import org.osgi.service.component.annotations.Component; @Component( immediate = true, property = {"service.ranking:Integer=100"}, service = OIDCUserMapper.class ) public class CustomOIDCUserMapper implements OIDCUserMapper { // 实现代码同上 }
额外注意事项
- 确保项目依赖中已引入
com.liferay.portal.security.sso.openid.connect.api和com.liferay.portal.kernel包。 - 若需处理OIDC返回的自定义扩展属性,可通过
OpenIdConnectUserInfo.getAttributes()获取全部属性后进行映射。
内容的提问来源于stack exchange,提问作者Akash
相关产品推荐
相关产品推荐

