From c242a9fafd4405b5812dd0ba1fdf5d00510a5458 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Thu, 22 May 2025 21:46:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E9=83=A8=E5=88=86=E4=B8=89=E6=96=B9=E8=B4=A6=E5=8F=B7=E9=94=99?= =?UTF-8?q?=E8=AF=AF=EF=BC=88=E6=9A=82=E6=97=B6=E4=BF=AE=E5=A4=8D=EF=BC=8C?= =?UTF-8?q?=E5=90=8E=E7=BB=AD=E9=80=9A=E8=BF=87=E9=87=8D=E5=86=99=20starte?= =?UTF-8?q?r=20=E8=A7=A3=E5=86=B3=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/UserProfileController.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/continew-webapi/src/main/java/top/continew/admin/controller/system/UserProfileController.java b/continew-webapi/src/main/java/top/continew/admin/controller/system/UserProfileController.java index 932ab7f2..3787796a 100644 --- a/continew-webapi/src/main/java/top/continew/admin/controller/system/UserProfileController.java +++ b/continew-webapi/src/main/java/top/continew/admin/controller/system/UserProfileController.java @@ -16,13 +16,16 @@ package top.continew.admin.controller.system; -import com.xkcoding.justauth.AuthRequestFactory; +import com.xkcoding.justauth.autoconfigure.JustAuthProperties; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.enums.ParameterIn; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; import jakarta.validation.constraints.NotNull; import lombok.RequiredArgsConstructor; +import me.zhyd.oauth.AuthRequestBuilder; +import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.model.AuthCallback; import me.zhyd.oauth.model.AuthResponse; import me.zhyd.oauth.model.AuthUser; @@ -41,10 +44,10 @@ import top.continew.admin.system.model.req.user.UserPasswordUpdateReq; import top.continew.admin.system.model.req.user.UserPhoneUpdateReq; import top.continew.admin.system.model.resp.AvatarResp; import top.continew.admin.system.model.resp.user.UserSocialBindResp; -import top.continew.admin.system.service.NoticeService; import top.continew.admin.system.service.UserService; import top.continew.admin.system.service.UserSocialService; import top.continew.starter.cache.redisson.util.RedisUtils; +import top.continew.starter.core.exception.BadRequestException; import top.continew.starter.core.util.ExceptionUtils; import top.continew.starter.core.validation.ValidationUtils; @@ -68,8 +71,7 @@ public class UserProfileController { private static final String CAPTCHA_EXPIRED = "验证码已失效"; private final UserService userService; private final UserSocialService userSocialService; - private final NoticeService noticeService; - private final AuthRequestFactory authRequestFactory; + private final JustAuthProperties authProperties; @Operation(summary = "修改头像", description = "用户修改个人头像") @PatchMapping("/avatar") @@ -113,7 +115,7 @@ public class UserProfileController { @Operation(summary = "修改邮箱", description = "修改用户邮箱") @PatchMapping("/email") - public void updateEmail(@Validated @RequestBody UserEmailUpdateReq updateReq) { + public void updateEmail(@Valid @RequestBody UserEmailUpdateReq updateReq) { String rawOldPassword = ExceptionUtils.exToNull(() -> SecureUtils.decryptByRsaPrivateKey(updateReq .getOldPassword())); ValidationUtils.throwIfBlank(rawOldPassword, DECRYPT_FAILED); @@ -142,7 +144,7 @@ public class UserProfileController { @Parameter(name = "source", description = "来源", example = "gitee", in = ParameterIn.PATH) @PostMapping("/social/{source}") public void bindSocial(@PathVariable String source, @RequestBody AuthCallback callback) { - AuthRequest authRequest = authRequestFactory.get(source); + AuthRequest authRequest = this.getAuthRequest(source); AuthResponse response = authRequest.login(callback); ValidationUtils.throwIf(!response.ok(), response.getMsg()); AuthUser authUser = response.getData(); @@ -155,4 +157,13 @@ public class UserProfileController { public void unbindSocial(@PathVariable String source) { userSocialService.deleteBySourceAndUserId(source, UserContextHolder.getUserId()); } + + private AuthRequest getAuthRequest(String source) { + try { + AuthConfig authConfig = authProperties.getType().get(source.toUpperCase()); + return AuthRequestBuilder.builder().source(source).authConfig(authConfig).build(); + } catch (Exception e) { + throw new BadRequestException("暂不支持 [%s] 平台账号登录".formatted(source)); + } + } }