From 16c369c82a676012022310ca5d52f6aa749a08e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E9=B9=8F?= Date: Thu, 13 Feb 2025 09:04:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=8C=E7=BB=B4=E7=A0=81=E5=BF=AB=E9=80=9F?= =?UTF-8?q?=E7=99=BB=E9=99=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admin/auth/AuthController.java | 56 ++++++++++++++++++- .../admin/auth/vo/AuthSocialLoginReqVO.java | 3 + .../service/auth/AdminAuthServiceImpl.java | 2 +- .../service/weixin/TokenValidatorService.java | 4 ++ .../src/main/resources/application-dev.yaml | 8 +-- 5 files changed, 67 insertions(+), 6 deletions(-) diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java index 9f96fe4..df017eb 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java @@ -1,10 +1,15 @@ package cn.iocoder.yudao.module.system.controller.admin.auth; +import cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.common.util.json.JsonUtils; import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission; import cn.iocoder.yudao.framework.security.config.SecurityProperties; import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; @@ -12,11 +17,14 @@ import cn.iocoder.yudao.module.system.api.social.dto.SocialWxQrcodeReqDTO; import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; import cn.iocoder.yudao.module.system.controller.admin.auth.vo.*; import cn.iocoder.yudao.module.system.convert.auth.AuthConvert; +import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO; import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; +import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO; import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum; import cn.iocoder.yudao.module.system.service.auth.AdminAuthService; +import cn.iocoder.yudao.module.system.service.oauth2.OAuth2TokenService; import cn.iocoder.yudao.module.system.service.permission.MenuService; import cn.iocoder.yudao.module.system.service.permission.PermissionService; import cn.iocoder.yudao.module.system.service.permission.RoleService; @@ -28,6 +36,7 @@ import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -38,10 +47,13 @@ import javax.validation.Valid; import java.util.Collections; import java.util.List; import java.util.Set; +import java.util.concurrent.TimeUnit; +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.AUTH_THIRD_LOGIN_NOT_BIND; @Tag(name = "管理后台 - 认证") @RestController @@ -70,6 +82,12 @@ public class AuthController { private SocialUserService socialUserService; + @Resource + private StringRedisTemplate stringRedisTemplate; + + @Resource + private OAuth2TokenService oAuth2TokenService; + @PostMapping("/login") @PermitAll @Operation(summary = "使用账号密码登录") @@ -99,7 +117,7 @@ public class AuthController { } @PermitAll - @PostMapping("/web_login") + @PostMapping("/web_code_login") @Operation(summary = "后台调用小程序微信扫码登录") public CommonResult webLogin(@RequestBody @Valid SocialWxQrcodeReqDTO reqVO) { return success(socialClientService.getWxaQrcode(reqVO)); @@ -198,4 +216,40 @@ public class AuthController { return success(authService.socialLogin(reqVO)); } + + @PostMapping("/social-login-openid") + @PermitAll + @Operation(summary = "社交快捷登录,使用 code 授权码", description = "适合未登录的用户,但是社交账号已绑定用户") + public void socialQuickLoginOpenId(@RequestBody AuthSocialLoginReqVO reqVO) { + OAuth2AccessTokenDO accessToken = oAuth2TokenService.getAccessToken(reqVO.getOpenid()); + Long userId = accessToken.getUserId(); + List socialUserList = socialUserService.getSocialUserList(userId, UserTypeEnum.MEMBER.getValue()); + //判断是否为对象空 + if(!CollUtil.isEmpty(socialUserList)){ + stringRedisTemplate.opsForValue().set(reqVO.getCode(), JsonUtils.toJsonString(socialUserList.get(0)),10, TimeUnit.MINUTES); + } + } + + @PostMapping("/social-qr-login-openid") + @PermitAll + @Operation(summary = "社交快捷登录,使用 code 授权码", description = "适合未登录的用户,但是社交账号已绑定用户") + public CommonResult qrLoginCode(@RequestBody AuthSocialLoginReqVO reqVO) { + String json = stringRedisTemplate.opsForValue().get(reqVO.getCode()); + + //判断是否为对象空 + if(StrUtil.isNotEmpty(json)){ + SocialUserDO socialUserDO = JsonUtils.parseObject(json, SocialUserDO.class); + AuthSocialLoginReqVO authSocialLoginReqVO=new AuthSocialLoginReqVO(); + authSocialLoginReqVO.setCode(socialUserDO.getCode()); + authSocialLoginReqVO.setType(socialUserDO.getType()); + authSocialLoginReqVO.setState(socialUserDO.getState()); + authSocialLoginReqVO.setOpenid(socialUserDO.getOpenid()); + return success(authService.socialLogin(authSocialLoginReqVO)); + } + else{ + return success(null); + } + + } + } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/vo/AuthSocialLoginReqVO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/vo/AuthSocialLoginReqVO.java index 0cacb6f..ad5670e 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/vo/AuthSocialLoginReqVO.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/vo/AuthSocialLoginReqVO.java @@ -34,4 +34,7 @@ public class AuthSocialLoginReqVO { @Schema(description = "用户类型" ) private Integer userType; + @Schema(description = "社交 openid", example = "oz-Jdt0kd_jdhUxJHQdBJMlOFN7w") + private String openid; + } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java index f1e83f7..74192e4 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java @@ -174,7 +174,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { @Override public AuthLoginRespVO socialLogin(AuthSocialLoginReqVO reqVO) { // 使用 code 授权码,进行登录。然后,获得到绑定的用户编号 - SocialUserRespDTO socialUser = socialUserService.getSocialUserByCode(UserTypeEnum.ADMIN.getValue(), reqVO.getType(), + SocialUserRespDTO socialUser = socialUserService.getSocialUserByCode(UserTypeEnum.MEMBER.getValue(), reqVO.getType(), reqVO.getCode(), reqVO.getState()); if (socialUser == null || socialUser.getUserId() == null) { throw exception(AUTH_THIRD_LOGIN_NOT_BIND); diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/weixin/TokenValidatorService.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/weixin/TokenValidatorService.java index 1477435..a6725c4 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/weixin/TokenValidatorService.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/weixin/TokenValidatorService.java @@ -1,8 +1,12 @@ package cn.iocoder.yudao.module.system.service.weixin; +import org.springframework.stereotype.Service; + import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; + +@Service public class TokenValidatorService { public static boolean validateToken(String token, String signature, String timestamp, String nonce) { String[] arr = new String[]{token, timestamp, nonce}; diff --git a/yudao-server/src/main/resources/application-dev.yaml b/yudao-server/src/main/resources/application-dev.yaml index a74c80c..16ab13e 100644 --- a/yudao-server/src/main/resources/application-dev.yaml +++ b/yudao-server/src/main/resources/application-dev.yaml @@ -145,16 +145,16 @@ logging: wx: # 参见 https://github.com/Wechat-Group/WxJava/blob/develop/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md 文档 mp: # 公众号配置(必填) - app-id: wx041349c6f39b268b - secret: 5abee519483bc9f8cb37ce280e814bd0 + app-id: wx6d80755768234f3b + secret: c8180f2ab1b8454d403c7aa336782e21 # 存储配置,解决 AccessToken 的跨节点的共享 config-storage: type: RedisTemplate # 采用 RedisTemplate 操作 Redis,会自动从 Spring 中获取 key-prefix: wx # Redis Key 的前缀 http-client-type: HttpClient # 采用 HttpClient 请求微信公众号平台 miniapp: # 小程序配置(必填),参见 https://github.com/Wechat-Group/WxJava/blob/develop/spring-boot-starters/wx-java-miniapp-spring-boot-starter/README.md 文档 - appid: wx63c280fe3248a3e7 - secret: 6f270509224a7ae1296bbf1c8cb97aed + appid: wx6d80755768234f3b + secret: c8180f2ab1b8454d403c7aa336782e21 config-storage: type: RedisTemplate # 采用 RedisTemplate 操作 Redis,会自动从 Spring 中获取 key-prefix: wa # Redis Key 的前缀