From 13262aa4b1aee8c34728aa3ae8645f4ac3ad45fb Mon Sep 17 00:00:00 2001 From: figmar Date: Tue, 1 Sep 2026 19:22:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(gateway):=20casdoor=20provisioning=20?= =?UTF-8?q?=E2=80=94=20=E7=A9=BA=20email=20=E8=90=BD=20NULL=20=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E5=94=AF=E4=B8=80=E9=94=AE=E5=86=B2=E7=AA=81=EF=BC=9B?= =?UTF-8?q?race=20=E5=88=86=E6=94=AF=E6=9F=A5=E9=87=8D=E4=B8=BA=E7=A9=BA?= =?UTF-8?q?=E6=97=B6=E6=98=BE=E5=BC=8F=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../infrastructure/casdoor/CasdoorUserService.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/backend/api-gateway/src/main/java/com/deepdata/gateway/infrastructure/casdoor/CasdoorUserService.java b/backend/api-gateway/src/main/java/com/deepdata/gateway/infrastructure/casdoor/CasdoorUserService.java index 4e61640..f0fb29d 100644 --- a/backend/api-gateway/src/main/java/com/deepdata/gateway/infrastructure/casdoor/CasdoorUserService.java +++ b/backend/api-gateway/src/main/java/com/deepdata/gateway/infrastructure/casdoor/CasdoorUserService.java @@ -84,6 +84,11 @@ public class CasdoorUserService { // 并发首登:另一请求已建号 → 复用,避免唯一键冲突 log.info("casdoor provision race on [{}], reusing existing user", username); created = findByUsername(username); + if (created == null) { + // 冲突来自 email 唯一键(用户名不冲突)→ 显式报错,避免 NPE + throw new CasdoorProvisionException( + "unique-key conflict but user not found (likely email collision): " + username); + } } log.info("casdoor auto-provisioned local user [{}] role={} groups={}", username, created.getRole(), created.getUserGroups()); @@ -111,7 +116,9 @@ public class CasdoorUserService { private User createUser(IdTokenClaims claims, String username, LocalDateTime now) { User user = new User(); user.setUsername(username); - user.setEmail(claims.email()); + // email 列有唯一约束:空串会互相冲突,空值统一落 NULL(NULL 不参与唯一约束) + String email = claims.email(); + user.setEmail(email == null || email.isBlank() ? null : email.trim()); user.setFullName(claims.name()); // 本地密码不可用:随机占位(登录统一走 Casdoor,本地 BCrypt 仅迁移期兜底) user.setPasswordHash(new BCryptPasswordEncoder(10).encode(UUID.randomUUID().toString()));