From ea5fd5d9034685196f68f7da875308cdc329d99c Mon Sep 17 00:00:00 2001 From: YunaiV Date: Thu, 22 Oct 2026 01:34:31 +0800 Subject: [PATCH] feat(system): 升级钉钉 V2 社交登录并校验用户标识 --- yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/enums/social/SocialTypeEnum.java | 6 ++++-- yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java | 8 +++++++- yudao-module-system/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImplTest.java | 42 ++++++++++++++++++++++++++++++++++++++++++ yudao-server/src/main/resources/application-dev.yaml | 2 +- yudao-server/src/main/resources/application-local.yaml | 2 +- 5 files changed, 55 insertions(+), 5 deletions(-) diff --git a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/enums/social/SocialTypeEnum.java b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/enums/social/SocialTypeEnum.java index f84ce53..de5d5a7 100644 --- a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/enums/social/SocialTypeEnum.java +++ b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/enums/social/SocialTypeEnum.java @@ -25,9 +25,11 @@ public enum SocialTypeEnum implements ArrayValuable { /** * 钉钉 * - * @see 接入文档 + * 使用新版登录,对应 JustAuth 的 DINGTALK_V2,相关反馈:https://t.zsxq.com/QY7xI + * + * @see 接入文档 */ - DINGTALK(20, "DINGTALK"), + DINGTALK(20, "DINGTALK_V2"), /** * 企业微信 diff --git a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java index a083477..e3e9539 100644 --- a/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java +++ b/yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java @@ -16,6 +16,7 @@ import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.util.DesensitizedUtil; import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.ReflectUtil; +import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.pojo.PageResult; @@ -187,7 +188,12 @@ public class SocialClientServiceImpl implements SocialClientService { if (!authResponse.ok()) { throw exception(SOCIAL_USER_AUTH_FAILURE, authResponse.getMsg()); } - return (AuthUser) authResponse.getData(); + // 部分平台获取用户信息失败时,仍然返回成功,但没有用户标识。例如说,钉钉新版登录缺少权限时,接口返回 403,但 JustAuth 不校验 + AuthUser authUser = (AuthUser) authResponse.getData(); + if (authUser == null || StrUtil.isBlank(authUser.getUuid())) { + throw exception(SOCIAL_USER_AUTH_FAILURE, "未获取到用户标识"); + } + return authUser; } /** diff --git a/yudao-module-system/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImplTest.java b/yudao-module-system/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImplTest.java index 9d00119..6172969 100644 --- a/yudao-module-system/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImplTest.java +++ b/yudao-module-system/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImplTest.java @@ -146,6 +146,48 @@ public class SocialClientServiceImplTest extends BaseDbUnitTest { } @Test + public void testAuthSocialUser_uuidNull() { + // 准备参数 + Integer socialType = SocialTypeEnum.DINGTALK.getType(); + Integer userType = randomPojo(UserTypeEnum.class).getValue(); + String code = randomString(); + String state = randomString(); + // mock 方法(AuthRequest) + AuthRequest authRequest = mock(AuthRequest.class); + when(authRequestFactory.get(eq("DINGTALK_V2"))).thenReturn(authRequest); + // mock 方法(AuthResponse):获取用户信息失败时,JustAuth 仍然返回成功,但 uuid 为空 + AuthUser authUser = randomPojo(AuthUser.class, o -> o.setUuid(null)); + AuthResponse authResponse = new AuthResponse<>(2000, null, authUser); + when(authRequest.login(any())).thenReturn(authResponse); + + // 调用并断言 + assertServiceException( + () -> socialClientService.getAuthUser(socialType, userType, code, state), + SOCIAL_USER_AUTH_FAILURE, "未获取到用户标识"); + } + + @Test + public void testAuthSocialUser_uuidBlank() { + // 准备参数 + Integer socialType = SocialTypeEnum.DINGTALK.getType(); + Integer userType = randomPojo(UserTypeEnum.class).getValue(); + String code = randomString(); + String state = randomString(); + // mock 方法(AuthRequest) + AuthRequest authRequest = mock(AuthRequest.class); + when(authRequestFactory.get(eq("DINGTALK_V2"))).thenReturn(authRequest); + // mock 方法(AuthResponse) + AuthUser authUser = randomPojo(AuthUser.class, o -> o.setUuid(" ")); + AuthResponse authResponse = new AuthResponse<>(2000, null, authUser); + when(authRequest.login(any())).thenReturn(authResponse); + + // 调用并断言 + assertServiceException( + () -> socialClientService.getAuthUser(socialType, userType, code, state), + SOCIAL_USER_AUTH_FAILURE, "未获取到用户标识"); + } + + @Test public void testBuildAuthRequest_clientNull() { // 准备参数 Integer socialType = SocialTypeEnum.WECHAT_MP.getType(); diff --git a/yudao-server/src/main/resources/application-dev.yaml b/yudao-server/src/main/resources/application-dev.yaml index ce4d787..98fc077 100644 --- a/yudao-server/src/main/resources/application-dev.yaml +++ b/yudao-server/src/main/resources/application-dev.yaml @@ -181,7 +181,7 @@ yudao: justauth: enabled: true type: - DINGTALK: # 钉钉 + DINGTALK_V2: # 钉钉(新版登录) client-id: dingvrnreaje3yqvzhxg client-secret: i8E6iZyDvZj51JIb0tYsYfVQYOks9Cq1lgryEjFRqC79P3iJcrxEwT6Qk2QvLrLI ignore-check-redirect-uri: true diff --git a/yudao-server/src/main/resources/application-local.yaml b/yudao-server/src/main/resources/application-local.yaml index c8d4244..7170af9 100644 --- a/yudao-server/src/main/resources/application-local.yaml +++ b/yudao-server/src/main/resources/application-local.yaml @@ -254,7 +254,7 @@ yudao: justauth: enabled: true type: - DINGTALK: # 钉钉 + DINGTALK_V2: # 钉钉(新版登录) client-id: dingvrnreaje3yqvzhxg client-secret: i8E6iZyDvZj51JIb0tYsYfVQYOks9Cq1lgryEjFRqC79P3iJcrxEwT6Qk2QvLrLI ignore-check-redirect-uri: true -- libgit2 0.21.4