From fcf1c228c58a727497bcc005f4ef5f06d733647e Mon Sep 17 00:00:00 2001 From: timfruit Date: Tue, 26 Oct 2021 21:35:06 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=85=AC=E4=BC=97=E5=8F=B7=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yudao-admin-server/pom.xml | 6 -- .../controller/auth/SysAuthController.java | 5 +- .../auth/vo/auth/SysAuthSocialBindReqVO.java | 2 +- .../vo/auth/SysAuthSocialLogin2ReqVO.java | 2 +- .../auth/vo/auth/SysAuthSocialLoginReqVO.java | 2 +- .../dal/redis/SysRedisKeyConstants.java | 7 -- .../system/enums/SysErrorCodeConstants.java | 4 +- .../service/auth/impl/SysAuthServiceImpl.java | 16 ++-- yudao-core-service/pom.xml | 6 ++ .../dataobject/social/SysSocialUserDO.java | 2 +- .../dal/mysql/social/SysSocialUserMapper.java | 4 +- .../dal/redis/SysRedisKeyCoreConstants.java | 10 +++ .../social/SysSocialAuthUserRedisDAO.java | 7 +- .../system/enums/SysErrorCodeConstants.java | 5 ++ .../enums/social/SysSocialTypeEnum.java | 12 ++- .../service/social/SysSocialService.java | 15 ++-- .../social/impl/SysSocialServiceImpl.java | 40 +++++----- .../enums/social/SysSocialTypeEnum.java | 55 -------------- .../controller/auth/SysAuthController.java | 48 +++++++----- .../auth/vo/MbrAuthSocialBindReqVO.java | 2 +- .../auth/vo/MbrAuthSocialLogin2ReqVO.java | 2 +- .../auth/vo/MbrAuthSocialLoginReqVO.java | 2 +- .../auth/vo/MbrAuthSocialUnbindReqVO.java | 2 +- .../system/enums/SysErrorCodeConstants.java | 4 + .../system/service/auth/SysAuthService.java | 32 +++++++- .../service/auth/impl/SysAuthServiceImpl.java | 74 ++++++++++++++++++- .../src/main/resources/application-dev.yaml | 27 +++++++ .../src/main/resources/application-local.yaml | 26 +++++++ 28 files changed, 275 insertions(+), 144 deletions(-) rename {yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver => yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice}/modules/system/dal/dataobject/social/SysSocialUserDO.java (96%) rename {yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver => yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice}/modules/system/dal/mysql/social/SysSocialUserMapper.java (89%) rename {yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver => yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice}/modules/system/dal/redis/social/SysSocialAuthUserRedisDAO.java (83%) rename {yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver => yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice}/modules/system/enums/social/SysSocialTypeEnum.java (74%) rename {yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver => yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice}/modules/system/service/social/SysSocialService.java (83%) rename {yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver => yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice}/modules/system/service/social/impl/SysSocialServiceImpl.java (84%) delete mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/enums/social/SysSocialTypeEnum.java diff --git a/yudao-admin-server/pom.xml b/yudao-admin-server/pom.xml index 120d3e926..bfccf8092 100644 --- a/yudao-admin-server/pom.xml +++ b/yudao-admin-server/pom.xml @@ -122,12 +122,6 @@ screw-core - - - com.xkcoding.justauth - justauth-spring-boot-starter - 1.4.0 - diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/SysAuthController.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/SysAuthController.java index b87deaca9..16130e17e 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/SysAuthController.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/SysAuthController.java @@ -8,10 +8,11 @@ import cn.iocoder.yudao.adminserver.modules.system.enums.permission.MenuTypeEnum import cn.iocoder.yudao.adminserver.modules.system.service.auth.SysAuthService; import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService; import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysRoleService; -import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService; import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO; +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService; import cn.iocoder.yudao.coreservice.modules.system.service.user.SysUserCoreService; 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.util.collection.SetUtils; import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; @@ -132,7 +133,7 @@ public class SysAuthController { @DeleteMapping("/social-unbind") @ApiOperation("取消社交绑定") public CommonResult socialUnbind(@RequestBody SysAuthSocialUnbindReqVO reqVO) { - socialService.unbindSocialUser(getLoginUserId(), reqVO.getType(), reqVO.getUnionId()); + socialService.unbindSocialUser(getLoginUserId(), reqVO.getType(), reqVO.getUnionId(), UserTypeEnum.ADMIN); return CommonResult.success(true); } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialBindReqVO.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialBindReqVO.java index 66d05cedf..e92d3fc50 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialBindReqVO.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialBindReqVO.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth; -import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum; +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; import cn.iocoder.yudao.framework.common.validation.InEnum; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialLogin2ReqVO.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialLogin2ReqVO.java index bedf8ba3e..13aaa71cc 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialLogin2ReqVO.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialLogin2ReqVO.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth; -import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum; +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; import cn.iocoder.yudao.framework.common.validation.InEnum; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialLoginReqVO.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialLoginReqVO.java index b34022d4f..71d19685d 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialLoginReqVO.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialLoginReqVO.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth; -import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum; +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; import cn.iocoder.yudao.framework.common.validation.InEnum; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/SysRedisKeyConstants.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/SysRedisKeyConstants.java index e7c484bb0..e9d3373b2 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/SysRedisKeyConstants.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/SysRedisKeyConstants.java @@ -19,12 +19,5 @@ public interface SysRedisKeyConstants { "captcha_code:%s", // 参数为 uuid STRING, String.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC); - RedisKeyDefine SOCIAL_AUTH_USER = new RedisKeyDefine("社交的授权用户", - "social_auth_user:%d:%s", // 参数为 type,code - STRING, AuthUser.class, Duration.ofDays(1)); - - RedisKeyDefine SOCIAL_AUTH_STATE = new RedisKeyDefine("社交的 state", - "social_auth_state:%s", // 参数为 state - STRING, String.class, Duration.ofHours(24)); // 值为 state } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/enums/SysErrorCodeConstants.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/enums/SysErrorCodeConstants.java index 8a606e062..3b4051d20 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/enums/SysErrorCodeConstants.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/enums/SysErrorCodeConstants.java @@ -90,8 +90,6 @@ public interface SysErrorCodeConstants { ErrorCode ERROR_CODE_NOT_EXISTS = new ErrorCode(1002013000, "错误码不存在"); ErrorCode ERROR_CODE_DUPLICATE = new ErrorCode(1002013001, "已经存在编码为【{}】的错误码"); - // ========== 社交模块 1002014000 ========== - ErrorCode SOCIAL_AUTH_FAILURE = new ErrorCode(1002014000, "社交授权失败,原因是:{}"); - ErrorCode SOCIAL_UNBIND_NOT_SELF = new ErrorCode(1002014001, "社交解绑失败,非当前用户绑定"); + } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java index d5fb91f95..0bf08e8c2 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java @@ -6,18 +6,18 @@ import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth.SysAu import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth.SysAuthSocialLogin2ReqVO; import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth.SysAuthSocialLoginReqVO; import cn.iocoder.yudao.adminserver.modules.system.convert.auth.SysAuthConvert; -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; import cn.iocoder.yudao.adminserver.modules.system.enums.logger.SysLoginLogTypeEnum; import cn.iocoder.yudao.adminserver.modules.system.enums.logger.SysLoginResultEnum; import cn.iocoder.yudao.adminserver.modules.system.service.auth.SysAuthService; import cn.iocoder.yudao.adminserver.modules.system.service.common.SysCaptchaService; import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService; -import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService; import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO; import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO; import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService; import cn.iocoder.yudao.coreservice.modules.system.service.logger.SysLoginLogCoreService; import cn.iocoder.yudao.coreservice.modules.system.service.logger.dto.SysLoginLogCreateReqDTO; +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService; import cn.iocoder.yudao.coreservice.modules.system.service.user.SysUserCoreService; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; @@ -75,6 +75,8 @@ public class SysAuthServiceImpl implements SysAuthService { @Resource private SysSocialService socialService; + private static final UserTypeEnum userTypeEnum = UserTypeEnum.ADMIN; + @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { // 获取 username 对应的 SysUserDO @@ -198,7 +200,7 @@ public class SysAuthServiceImpl implements SysAuthService { // 如果未绑定 SysSocialUserDO 用户,则无法自动登录,进行报错 String unionId = socialService.getAuthUserUnionId(authUser); - List socialUsers = socialService.getAllSocialUserList(reqVO.getType(), unionId); + List socialUsers = socialService.getAllSocialUserList(reqVO.getType(), unionId, userTypeEnum); if (CollUtil.isEmpty(socialUsers)) { throw exception(AUTH_THIRD_LOGIN_NOT_BIND); } @@ -216,7 +218,7 @@ public class SysAuthServiceImpl implements SysAuthService { loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 // 绑定社交用户(更新) - socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser); + socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum); // 缓存登录用户到 Redis 中,返回 sessionId 编号 return userSessionCoreService.createUserSession(loginUser, userIp, userAgent); @@ -233,7 +235,7 @@ public class SysAuthServiceImpl implements SysAuthService { loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 // 绑定社交用户(新增) - socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser); + socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum); // 缓存登录用户到 Redis 中,返回 sessionId 编号 return userSessionCoreService.createUserSession(loginUser, userIp, userAgent); @@ -246,7 +248,7 @@ public class SysAuthServiceImpl implements SysAuthService { Assert.notNull(authUser, "授权用户不为空"); // 绑定社交用户(新增) - socialService.bindSocialUser(userId, reqVO.getType(), authUser); + socialService.bindSocialUser(userId, reqVO.getType(), authUser, userTypeEnum); } @Override @@ -267,7 +269,7 @@ public class SysAuthServiceImpl implements SysAuthService { reqDTO.setLogType(SysLoginLogTypeEnum.LOGOUT_SELF.getType()); reqDTO.setTraceId(TracerUtils.getTraceId()); reqDTO.setUserId(userId); - reqDTO.setUserType(UserTypeEnum.ADMIN.getValue()); + reqDTO.setUserType(userTypeEnum.getValue()); reqDTO.setUsername(username); reqDTO.setUserAgent(ServletUtils.getUserAgent()); reqDTO.setUserIp(ServletUtils.getClientIP()); diff --git a/yudao-core-service/pom.xml b/yudao-core-service/pom.xml index 43cdae127..42a7d9627 100644 --- a/yudao-core-service/pom.xml +++ b/yudao-core-service/pom.xml @@ -85,6 +85,12 @@ com.google.guava guava + + + com.xkcoding.justauth + justauth-spring-boot-starter + 1.4.0 + diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/social/SysSocialUserDO.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/dataobject/social/SysSocialUserDO.java similarity index 96% rename from yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/social/SysSocialUserDO.java rename to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/dataobject/social/SysSocialUserDO.java index 7e998b8fc..d1772a132 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/social/SysSocialUserDO.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/dataobject/social/SysSocialUserDO.java @@ -1,4 +1,4 @@ -package cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social; +package cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social; import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/social/SysSocialUserMapper.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/social/SysSocialUserMapper.java similarity index 89% rename from yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/social/SysSocialUserMapper.java rename to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/social/SysSocialUserMapper.java index b5762ec7f..43fc4a790 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/social/SysSocialUserMapper.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/social/SysSocialUserMapper.java @@ -1,6 +1,6 @@ -package cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social; +package cn.iocoder.yudao.coreservice.modules.system.dal.mysql.social; -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import org.apache.ibatis.annotations.Mapper; diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/SysRedisKeyCoreConstants.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/SysRedisKeyCoreConstants.java index fb4118874..8076980d3 100644 --- a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/SysRedisKeyCoreConstants.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/SysRedisKeyCoreConstants.java @@ -2,6 +2,9 @@ package cn.iocoder.yudao.coreservice.modules.system.dal.redis; import cn.iocoder.yudao.framework.redis.core.RedisKeyDefine; import cn.iocoder.yudao.framework.security.core.LoginUser; +import me.zhyd.oauth.model.AuthUser; + +import java.time.Duration; import static cn.iocoder.yudao.framework.redis.core.RedisKeyDefine.KeyTypeEnum.STRING; @@ -16,4 +19,11 @@ public interface SysRedisKeyCoreConstants { "login_user:%s", // 参数为 sessionId STRING, LoginUser.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC); + RedisKeyDefine SOCIAL_AUTH_USER = new RedisKeyDefine("社交的授权用户", + "social_auth_user:%d:%s", // 参数为 type,code + STRING, AuthUser.class, Duration.ofDays(1)); + + RedisKeyDefine SOCIAL_AUTH_STATE = new RedisKeyDefine("社交的 state", + "social_auth_state:%s", // 参数为 state + STRING, String.class, Duration.ofHours(24)); // 值为 state } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/social/SysSocialAuthUserRedisDAO.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/social/SysSocialAuthUserRedisDAO.java similarity index 83% rename from yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/social/SysSocialAuthUserRedisDAO.java rename to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/social/SysSocialAuthUserRedisDAO.java index d5e84e62d..e82c81394 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/social/SysSocialAuthUserRedisDAO.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/social/SysSocialAuthUserRedisDAO.java @@ -1,4 +1,4 @@ -package cn.iocoder.yudao.adminserver.modules.system.dal.redis.social; +package cn.iocoder.yudao.coreservice.modules.system.dal.redis.social; import cn.iocoder.yudao.framework.common.util.json.JsonUtils; import me.zhyd.oauth.model.AuthCallback; @@ -8,10 +8,11 @@ import org.springframework.stereotype.Repository; import javax.annotation.Resource; -import static cn.iocoder.yudao.adminserver.modules.system.dal.redis.SysRedisKeyConstants.SOCIAL_AUTH_USER; +import static cn.iocoder.yudao.coreservice.modules.system.dal.redis.SysRedisKeyCoreConstants.SOCIAL_AUTH_USER; + /** - * 社交 {@link me.zhyd.oauth.model.AuthUser} 的 RedisDAO + * 社交 {@link AuthUser} 的 RedisDAO * * @author 芋道源码 */ diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/SysErrorCodeConstants.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/SysErrorCodeConstants.java index 3c1fc87ef..a7231f421 100644 --- a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/SysErrorCodeConstants.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/SysErrorCodeConstants.java @@ -14,4 +14,9 @@ public interface SysErrorCodeConstants { ErrorCode SMS_SEND_MOBILE_TEMPLATE_PARAM_MISS = new ErrorCode(1006000001, "模板参数({})缺失"); ErrorCode SMS_SEND_TEMPLATE_NOT_EXISTS = new ErrorCode(1006000000, "短信模板不存在"); + + // ========== 社交模块 1006001000 ========== + ErrorCode SOCIAL_AUTH_FAILURE = new ErrorCode(1006001000, "社交授权失败,原因是:{}"); + ErrorCode SOCIAL_UNBIND_NOT_SELF = new ErrorCode(1006001001, "社交解绑失败,非当前用户绑定"); + } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/enums/social/SysSocialTypeEnum.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java similarity index 74% rename from yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/enums/social/SysSocialTypeEnum.java rename to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java index 87d762dce..592c274a1 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/enums/social/SysSocialTypeEnum.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java @@ -1,4 +1,4 @@ -package cn.iocoder.yudao.adminserver.modules.system.enums.social; +package cn.iocoder.yudao.coreservice.modules.system.enums.social; import cn.hutool.core.collection.ListUtil; import cn.hutool.core.util.ArrayUtil; @@ -21,11 +21,19 @@ public enum SysSocialTypeEnum implements IntArrayValuable { GITEE(10, "GITEE"), // https://gitee.com/api/v5/oauth_doc#/ DINGTALK(20, "DINGTALK"), // https://developers.dingtalk.com/document/app/obtain-identity-credentials WECHAT_ENTERPRISE(30, "WECHAT_ENTERPRISE"), // https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html + /** + * 微信公众平台 - H5 + */ + WECHAT_MP(12, "WECHAT_MP"), // https://www.cnblogs.com/juewuzhe/p/11905461.html + /** + * 微信开放平台 - 小程序 + */ + WECHAT_OPEN(11, "WECHAT_OPEN"), // https://justauth.wiki/guide/oauth/wechat_open/#_2-%E7%94%B3%E8%AF%B7%E5%BC%80%E5%8F%91%E8%80%85%E8%B5%84%E8%B4%A8%E8%AE%A4%E8%AF%81 ; public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(SysSocialTypeEnum::getType).toArray(); - public static final List WECHAT_ALL = ListUtil.toList(WECHAT_ENTERPRISE.type); + public static final List WECHAT_ALL = ListUtil.toList(WECHAT_ENTERPRISE.type, WECHAT_MP.type, WECHAT_OPEN.type); /** * 类型 diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialService.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/SysSocialService.java similarity index 83% rename from yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialService.java rename to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/SysSocialService.java index 44dfad63a..81ee31ecf 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialService.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/SysSocialService.java @@ -1,8 +1,9 @@ -package cn.iocoder.yudao.adminserver.modules.system.service.social; +package cn.iocoder.yudao.coreservice.modules.system.service.social; import cn.hutool.core.util.StrUtil; -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; -import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO; +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.exception.ServiceException; import me.zhyd.oauth.model.AuthUser; @@ -49,7 +50,7 @@ public interface SysSocialService { * @param unionId 社交平台的 unionId * @return 社交用户列表 */ - List getAllSocialUserList(Integer type, String unionId); + List getAllSocialUserList(Integer type, String unionId, UserTypeEnum userTypeEnum); /** * 获得指定用户的社交用户列表 @@ -57,7 +58,7 @@ public interface SysSocialService { * @param userId 用户编号 * @return 社交用户列表 */ - List getSocialUserList(Long userId); + List getSocialUserList(Long userId,UserTypeEnum userTypeEnum); /** * 绑定社交用户 @@ -66,7 +67,7 @@ public interface SysSocialService { * @param type 社交平台的类型 {@link SysSocialTypeEnum} * @param authUser 授权用户 */ - void bindSocialUser(Long userId, Integer type, AuthUser authUser); + void bindSocialUser(Long userId, Integer type, AuthUser authUser, UserTypeEnum userTypeEnum); /** * 取消绑定社交用户 @@ -75,6 +76,6 @@ public interface SysSocialService { * @param type 社交平台的类型 {@link SysSocialTypeEnum} * @param unionId 社交平台的 unionId */ - void unbindSocialUser(Long userId, Integer type, String unionId); + void unbindSocialUser(Long userId, Integer type, String unionId,UserTypeEnum userTypeEnum); } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/impl/SysSocialServiceImpl.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/impl/SysSocialServiceImpl.java similarity index 84% rename from yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/impl/SysSocialServiceImpl.java rename to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/impl/SysSocialServiceImpl.java index 291015aa5..2bc8a7385 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/impl/SysSocialServiceImpl.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/impl/SysSocialServiceImpl.java @@ -1,11 +1,11 @@ -package cn.iocoder.yudao.adminserver.modules.system.service.social.impl; +package cn.iocoder.yudao.coreservice.modules.system.service.social.impl; import cn.hutool.core.collection.CollUtil; -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; -import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social.SysSocialUserMapper; -import cn.iocoder.yudao.adminserver.modules.system.dal.redis.social.SysSocialAuthUserRedisDAO; -import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum; -import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO; +import cn.iocoder.yudao.coreservice.modules.system.dal.mysql.social.SysSocialUserMapper; +import cn.iocoder.yudao.coreservice.modules.system.dal.redis.social.SysSocialAuthUserRedisDAO; +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.iocoder.yudao.framework.common.util.http.HttpUtils; @@ -25,8 +25,8 @@ import javax.validation.Valid; import java.util.List; import java.util.Objects; -import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.SOCIAL_AUTH_FAILURE; -import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.SOCIAL_UNBIND_NOT_SELF; +import static cn.iocoder.yudao.coreservice.modules.system.enums.SysErrorCodeConstants.SOCIAL_AUTH_FAILURE; +import static cn.iocoder.yudao.coreservice.modules.system.enums.SysErrorCodeConstants.SOCIAL_UNBIND_NOT_SELF; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; @@ -75,25 +75,25 @@ public class SysSocialServiceImpl implements SysSocialService { } @Override - public List getAllSocialUserList(Integer type, String unionId) { + public List getAllSocialUserList(Integer type, String unionId,UserTypeEnum userTypeEnum) { List types = SysSocialTypeEnum.getRelationTypes(type); - return socialUserMapper.selectListByTypeAndUnionId(UserTypeEnum.ADMIN.getValue(), types, unionId); + return socialUserMapper.selectListByTypeAndUnionId(userTypeEnum.getValue(), types, unionId); } @Override - public List getSocialUserList(Long userId) { - return socialUserMapper.selectListByUserId(UserTypeEnum.ADMIN.getValue(), userId); + public List getSocialUserList(Long userId,UserTypeEnum userTypeEnum) { + return socialUserMapper.selectListByUserId(userTypeEnum.getValue(), userId); } @Override @Transactional - public void bindSocialUser(Long userId, Integer type, AuthUser authUser) { + public void bindSocialUser(Long userId, Integer type, AuthUser authUser,UserTypeEnum userTypeEnum) { // 获得 unionId 对应的 SysSocialUserDO 列表 String unionId = getAuthUserUnionId(authUser); - List socialUsers = this.getAllSocialUserList(type, unionId); + List socialUsers = this.getAllSocialUserList(type, unionId, userTypeEnum); // 逻辑一:如果 userId 之前绑定过该 type 的其它账号,需要进行解绑 - this.unbindOldSocialUser(userId, type, unionId); + this.unbindOldSocialUser(userId, type, unionId, userTypeEnum); // 逻辑二:如果 socialUsers 指定的 userId 改变,需要进行更新 // 例如说,一个微信 unionId 对应了多个社交账号,结果其中有个关联了新的 userId,则其它也要跟着修改 @@ -112,7 +112,7 @@ public class SysSocialServiceImpl implements SysSocialService { .nickname(authUser.getNickname()).avatar(authUser.getAvatar()).rawUserInfo(toJsonString(authUser.getRawUserInfo())) .build(); if (socialUser == null) { - saveSocialUser.setUserId(userId).setUserType(UserTypeEnum.ADMIN.getValue()) + saveSocialUser.setUserId(userId).setUserType(userTypeEnum.getValue()) .setType(type).setOpenid(authUser.getUuid()).setUnionId(unionId); socialUserMapper.insert(saveSocialUser); } else { @@ -122,9 +122,9 @@ public class SysSocialServiceImpl implements SysSocialService { } @Override - public void unbindSocialUser(Long userId, Integer type, String unionId) { + public void unbindSocialUser(Long userId, Integer type, String unionId, UserTypeEnum userTypeEnum) { // 获得 unionId 对应的所有 SysSocialUserDO 社交用户 - List socialUsers = this.getAllSocialUserList(type, unionId); + List socialUsers = this.getAllSocialUserList(type, unionId, userTypeEnum); if (CollUtil.isEmpty(socialUsers)) { return; } @@ -140,10 +140,10 @@ public class SysSocialServiceImpl implements SysSocialService { } @VisibleForTesting - public void unbindOldSocialUser(Long userId, Integer type, String newUnionId) { + public void unbindOldSocialUser(Long userId, Integer type, String newUnionId, UserTypeEnum userTypeEnum) { List types = SysSocialTypeEnum.getRelationTypes(type); List oldSocialUsers = socialUserMapper.selectListByTypeAndUserId( - UserTypeEnum.ADMIN.getValue(), types, userId); + userTypeEnum.getValue(), types, userId); // 如果新老的 unionId 是一致的,说明无需解绑 if (CollUtil.isEmpty(oldSocialUsers) || Objects.equals(newUnionId, oldSocialUsers.get(0).getUnionId())) { return; diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/enums/social/SysSocialTypeEnum.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/enums/social/SysSocialTypeEnum.java deleted file mode 100644 index fe93f39c8..000000000 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/enums/social/SysSocialTypeEnum.java +++ /dev/null @@ -1,55 +0,0 @@ -package cn.iocoder.yudao.userserver.modules.member.enums.social; - -import cn.hutool.core.collection.ListUtil; -import cn.hutool.core.util.ArrayUtil; -import cn.iocoder.yudao.framework.common.core.IntArrayValuable; -import lombok.AllArgsConstructor; -import lombok.Getter; - -import java.util.Arrays; -import java.util.List; - -/** - * 社交平台的类型枚举 - * - * @author 芋道源码 - */ -@Getter -@AllArgsConstructor -public enum SysSocialTypeEnum implements IntArrayValuable { - - GITEE(10, "GITEE"), // https://gitee.com/api/v5/oauth_doc#/ - DINGTALK(20, "DINGTALK"), // https://developers.dingtalk.com/document/app/obtain-identity-credentials - WECHAT_ENTERPRISE(30, "WECHAT_ENTERPRISE"), // https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html - ; - - public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(SysSocialTypeEnum::getType).toArray(); - - public static final List WECHAT_ALL = ListUtil.toList(WECHAT_ENTERPRISE.type); - - /** - * 类型 - */ - private final Integer type; - /** - * 类型的标识 - */ - private final String source; - - @Override - public int[] array() { - return ARRAYS; - } - - public static SysSocialTypeEnum valueOfType(Integer type) { - return ArrayUtil.firstMatch(o -> o.getType().equals(type), values()); - } - - public static List getRelationTypes(Integer type) { - if (WECHAT_ALL.contains(type)) { - return WECHAT_ALL; - } - return ListUtil.toList(type); - } - -} diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java index 2b1fd6161..377dd9966 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java @@ -1,9 +1,13 @@ package cn.iocoder.yudao.userserver.modules.system.controller.auth; +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService; +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.*; import cn.iocoder.yudao.userserver.modules.system.service.auth.SysAuthService; import cn.iocoder.yudao.userserver.modules.system.service.sms.SysSmsCodeService; +import com.alibaba.fastjson.JSON; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; @@ -13,11 +17,13 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP; import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getUserAgent; +import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; @Api(tags = "认证") @RestController @@ -30,6 +36,9 @@ public class SysAuthController { private SysAuthService authService; @Resource private SysSmsCodeService smsCodeService; + @Resource + private SysSocialService socialService; + @PostMapping("/login") @ApiOperation("使用手机 + 密码登录") @@ -70,42 +79,47 @@ public class SysAuthController { }) public CommonResult socialAuthRedirect(@RequestParam("type") Integer type, @RequestParam("redirectUri") String redirectUri) { -// return CommonResult.success(socialService.getAuthorizeUrl(type, redirectUri)); - return null; + return CommonResult.success(socialService.getAuthorizeUrl(type, redirectUri)); + } + + @GetMapping("/social-login-get") + @ApiOperation("社交登录,使用 code 授权码") + @ResponseBody + @Deprecated + public CommonResult socialLoginGet(HttpServletRequest request,String code,String state) { + // 返回结果 + MbrAuthSocialLoginReqVO reqVO = MbrAuthSocialLoginReqVO.builder().state(state).code(code).build(); + reqVO.setType(12); + System.out.println(JSON.toJSON(reqVO)); + return success(reqVO); } @PostMapping("/social-login") @ApiOperation("社交登录,使用 code 授权码") - public CommonResult socialLogin(@RequestBody @Valid MbrAuthSocialLoginReqVO reqVO) { -// String token = authService.socialLogin(reqVO, getClientIP(), getUserAgent()); -// // 返回结果 -// return success(MbrAuthLoginRespVO.builder().token(token).build()); - return null; + public CommonResult socialLogin(@RequestBody @Valid MbrAuthSocialLoginReqVO reqVO) { + String token = authService.socialLogin(reqVO, getClientIP(), getUserAgent()); + return success(SysAuthLoginRespVO.builder().token(token).build()); } @PostMapping("/social-login2") @ApiOperation("社交登录,使用 code 授权码 + 账号密码") public CommonResult socialLogin2(@RequestBody @Valid MbrAuthSocialLogin2ReqVO reqVO) { -// String token = authService.socialLogin2(reqVO, getClientIP(), getUserAgent()); -// // 返回结果 -// return success(MbrAuthLoginRespVO.builder().token(token).build()); - return null; + String token = authService.socialLogin2(reqVO, getClientIP(), getUserAgent()); + return success(SysAuthLoginRespVO.builder().token(token).build()); } @PostMapping("/social-bind") @ApiOperation("社交绑定,使用 code 授权码") public CommonResult socialBind(@RequestBody @Valid MbrAuthSocialBindReqVO reqVO) { -// authService.socialBind(getLoginUserId(), reqVO); -// return CommonResult.success(true); - return null; + authService.socialBind(getLoginUserId(), reqVO); + return CommonResult.success(true); } @DeleteMapping("/social-unbind") @ApiOperation("取消社交绑定") public CommonResult socialUnbind(@RequestBody MbrAuthSocialUnbindReqVO reqVO) { -// socialService.unbindSocialUser(getLoginUserId(), reqVO.getType(), reqVO.getUnionId()); -// return CommonResult.success(true); - return null; + socialService.unbindSocialUser(getLoginUserId(), reqVO.getType(), reqVO.getUnionId(), UserTypeEnum.MEMBER); + return CommonResult.success(true); } } diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialBindReqVO.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialBindReqVO.java index d866411d2..767446b5f 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialBindReqVO.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialBindReqVO.java @@ -1,7 +1,7 @@ package cn.iocoder.yudao.userserver.modules.system.controller.auth.vo; +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; import cn.iocoder.yudao.framework.common.validation.InEnum; -import cn.iocoder.yudao.userserver.modules.member.enums.social.SysSocialTypeEnum; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialLogin2ReqVO.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialLogin2ReqVO.java index 371e7e9b3..584df7ed1 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialLogin2ReqVO.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialLogin2ReqVO.java @@ -1,7 +1,7 @@ package cn.iocoder.yudao.userserver.modules.system.controller.auth.vo; +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; import cn.iocoder.yudao.framework.common.validation.InEnum; -import cn.iocoder.yudao.userserver.modules.member.enums.social.SysSocialTypeEnum; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialLoginReqVO.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialLoginReqVO.java index 5fdd424c1..cb430231a 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialLoginReqVO.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialLoginReqVO.java @@ -1,7 +1,7 @@ package cn.iocoder.yudao.userserver.modules.system.controller.auth.vo; +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; import cn.iocoder.yudao.framework.common.validation.InEnum; -import cn.iocoder.yudao.userserver.modules.member.enums.social.SysSocialTypeEnum; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialUnbindReqVO.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialUnbindReqVO.java index ea9050a29..75e31701d 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialUnbindReqVO.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialUnbindReqVO.java @@ -1,7 +1,7 @@ package cn.iocoder.yudao.userserver.modules.system.controller.auth.vo; +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; import cn.iocoder.yudao.framework.common.validation.InEnum; -import cn.iocoder.yudao.userserver.modules.member.enums.social.SysSocialTypeEnum; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/enums/SysErrorCodeConstants.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/enums/SysErrorCodeConstants.java index e5f4ac678..f24be9cc6 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/enums/SysErrorCodeConstants.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/enums/SysErrorCodeConstants.java @@ -14,6 +14,7 @@ public interface SysErrorCodeConstants { ErrorCode AUTH_LOGIN_USER_DISABLED = new ErrorCode(1005000001, "登录失败,账号被禁用"); ErrorCode AUTH_LOGIN_FAIL_UNKNOWN = new ErrorCode(1005000002, "登录失败"); // 登录失败的兜底,未知原因 ErrorCode AUTH_TOKEN_EXPIRED = new ErrorCode(1005000003, "Token 已经过期"); + ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1005000004, "未绑定账号,需要进行绑定"); // ========== SMS CODE 模块 1005001000 ========== ErrorCode USER_SMS_CODE_NOT_FOUND = new ErrorCode(1005001000, "验证码不存在"); @@ -22,4 +23,7 @@ public interface SysErrorCodeConstants { ErrorCode USER_SMS_CODE_NOT_CORRECT = new ErrorCode(1005001003, "验证码不正确"); ErrorCode USER_SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY = new ErrorCode(1005001004, "超过每日短信发送数量"); ErrorCode USER_SMS_CODE_SEND_TOO_FAST = new ErrorCode(1005001005, "短信发送过于频率"); + + // ========== 用户模块 1005002000 ========== + ErrorCode USER_NOT_EXISTS = new ErrorCode(1005002001, "用户不存在"); } diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysAuthService.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysAuthService.java index b35d1c27a..628f95c80 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysAuthService.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysAuthService.java @@ -1,8 +1,7 @@ package cn.iocoder.yudao.userserver.modules.system.service.auth; import cn.iocoder.yudao.framework.security.core.service.SecurityAuthFrameworkService; -import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthLoginReqVO; -import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthSmsLoginReqVO; +import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.*; import javax.validation.Valid; @@ -35,4 +34,33 @@ public interface SysAuthService extends SecurityAuthFrameworkService { */ String smsLogin(@Valid SysAuthSmsLoginReqVO reqVO, String userIp, String userAgent); + + /** + * 社交登录,使用 code 授权码 + * + * @param reqVO 登录信息 + * @param userIp 用户 IP + * @param userAgent 用户 UA + * @return 身份令牌,使用 JWT 方式 + */ + String socialLogin(@Valid MbrAuthSocialLoginReqVO reqVO, String userIp, String userAgent); + + /** + * 社交登录,使用 code 授权码 + 账号密码 + * + * @param reqVO 登录信息 + * @param userIp 用户 IP + * @param userAgent 用户 UA + * @return 身份令牌,使用 JWT 方式 + */ + String socialLogin2(@Valid MbrAuthSocialLogin2ReqVO reqVO, String userIp, String userAgent); + + /** + * 社交绑定,使用 code 授权码 + * + * @param userId 用户编号 + * @param reqVO 绑定信息 + */ + void socialBind(Long userId, @Valid MbrAuthSocialBindReqVO reqVO); + } diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java index 0199c078e..52acc46a3 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java @@ -1,25 +1,29 @@ package cn.iocoder.yudao.userserver.modules.system.service.auth.impl; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.Assert; import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO; import cn.iocoder.yudao.coreservice.modules.system.enums.logger.SysLoginLogTypeEnum; import cn.iocoder.yudao.coreservice.modules.system.enums.logger.SysLoginResultEnum; import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService; import cn.iocoder.yudao.coreservice.modules.system.service.logger.SysLoginLogCoreService; import cn.iocoder.yudao.coreservice.modules.system.service.logger.dto.SysLoginLogCreateReqDTO; +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; import cn.iocoder.yudao.framework.security.core.LoginUser; import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService; -import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthLoginReqVO; -import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthSmsLoginReqVO; +import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.*; import cn.iocoder.yudao.userserver.modules.system.convert.auth.SysAuthConvert; import cn.iocoder.yudao.userserver.modules.system.enums.sms.SysSmsSceneEnum; import cn.iocoder.yudao.userserver.modules.system.service.auth.SysAuthService; import cn.iocoder.yudao.userserver.modules.system.service.sms.SysSmsCodeService; import lombok.extern.slf4j.Slf4j; +import me.zhyd.oauth.model.AuthUser; import org.springframework.context.annotation.Lazy; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.BadCredentialsException; @@ -33,6 +37,8 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import javax.validation.Valid; +import java.util.List; import java.util.Objects; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; @@ -59,6 +65,9 @@ public class SysAuthServiceImpl implements SysAuthService { private SysLoginLogCoreService loginLogCoreService; @Resource private SysUserSessionCoreService userSessionCoreService; + @Resource + private SysSocialService socialService; + private static final UserTypeEnum userTypeEnum = UserTypeEnum.MEMBER; @Override public UserDetails loadUserByUsername(String mobile) throws UsernameNotFoundException { @@ -99,6 +108,65 @@ public class SysAuthServiceImpl implements SysAuthService { return userSessionCoreService.createUserSession(loginUser, userIp, userAgent); } + @Override + public String socialLogin(MbrAuthSocialLoginReqVO reqVO, String userIp, String userAgent) { + // 使用 code 授权码,进行登录 + AuthUser authUser = socialService.getAuthUser(reqVO.getType(), reqVO.getCode(), reqVO.getState()); + org.springframework.util.Assert.notNull(authUser, "授权用户不为空"); + + // 如果未绑定 SysSocialUserDO 用户,则无法自动登录,进行报错 + String unionId = socialService.getAuthUserUnionId(authUser); + List socialUsers = socialService.getAllSocialUserList(reqVO.getType(), unionId, userTypeEnum); + if (CollUtil.isEmpty(socialUsers)) { + throw exception(AUTH_THIRD_LOGIN_NOT_BIND); + } + + // 自动登录 + MbrUserDO user = userService.getUser(socialUsers.get(0).getUserId()); + if (user == null) { + throw exception(USER_NOT_EXISTS); + } + this.createLoginLog(user.getMobile(), SysLoginLogTypeEnum.LOGIN_SOCIAL, SysLoginResultEnum.SUCCESS); + + // 创建 LoginUser 对象 + LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user); + // TODO 芋艿:需要改造下,增加各种登录方式 +// loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 + + // 绑定社交用户(更新) + socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum); + + // 缓存登录用户到 Redis 中,返回 sessionId 编号 + return userSessionCoreService.createUserSession(loginUser, userIp, userAgent); + } + + @Override + public String socialLogin2(MbrAuthSocialLogin2ReqVO reqVO, String userIp, String userAgent) { + // 使用 code 授权码,进行登录 + AuthUser authUser = socialService.getAuthUser(reqVO.getType(), reqVO.getCode(), reqVO.getState()); + org.springframework.util.Assert.notNull(authUser, "授权用户不为空"); + + // 使用账号密码,进行登录。 + LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword()); +// loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 + + // 绑定社交用户(新增) + socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum); + + // 缓存登录用户到 Redis 中,返回 sessionId 编号 + return userSessionCoreService.createUserSession(loginUser, userIp, userAgent); + } + + @Override + public void socialBind(Long userId, MbrAuthSocialBindReqVO reqVO) { + // 使用 code 授权码,进行登录 + AuthUser authUser = socialService.getAuthUser(reqVO.getType(), reqVO.getCode(), reqVO.getState()); + org.springframework.util.Assert.notNull(authUser, "授权用户不为空"); + + // 绑定社交用户(新增) + socialService.bindSocialUser(userId, reqVO.getType(), authUser, userTypeEnum); + } + private LoginUser login0(String username, String password) { final SysLoginLogTypeEnum logTypeEnum = SysLoginLogTypeEnum.LOGIN_USERNAME; // 用户验证 @@ -207,7 +275,7 @@ public class SysAuthServiceImpl implements SysAuthService { reqDTO.setLogType(SysLoginLogTypeEnum.LOGOUT_SELF.getType()); reqDTO.setTraceId(TracerUtils.getTraceId()); reqDTO.setUserId(userId); - reqDTO.setUserType(UserTypeEnum.MEMBER.getValue()); + reqDTO.setUserType(userTypeEnum.getValue()); reqDTO.setUsername(username); reqDTO.setUserAgent(ServletUtils.getUserAgent()); reqDTO.setUserIp(ServletUtils.getClientIP()); diff --git a/yudao-user-server/src/main/resources/application-dev.yaml b/yudao-user-server/src/main/resources/application-dev.yaml index b6bfbd1dd..bbf739864 100644 --- a/yudao-user-server/src/main/resources/application-dev.yaml +++ b/yudao-user-server/src/main/resources/application-dev.yaml @@ -138,3 +138,30 @@ yudao: - ${spring.boot.admin.context-path}/** # 不处理 Spring Boot Admin 的请求 - ${management.endpoints.web.base-path}/** # 不处理 Actuator 的请求 demo: true # 开启演示模式 + + +justauth: + enabled: true + type: + GITEE: # Gitee + client-id: ee61f0374a4c6c404a8717094caa7a410d76950e45ff60348015830c519ba5c1 + client-secret: 7c044a5671be3b051414db0cf2cec6ad702dd298d2416ba24ceaf608e6fa26f9 + ignore-check-redirect-uri: true + DINGTALK: # 钉钉 + client-id: dingvrnreaje3yqvzhxg + client-secret: i8E6iZyDvZj51JIb0tYsYfVQYOks9Cq1lgryEjFRqC79P3iJcrxEwT6Qk2QvLrLI + ignore-check-redirect-uri: true + WECHAT_ENTERPRISE: # 企业微信 + client-id: wwd411c69a39ad2e54 + client-secret: 1wTb7hYxnpT2TUbIeHGXGo7T0odav1ic10mLdyyATOw + agent-id: 1000004 + ignore-check-redirect-uri: true + WECHAT_MP: # 微信公众平台 - H5 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index + client-id: wxa5a05b85ac361f96 + client-secret: 247073c7cebb67f27f0e769195c2a57e + ignore-check-redirect-uri: true + cache: + type: REDIS + prefix: 'social_auth_state:' # 缓存前缀,目前只对 Redis 缓存生效,默认 JUSTAUTH::STATE:: + timeout: 24h # 超时时长,目前只对 Redis 缓存生效,默认 3 分钟 + diff --git a/yudao-user-server/src/main/resources/application-local.yaml b/yudao-user-server/src/main/resources/application-local.yaml index 506ee19f6..74f8abbb2 100644 --- a/yudao-user-server/src/main/resources/application-local.yaml +++ b/yudao-user-server/src/main/resources/application-local.yaml @@ -140,3 +140,29 @@ yudao: - ${spring.boot.admin.context-path}/** # 不处理 Spring Boot Admin 的请求 - ${management.endpoints.web.base-path}/** # 不处理 Actuator 的请求 demo: false # 关闭演示模式 + + +justauth: + enabled: true + type: + GITEE: # Gitee + client-id: ee61f0374a4c6c404a8717094caa7a410d76950e45ff60348015830c519ba5c1 + client-secret: 7c044a5671be3b051414db0cf2cec6ad702dd298d2416ba24ceaf608e6fa26f9 + ignore-check-redirect-uri: true + DINGTALK: # 钉钉 + client-id: dingvrnreaje3yqvzhxg + client-secret: i8E6iZyDvZj51JIb0tYsYfVQYOks9Cq1lgryEjFRqC79P3iJcrxEwT6Qk2QvLrLI + ignore-check-redirect-uri: true + WECHAT_ENTERPRISE: # 企业微信 + client-id: wwd411c69a39ad2e54 + client-secret: 1wTb7hYxnpT2TUbIeHGXGo7T0odav1ic10mLdyyATOw + agent-id: 1000004 + ignore-check-redirect-uri: true + WECHAT_MP: # 微信公众平台 - H5 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index + client-id: wxa5a05b85ac361f96 + client-secret: 247073c7cebb67f27f0e769195c2a57e + ignore-check-redirect-uri: true + cache: + type: REDIS + prefix: 'social_auth_state:' # 缓存前缀,目前只对 Redis 缓存生效,默认 JUSTAUTH::STATE:: + timeout: 24h # 超时时长,目前只对 Redis 缓存生效,默认 3 分钟 \ No newline at end of file From 1a9440824c65a7d21904cb81e329d48dd4937c63 Mon Sep 17 00:00:00 2001 From: timfruit Date: Tue, 26 Oct 2021 22:26:33 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/system/controller/auth/SysAuthController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java index 377dd9966..1e85f427d 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java @@ -83,13 +83,14 @@ public class SysAuthController { } @GetMapping("/social-login-get") - @ApiOperation("社交登录,使用 code 授权码") + @ApiOperation("微信公众号授权回调地址,输出social-login2的必要参数用于测试,使用 code 授权码") @ResponseBody @Deprecated public CommonResult socialLoginGet(HttpServletRequest request,String code,String state) { // 返回结果 MbrAuthSocialLoginReqVO reqVO = MbrAuthSocialLoginReqVO.builder().state(state).code(code).build(); reqVO.setType(12); + //输出social-login2的必要参数用于测试 System.out.println(JSON.toJSON(reqVO)); return success(reqVO); } From 55e5ca4644293afc0d2f5edf3cd272cdef6df01c Mon Sep 17 00:00:00 2001 From: YunaiV Date: Thu, 28 Oct 2021 08:46:51 +0800 Subject: [PATCH 3/8] =?UTF-8?q?code=20review=20=E7=94=A8=E6=88=B7=E5=89=8D?= =?UTF-8?q?=E5=8F=B0=E7=9A=84=E7=A4=BE=E4=BA=A4=E7=99=BB=E9=99=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/service/auth/impl/SysAuthServiceImpl.java | 2 +- yudao-core-service/pom.xml | 3 ++- .../system/dal/mysql/social/SysSocialUserMapper.java | 1 + .../system/dal/redis/SysRedisKeyCoreConstants.java | 4 ++-- .../dal/redis/social/SysSocialAuthUserRedisDAO.java | 2 +- .../modules/system/enums/social/SysSocialTypeEnum.java | 1 + .../modules/system/service/social/SysSocialService.java | 2 ++ yudao-dependencies/pom.xml | 9 ++++++++- .../system/controller/auth/SysAuthController.java | 2 +- .../system/service/auth/impl/SysAuthServiceImpl.java | 4 ---- .../src/main/resources/application-dev.yaml | 2 +- 11 files changed, 20 insertions(+), 12 deletions(-) diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java index 0bf08e8c2..558967c2e 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java @@ -75,6 +75,7 @@ public class SysAuthServiceImpl implements SysAuthService { @Resource private SysSocialService socialService; + // TODO @timfruit:静态枚举类,需要都大写,例如说 USER_TYPE_ENUM;静态变量,放在普通变量前面;这个实践不错哈。 private static final UserTypeEnum userTypeEnum = UserTypeEnum.ADMIN; @Override @@ -214,7 +215,6 @@ public class SysAuthServiceImpl implements SysAuthService { // 创建 LoginUser 对象 LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user); - // TODO 芋艿:需要改造下,增加各种登录方式 loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 // 绑定社交用户(更新) diff --git a/yudao-core-service/pom.xml b/yudao-core-service/pom.xml index d1102ae22..56402180f 100644 --- a/yudao-core-service/pom.xml +++ b/yudao-core-service/pom.xml @@ -86,11 +86,12 @@ guava + com.xkcoding.justauth justauth-spring-boot-starter - 1.4.0 + diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/social/SysSocialUserMapper.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/social/SysSocialUserMapper.java index 43fc4a790..ee8db4eac 100644 --- a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/social/SysSocialUserMapper.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/social/SysSocialUserMapper.java @@ -8,6 +8,7 @@ import org.apache.ibatis.annotations.Mapper; import java.util.Collection; import java.util.List; +// TODO @timfruit:SysSocialUserCoreMapper 改名,方便区分 @Mapper public interface SysSocialUserMapper extends BaseMapperX { diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/SysRedisKeyCoreConstants.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/SysRedisKeyCoreConstants.java index 8076980d3..5db3e9fd4 100644 --- a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/SysRedisKeyCoreConstants.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/SysRedisKeyCoreConstants.java @@ -19,11 +19,11 @@ public interface SysRedisKeyCoreConstants { "login_user:%s", // 参数为 sessionId STRING, LoginUser.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC); - RedisKeyDefine SOCIAL_AUTH_USER = new RedisKeyDefine("社交的授权用户", + RedisKeyDefine SOCIAL_AUTH_USER = new RedisKeyDefine("社交登陆的授权用户", "social_auth_user:%d:%s", // 参数为 type,code STRING, AuthUser.class, Duration.ofDays(1)); - RedisKeyDefine SOCIAL_AUTH_STATE = new RedisKeyDefine("社交的 state", + RedisKeyDefine SOCIAL_AUTH_STATE = new RedisKeyDefine("社交登陆的 state", "social_auth_state:%s", // 参数为 state STRING, String.class, Duration.ofHours(24)); // 值为 state } diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/social/SysSocialAuthUserRedisDAO.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/social/SysSocialAuthUserRedisDAO.java index e82c81394..514281071 100644 --- a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/social/SysSocialAuthUserRedisDAO.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/social/SysSocialAuthUserRedisDAO.java @@ -10,7 +10,7 @@ import javax.annotation.Resource; import static cn.iocoder.yudao.coreservice.modules.system.dal.redis.SysRedisKeyCoreConstants.SOCIAL_AUTH_USER; - +// TODO @timfruit,这里的 AuthUser 还是保留全路径,主要想体现出来,不是自己定义的 /** * 社交 {@link AuthUser} 的 RedisDAO * diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java index 592c274a1..8532283c1 100644 --- a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java @@ -21,6 +21,7 @@ public enum SysSocialTypeEnum implements IntArrayValuable { GITEE(10, "GITEE"), // https://gitee.com/api/v5/oauth_doc#/ DINGTALK(20, "DINGTALK"), // https://developers.dingtalk.com/document/app/obtain-identity-credentials WECHAT_ENTERPRISE(30, "WECHAT_ENTERPRISE"), // https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html + // TODO @timfruit:微信平台,从 30 开始递增哈。另外,尽量不要出现,先 12,然后 11,有序,嘿嘿。 /** * 微信公众平台 - H5 */ diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/SysSocialService.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/SysSocialService.java index 81ee31ecf..ad72a5780 100644 --- a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/SysSocialService.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/SysSocialService.java @@ -15,6 +15,7 @@ import java.util.List; * * @author 芋道源码 */ +// TODO @timfruit:SysSocialCoreService 改名,方便区分 public interface SysSocialService { /** @@ -77,5 +78,6 @@ public interface SysSocialService { * @param unionId 社交平台的 unionId */ void unbindSocialUser(Long userId, Integer type, String unionId,UserTypeEnum userTypeEnum); + // TODO @timfruit:逗号后面要有空格;缺少了 @userTypeEnum 的注释,都补充下哈。 } diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml index 723ea0c6a..18894b0d9 100644 --- a/yudao-dependencies/pom.xml +++ b/yudao-dependencies/pom.xml @@ -52,6 +52,7 @@ 4.5.25 2.1.0 1.2.7 + 1.4.0 @@ -399,7 +400,6 @@ yunpian-java-sdk ${yunpian-java-sdk.version} - com.aliyun aliyun-java-sdk-core @@ -421,6 +421,13 @@ ${aliyun-java-sdk-dysmsapi.version} + + + com.xkcoding.justauth + justauth-spring-boot-starter + ${justauth.version} + + diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java index 1e85f427d..a21a56ede 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java @@ -3,7 +3,6 @@ package cn.iocoder.yudao.userserver.modules.system.controller.auth; import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.pojo.CommonResult; -import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.*; import cn.iocoder.yudao.userserver.modules.system.service.auth.SysAuthService; import cn.iocoder.yudao.userserver.modules.system.service.sms.SysSmsCodeService; @@ -82,6 +81,7 @@ public class SysAuthController { return CommonResult.success(socialService.getAuthorizeUrl(type, redirectUri)); } + // TODO @timfruit:这个接口,是要删除的么? @GetMapping("/social-login-get") @ApiOperation("微信公众号授权回调地址,输出social-login2的必要参数用于测试,使用 code 授权码") @ResponseBody diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java index 52acc46a3..2394cd03a 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java @@ -4,7 +4,6 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.Assert; import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO; import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO; -import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO; import cn.iocoder.yudao.coreservice.modules.system.enums.logger.SysLoginLogTypeEnum; import cn.iocoder.yudao.coreservice.modules.system.enums.logger.SysLoginResultEnum; import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService; @@ -37,7 +36,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; -import javax.validation.Valid; import java.util.List; import java.util.Objects; @@ -130,8 +128,6 @@ public class SysAuthServiceImpl implements SysAuthService { // 创建 LoginUser 对象 LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user); - // TODO 芋艿:需要改造下,增加各种登录方式 -// loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 // 绑定社交用户(更新) socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum); diff --git a/yudao-user-server/src/main/resources/application-dev.yaml b/yudao-user-server/src/main/resources/application-dev.yaml index bbf739864..c015867af 100644 --- a/yudao-user-server/src/main/resources/application-dev.yaml +++ b/yudao-user-server/src/main/resources/application-dev.yaml @@ -142,7 +142,7 @@ yudao: justauth: enabled: true - type: + type: # TODO @timfruit:GITEE、DINGTALK、WECHAT_ENTERPRISE 这个几个,对于用户端是不需要的哈,可以删除噢 GITEE: # Gitee client-id: ee61f0374a4c6c404a8717094caa7a410d76950e45ff60348015830c519ba5c1 client-secret: 7c044a5671be3b051414db0cf2cec6ad702dd298d2416ba24ceaf608e6fa26f9 From 68c98db147748881b9de59e8289fe13b9e40b6a9 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Thu, 28 Oct 2021 08:50:50 +0800 Subject: [PATCH 4/8] =?UTF-8?q?code=20review=20=E7=94=A8=E6=88=B7=E5=89=8D?= =?UTF-8?q?=E5=8F=B0=E7=9A=84=E7=A4=BE=E4=BA=A4=E7=99=BB=E9=99=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/system/controller/auth/SysAuthController.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java index a21a56ede..6d18d53be 100644 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java +++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java @@ -102,6 +102,8 @@ public class SysAuthController { return success(SysAuthLoginRespVO.builder().token(token).build()); } + // TODO @timfruit:社交登陆时,使用手机验证码来验证哈。这块我当时没设计好,改改,嘿嘿。 + @PostMapping("/social-login2") @ApiOperation("社交登录,使用 code 授权码 + 账号密码") public CommonResult socialLogin2(@RequestBody @Valid MbrAuthSocialLogin2ReqVO reqVO) { From de210341869dc0593b27f106ada60702c43d44ea Mon Sep 17 00:00:00 2001 From: timfruit Date: Fri, 29 Oct 2021 23:48:06 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E6=8E=88=E6=9D=83=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../compent/justauth/AuthExtendSource.java | 33 +++++++ .../compent/justauth/AuthExtendToken.java | 21 +++++ .../AuthWeChatMiniProgramRequest.java | 85 +++++++++++++++++++ .../enums/social/SysSocialTypeEnum.java | 19 +++-- .../src/main/resources/application-local.yaml | 11 ++- 5 files changed, 162 insertions(+), 7 deletions(-) create mode 100644 yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthExtendSource.java create mode 100644 yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthExtendToken.java create mode 100644 yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthWeChatMiniProgramRequest.java diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthExtendSource.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthExtendSource.java new file mode 100644 index 000000000..e005114ac --- /dev/null +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthExtendSource.java @@ -0,0 +1,33 @@ +package cn.iocoder.yudao.coreservice.modules.system.compent.justauth; + +import me.zhyd.oauth.config.AuthSource; + +public enum AuthExtendSource implements AuthSource { + + /** + * 微信小程序授权登录 + */ + WECHAT_MINI_PROGRAM{ + @Override + public String authorize() { + // https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html + throw new UnsupportedOperationException("不支持获取授权url, 请使用小程序内置函数wx.login()登录获取code"); + } + + @Override + public String accessToken() { + // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html + // 获取openid, unionid , session_key + return "https://api.weixin.qq.com/sns/jscode2session"; + } + + @Override + public String userInfo() { + //https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html + throw new UnsupportedOperationException("不支持获取用户信息url, 请使用小程序内置函数wx.getUserProfile()获取用户信息"); + } + } + + ; + +} diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthExtendToken.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthExtendToken.java new file mode 100644 index 000000000..b336511cb --- /dev/null +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthExtendToken.java @@ -0,0 +1,21 @@ +package cn.iocoder.yudao.coreservice.modules.system.compent.justauth; + +import lombok.*; +import me.zhyd.oauth.model.AuthToken; + +/** + * @author timfruit + * @date 2021-10-29 + */ +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class AuthExtendToken extends AuthToken { + /** + * 微信小程序 会话密钥 + */ + private String miniSessionKey; + + +} diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthWeChatMiniProgramRequest.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthWeChatMiniProgramRequest.java new file mode 100644 index 000000000..84c920b3d --- /dev/null +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthWeChatMiniProgramRequest.java @@ -0,0 +1,85 @@ +package cn.iocoder.yudao.coreservice.modules.system.compent.justauth; + +import com.alibaba.fastjson.JSONObject; +import me.zhyd.oauth.cache.AuthStateCache; +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.exception.AuthException; +import me.zhyd.oauth.model.AuthCallback; +import me.zhyd.oauth.model.AuthToken; +import me.zhyd.oauth.model.AuthUser; +import me.zhyd.oauth.request.AuthDefaultRequest; +import me.zhyd.oauth.utils.HttpUtils; +import me.zhyd.oauth.utils.UrlBuilder; + +/** + * 微信小程序 + * + * @author timfruit + * @date 2021-10-29 + */ +public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest { + public AuthWeChatMiniProgramRequest(AuthConfig config) { + super(config, AuthExtendSource.WECHAT_MINI_PROGRAM); + } + + public AuthWeChatMiniProgramRequest(AuthConfig config, AuthStateCache authStateCache) { + super(config, AuthExtendSource.WECHAT_MINI_PROGRAM, authStateCache); + } + + @Override + protected AuthToken getAccessToken(AuthCallback authCallback) { + // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html + String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl(authCallback.getCode())); + JSONObject accessTokenObject = JSONObject.parseObject(response); + + this.checkResponse(accessTokenObject); + + AuthExtendToken token=new AuthExtendToken(); + token.setMiniSessionKey(accessTokenObject.getString("session_key")); + token.setOpenId(accessTokenObject.getString("openid")); + token.setUnionId(accessTokenObject.getString("unionid")); + return token; + } + + @Override + protected AuthUser getUserInfo(AuthToken authToken) { + // https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html + // 如果需要用户信息,需要在小程序调用函数后传给后端 + return AuthUser.builder() + .uuid(authToken.getOpenId()) + //TODO 是使用默认值,还是有小程序获取用户信息 和 code 一起传过来 + .nickname("") + .avatar("") + .token(authToken) + .source(source.toString()) + .build(); + } + + /** + * 检查响应内容是否正确 + * + * @param object 请求响应内容 + */ + private void checkResponse(JSONObject object) { + int code = object.getIntValue("errcode"); + if(code != 0){ + throw new AuthException(object.getIntValue("errcode"), object.getString("errmsg")); + } + } + + /** + * 返回获取accessToken的url + * + * @param code 授权码 + * @return 返回获取accessToken的url + */ + @Override + protected String accessTokenUrl(String code) { + return UrlBuilder.fromBaseUrl(source.accessToken()) + .queryParam("appid", config.getClientId()) + .queryParam("secret", config.getClientSecret()) + .queryParam("js_code", code) + .queryParam("grant_type", "authorization_code") + .build(); + } +} diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java index 8532283c1..d36d8a727 100644 --- a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java @@ -20,21 +20,28 @@ public enum SysSocialTypeEnum implements IntArrayValuable { GITEE(10, "GITEE"), // https://gitee.com/api/v5/oauth_doc#/ DINGTALK(20, "DINGTALK"), // https://developers.dingtalk.com/document/app/obtain-identity-credentials + /** + * 企业微信 + */ WECHAT_ENTERPRISE(30, "WECHAT_ENTERPRISE"), // https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html - // TODO @timfruit:微信平台,从 30 开始递增哈。另外,尽量不要出现,先 12,然后 11,有序,嘿嘿。 /** - * 微信公众平台 - H5 + * 微信公众平台 - 移动端H5 */ - WECHAT_MP(12, "WECHAT_MP"), // https://www.cnblogs.com/juewuzhe/p/11905461.html + WECHAT_MP(31, "WECHAT_MP"), // https://www.cnblogs.com/juewuzhe/p/11905461.html /** - * 微信开放平台 - 小程序 + * 微信开放平台 - 网站应用 pc端扫码授权登录 */ - WECHAT_OPEN(11, "WECHAT_OPEN"), // https://justauth.wiki/guide/oauth/wechat_open/#_2-%E7%94%B3%E8%AF%B7%E5%BC%80%E5%8F%91%E8%80%85%E8%B5%84%E8%B4%A8%E8%AE%A4%E8%AF%81 + WECHAT_OPEN(32, "WECHAT_OPEN"), // https://justauth.wiki/guide/oauth/wechat_open/#_2-%E7%94%B3%E8%AF%B7%E5%BC%80%E5%8F%91%E8%80%85%E8%B5%84%E8%B4%A8%E8%AE%A4%E8%AF%81 + /** + * 微信小程序 + */ + WECHAT_MINI_PROGRAM(33, "WECHAT_MINI_PROGRAM"), // https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html ; public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(SysSocialTypeEnum::getType).toArray(); - public static final List WECHAT_ALL = ListUtil.toList(WECHAT_ENTERPRISE.type, WECHAT_MP.type, WECHAT_OPEN.type); + public static final List WECHAT_ALL = ListUtil.toList(WECHAT_ENTERPRISE.type, WECHAT_MP.type, WECHAT_OPEN.type, + WECHAT_MINI_PROGRAM.type); /** * 类型 diff --git a/yudao-user-server/src/main/resources/application-local.yaml b/yudao-user-server/src/main/resources/application-local.yaml index 74f8abbb2..e5756b9c9 100644 --- a/yudao-user-server/src/main/resources/application-local.yaml +++ b/yudao-user-server/src/main/resources/application-local.yaml @@ -158,10 +158,19 @@ justauth: client-secret: 1wTb7hYxnpT2TUbIeHGXGo7T0odav1ic10mLdyyATOw agent-id: 1000004 ignore-check-redirect-uri: true - WECHAT_MP: # 微信公众平台 - H5 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index + WECHAT_MP: # 微信公众平台 - 移动端H5 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index client-id: wxa5a05b85ac361f96 client-secret: 247073c7cebb67f27f0e769195c2a57e ignore-check-redirect-uri: true + extend: + enum-class: cn.iocoder.yudao.coreservice.modules.system.compent.justauth.AuthExtendSource + config: + WECHAT_MINI_PROGRAM: # 微信小程序 扩展 + request-class: cn.iocoder.yudao.coreservice.modules.system.compent.justauth.AuthWeChatMiniProgramRequest + client-id: wx44d047d87e6284d8 + client-secret: 21c3b7a8a51ee1b8f5cf875848ed4466 + ignore-check-redirect-uri: true + ignore-check-state: true cache: type: REDIS prefix: 'social_auth_state:' # 缓存前缀,目前只对 Redis 缓存生效,默认 JUSTAUTH::STATE:: From cba9c609d74c91663db46d8ece860f8e2d1fbe7b Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 30 Oct 2021 09:12:17 +0800 Subject: [PATCH 6/8] =?UTF-8?q?code=20review=20=E7=A4=BE=E4=BA=A4=E7=99=BB?= =?UTF-8?q?=E9=99=86=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../compent/justauth/AuthExtendToken.java | 3 +- .../AuthWeChatMiniProgramRequest.java | 17 +++++++---- .../enums/social/SysSocialTypeEnum.java | 29 ++++++++++++++----- .../src/main/resources/application-local.yaml | 15 +--------- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthExtendToken.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthExtendToken.java index b336511cb..2ecb0d461 100644 --- a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthExtendToken.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthExtendToken.java @@ -4,6 +4,7 @@ import lombok.*; import me.zhyd.oauth.model.AuthToken; /** + * TODO @timfruit:类注释 * @author timfruit * @date 2021-10-29 */ @@ -12,10 +13,10 @@ import me.zhyd.oauth.model.AuthToken; @NoArgsConstructor @AllArgsConstructor public class AuthExtendToken extends AuthToken { + /** * 微信小程序 会话密钥 */ private String miniSessionKey; - } diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthWeChatMiniProgramRequest.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthWeChatMiniProgramRequest.java index 84c920b3d..664f9157f 100644 --- a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthWeChatMiniProgramRequest.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthWeChatMiniProgramRequest.java @@ -11,13 +11,17 @@ import me.zhyd.oauth.request.AuthDefaultRequest; import me.zhyd.oauth.utils.HttpUtils; import me.zhyd.oauth.utils.UrlBuilder; +// TODO @timfruit:新建一个 yudao-spring-boot-starter-biz-social 包,把这个拓展拿进去哈。另外,可以思考下。 +// 1. application-local.yaml 的配置里,justauth.extend.enum-class 能否不配置,而是自动配置好 +// 2. application-local.yaml 的配置里,justauth.extend.extend.config.WECHAT_MINI_PROGRAM 有办法和 justauth.type.WECHAT_MP 持平 /** - * 微信小程序 + * 微信小程序登陆 * * @author timfruit * @date 2021-10-29 */ public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest { + public AuthWeChatMiniProgramRequest(AuthConfig config) { super(config, AuthExtendSource.WECHAT_MINI_PROGRAM); } @@ -30,11 +34,11 @@ public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest { protected AuthToken getAccessToken(AuthCallback authCallback) { // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl(authCallback.getCode())); - JSONObject accessTokenObject = JSONObject.parseObject(response); + JSONObject accessTokenObject = JSONObject.parseObject(response); // TODO @timfruit:使用 JsonUtils,项目尽量避免直接使用某个 json 库 this.checkResponse(accessTokenObject); - AuthExtendToken token=new AuthExtendToken(); + AuthExtendToken token = new AuthExtendToken(); token.setMiniSessionKey(accessTokenObject.getString("session_key")); token.setOpenId(accessTokenObject.getString("openid")); token.setUnionId(accessTokenObject.getString("unionid")); @@ -62,16 +66,16 @@ public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest { */ private void checkResponse(JSONObject object) { int code = object.getIntValue("errcode"); - if(code != 0){ + if(code != 0){ // TODO @timfruit:if (code != 0) { ,注意空格 throw new AuthException(object.getIntValue("errcode"), object.getString("errmsg")); } } /** - * 返回获取accessToken的url + * 返回获取 accessToken 的 url * * @param code 授权码 - * @return 返回获取accessToken的url + * @return 返回获取 accessToken 的 url */ @Override protected String accessTokenUrl(String code) { @@ -82,4 +86,5 @@ public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest { .queryParam("grant_type", "authorization_code") .build(); } + } diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java index d36d8a727..3dba5bda0 100644 --- a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java @@ -18,24 +18,37 @@ import java.util.List; @AllArgsConstructor public enum SysSocialTypeEnum implements IntArrayValuable { - GITEE(10, "GITEE"), // https://gitee.com/api/v5/oauth_doc#/ - DINGTALK(20, "DINGTALK"), // https://developers.dingtalk.com/document/app/obtain-identity-credentials + /** + * Gitee + * 文档链接:https://gitee.com/api/v5/oauth_doc#/ + */ + GITEE(10, "GITEE"), + /** + * 钉钉 + * 文档链接:https://developers.dingtalk.com/document/app/obtain-identity-credentials + */ + DINGTALK(20, "DINGTALK"), + /** * 企业微信 + * 文档链接:https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html */ - WECHAT_ENTERPRISE(30, "WECHAT_ENTERPRISE"), // https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html + WECHAT_ENTERPRISE(30, "WECHAT_ENTERPRISE"), /** - * 微信公众平台 - 移动端H5 + * 微信公众平台 - 移动端 H5 + * 文档链接:https://www.cnblogs.com/juewuzhe/p/11905461.html */ - WECHAT_MP(31, "WECHAT_MP"), // https://www.cnblogs.com/juewuzhe/p/11905461.html + WECHAT_MP(31, "WECHAT_MP"), /** - * 微信开放平台 - 网站应用 pc端扫码授权登录 + * 微信开放平台 - 网站应用 PC 端扫码授权登录 + * 文档链接:https://justauth.wiki/guide/oauth/wechat_open/#_2-申请开发者资质认证 */ - WECHAT_OPEN(32, "WECHAT_OPEN"), // https://justauth.wiki/guide/oauth/wechat_open/#_2-%E7%94%B3%E8%AF%B7%E5%BC%80%E5%8F%91%E8%80%85%E8%B5%84%E8%B4%A8%E8%AE%A4%E8%AF%81 + WECHAT_OPEN(32, "WECHAT_OPEN"), /** * 微信小程序 + * 文档链接:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html */ - WECHAT_MINI_PROGRAM(33, "WECHAT_MINI_PROGRAM"), // https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html + WECHAT_MINI_PROGRAM(33, "WECHAT_MINI_PROGRAM"), ; public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(SysSocialTypeEnum::getType).toArray(); diff --git a/yudao-user-server/src/main/resources/application-local.yaml b/yudao-user-server/src/main/resources/application-local.yaml index e5756b9c9..c57d17808 100644 --- a/yudao-user-server/src/main/resources/application-local.yaml +++ b/yudao-user-server/src/main/resources/application-local.yaml @@ -145,19 +145,6 @@ yudao: justauth: enabled: true type: - GITEE: # Gitee - client-id: ee61f0374a4c6c404a8717094caa7a410d76950e45ff60348015830c519ba5c1 - client-secret: 7c044a5671be3b051414db0cf2cec6ad702dd298d2416ba24ceaf608e6fa26f9 - ignore-check-redirect-uri: true - DINGTALK: # 钉钉 - client-id: dingvrnreaje3yqvzhxg - client-secret: i8E6iZyDvZj51JIb0tYsYfVQYOks9Cq1lgryEjFRqC79P3iJcrxEwT6Qk2QvLrLI - ignore-check-redirect-uri: true - WECHAT_ENTERPRISE: # 企业微信 - client-id: wwd411c69a39ad2e54 - client-secret: 1wTb7hYxnpT2TUbIeHGXGo7T0odav1ic10mLdyyATOw - agent-id: 1000004 - ignore-check-redirect-uri: true WECHAT_MP: # 微信公众平台 - 移动端H5 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index client-id: wxa5a05b85ac361f96 client-secret: 247073c7cebb67f27f0e769195c2a57e @@ -174,4 +161,4 @@ justauth: cache: type: REDIS prefix: 'social_auth_state:' # 缓存前缀,目前只对 Redis 缓存生效,默认 JUSTAUTH::STATE:: - timeout: 24h # 超时时长,目前只对 Redis 缓存生效,默认 3 分钟 \ No newline at end of file + timeout: 24h # 超时时长,目前只对 Redis 缓存生效,默认 3 分钟 From 038c0b87b9d4c45e90b4fa4713ed27427431b820 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 30 Oct 2021 09:43:26 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E7=99=BB=E9=99=86=E7=9A=84=E6=B5=8B=E8=AF=95=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application-local.yaml | 4 ++- .../main/resources/static/social-login.html | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 yudao-user-server/src/main/resources/static/social-login.html diff --git a/yudao-user-server/src/main/resources/application-local.yaml b/yudao-user-server/src/main/resources/application-local.yaml index 24e633132..1e904c3ee 100644 --- a/yudao-user-server/src/main/resources/application-local.yaml +++ b/yudao-user-server/src/main/resources/application-local.yaml @@ -160,9 +160,11 @@ yudao: justauth: enabled: true type: - WECHAT_MP: # 微信公众平台 - 移动端H5 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index + WECHAT_MP: # 微信公众平台 - 移动端 H5 https://www.yuque.com/docs/share/a795bef6-ee8a-494a-8dc4-2ef41927743b?#%20%E3%80%8A%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97%E5%8F%B7%E6%B5%8B%E8%AF%95%E3%80%8B client-id: wxa5a05b85ac361f96 client-secret: 247073c7cebb67f27f0e769195c2a57e +# client-id: wx5b23ba7a5589ecbb # TODO 芋艿:自己的测试,后续可以删除 +# client-secret: 2a7b3b20c537e52e74afd395eb85f61f ignore-check-redirect-uri: true extend: enum-class: cn.iocoder.yudao.coreservice.modules.system.compent.justauth.AuthExtendSource diff --git a/yudao-user-server/src/main/resources/static/social-login.html b/yudao-user-server/src/main/resources/static/social-login.html new file mode 100644 index 000000000..2570d7084 --- /dev/null +++ b/yudao-user-server/src/main/resources/static/social-login.html @@ -0,0 +1,36 @@ + + + + + + 社交登陆测试页 + + + +
点击如下按钮,发起登陆的测试
+
+ +
+ + + From 8dbd6143bf60613c802fb79663f2c6f940244349 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 30 Oct 2021 10:09:31 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E7=9A=84=E7=99=BB=E9=99=86=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mini-program-test/.eslintrc.js | 31 +++++++ mini-program-test/README.md | 1 + mini-program-test/app.js | 19 ++++ mini-program-test/app.json | 14 +++ mini-program-test/app.wxss | 10 ++ mini-program-test/pages/index/index.js | 91 +++++++++++++++++++ mini-program-test/pages/index/index.json | 3 + mini-program-test/pages/index/index.wxml | 29 ++++++ mini-program-test/pages/index/index.wxss | 19 ++++ mini-program-test/pages/logs/logs.js | 18 ++++ mini-program-test/pages/logs/logs.json | 4 + mini-program-test/pages/logs/logs.wxml | 6 ++ mini-program-test/pages/logs/logs.wxss | 8 ++ mini-program-test/project.config.json | 75 +++++++++++++++ mini-program-test/sitemap.json | 7 ++ mini-program-test/utils/common.js | 3 + mini-program-test/utils/util.js | 19 ++++ .../vo/auth/SysAuthSocialUnbindReqVO.java | 2 +- .../user/SysUserProfileController.java | 7 +- .../system/convert/user/SysUserConvert.java | 2 +- .../service/auth/SysAuthServiceImplTest.java | 2 +- .../service/social/SysSocialServiceTest.java | 21 +++-- .../service/social/SysSocialService.java | 2 +- .../social/impl/SysSocialServiceImpl.java | 2 +- .../src/main/resources/application-local.yaml | 5 +- 更新日志.md | 1 + 26 files changed, 381 insertions(+), 20 deletions(-) create mode 100755 mini-program-test/.eslintrc.js create mode 100644 mini-program-test/README.md create mode 100755 mini-program-test/app.js create mode 100755 mini-program-test/app.json create mode 100755 mini-program-test/app.wxss create mode 100755 mini-program-test/pages/index/index.js create mode 100755 mini-program-test/pages/index/index.json create mode 100755 mini-program-test/pages/index/index.wxml create mode 100755 mini-program-test/pages/index/index.wxss create mode 100755 mini-program-test/pages/logs/logs.js create mode 100755 mini-program-test/pages/logs/logs.json create mode 100755 mini-program-test/pages/logs/logs.wxml create mode 100755 mini-program-test/pages/logs/logs.wxss create mode 100755 mini-program-test/project.config.json create mode 100755 mini-program-test/sitemap.json create mode 100755 mini-program-test/utils/common.js create mode 100755 mini-program-test/utils/util.js diff --git a/mini-program-test/.eslintrc.js b/mini-program-test/.eslintrc.js new file mode 100755 index 000000000..115cc02b0 --- /dev/null +++ b/mini-program-test/.eslintrc.js @@ -0,0 +1,31 @@ +/* + * Eslint config file + * Documentation: https://eslint.org/docs/user-guide/configuring/ + * Install the Eslint extension before using this feature. + */ +module.exports = { + env: { + es6: true, + browser: true, + node: true, + }, + ecmaFeatures: { + modules: true, + }, + parserOptions: { + ecmaVersion: 2018, + sourceType: 'module', + }, + globals: { + wx: true, + App: true, + Page: true, + getCurrentPages: true, + getApp: true, + Component: true, + requirePlugin: true, + requireMiniProgram: true, + }, + // extends: 'eslint:recommended', + rules: {}, +} diff --git a/mini-program-test/README.md b/mini-program-test/README.md new file mode 100644 index 000000000..4e5737ccb --- /dev/null +++ b/mini-program-test/README.md @@ -0,0 +1 @@ +临时项目,作为测试微信小程序登陆之用 diff --git a/mini-program-test/app.js b/mini-program-test/app.js new file mode 100755 index 000000000..1ed57c47f --- /dev/null +++ b/mini-program-test/app.js @@ -0,0 +1,19 @@ +// app.js +App({ + onLaunch() { + // 展示本地存储能力 + const logs = wx.getStorageSync('logs') || [] + logs.unshift(Date.now()) + wx.setStorageSync('logs', logs) + + // 登录 + wx.login({ + success: res => { + // 发送 res.code 到后台换取 openId, sessionKey, unionId + } + }) + }, + globalData: { + userInfo: null + } +}) diff --git a/mini-program-test/app.json b/mini-program-test/app.json new file mode 100755 index 000000000..3d7616ff4 --- /dev/null +++ b/mini-program-test/app.json @@ -0,0 +1,14 @@ +{ + "pages":[ + "pages/index/index", + "pages/logs/logs" + ], + "window":{ + "backgroundTextStyle":"light", + "navigationBarBackgroundColor": "#fff", + "navigationBarTitleText": "Weixin", + "navigationBarTextStyle":"black" + }, + "style": "v2", + "sitemapLocation": "sitemap.json" +} diff --git a/mini-program-test/app.wxss b/mini-program-test/app.wxss new file mode 100755 index 000000000..06c6fc9ce --- /dev/null +++ b/mini-program-test/app.wxss @@ -0,0 +1,10 @@ +/**app.wxss**/ +.container { + height: 100%; + display: flex; + flex-direction: column; + align-items: center; + justify-content: space-between; + padding: 200rpx 0; + box-sizing: border-box; +} diff --git a/mini-program-test/pages/index/index.js b/mini-program-test/pages/index/index.js new file mode 100755 index 000000000..eadb684cf --- /dev/null +++ b/mini-program-test/pages/index/index.js @@ -0,0 +1,91 @@ +// index.js + +const common=require('../../utils/common.js') +// 获取应用实例 +const app = getApp() + +Page({ + data: { + motto: 'Hello World', + userInfo: {}, + hasUserInfo: false, + canIUse: wx.canIUse('button.open-type.getUserInfo'), + canIUseGetUserProfile: false, + canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false + holderText: 'to be auth' + }, + // 事件处理函数 + bindViewTap() { + wx.navigateTo({ + url: '../logs/logs' + }) + }, + onLoad() { + if (wx.getUserProfile) { + this.setData({ + canIUseGetUserProfile: true + }) + } + }, + getUserProfile(e) { + // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗 + wx.getUserProfile({ + desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 + success: (res) => { + console.log(res) + this.setData({ + userInfo: res.userInfo, + hasUserInfo: true + }) + } + }) + }, + getUserInfo(e) { + // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息 + console.log(e) + this.setData({ + userInfo: e.detail.userInfo, + hasUserInfo: true + }) + }, + // 小程序登录 https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html + wxLogin(e){ + let page=this; + wx.login({ + success (res) { + console.log("res:") + console.log(res) + if (res.code) { + //发起网络请求 + console.log('发起网络请求'+common.baseurl) + wx.request({ + url: common.baseurl+'/api/social-login2', + method: "POST", + data: { + code: res.code, + state: 'empty', + type: 33, + username: '15601691300', + password: 'admin123' + }, + header: { + 'content-type': 'application/json' // 默认值 + }, + success: function(res) { + console.log(res.data) + let holder="auth success, token:"+res.data.data.token + page.setData({holderText: holder}) + }, + fail: function(data){ + console.error("请求出错"); + console.error(data) + } + + }) + } else { + console.log('登录失败!' + res.errMsg) + } + } + }) + } +}) diff --git a/mini-program-test/pages/index/index.json b/mini-program-test/pages/index/index.json new file mode 100755 index 000000000..8835af069 --- /dev/null +++ b/mini-program-test/pages/index/index.json @@ -0,0 +1,3 @@ +{ + "usingComponents": {} +} \ No newline at end of file diff --git a/mini-program-test/pages/index/index.wxml b/mini-program-test/pages/index/index.wxml new file mode 100755 index 000000000..e3243c3e0 --- /dev/null +++ b/mini-program-test/pages/index/index.wxml @@ -0,0 +1,29 @@ + + + + + + + + + + + 请使用1.4.4及以上版本基础库 + + + + {{userInfo.nickName}} + + + + + + + + + 授权登录测试1024 + + + + {{holderText}} + diff --git a/mini-program-test/pages/index/index.wxss b/mini-program-test/pages/index/index.wxss new file mode 100755 index 000000000..eb642035f --- /dev/null +++ b/mini-program-test/pages/index/index.wxss @@ -0,0 +1,19 @@ +/**index.wxss**/ +.userinfo { + display: flex; + flex-direction: column; + align-items: center; + color: #aaa; +} + +.userinfo-avatar { + overflow: hidden; + width: 128rpx; + height: 128rpx; + margin: 20rpx; + border-radius: 50%; +} + +.usermotto { + margin-top: 200px; +} \ No newline at end of file diff --git a/mini-program-test/pages/logs/logs.js b/mini-program-test/pages/logs/logs.js new file mode 100755 index 000000000..85f6aac5a --- /dev/null +++ b/mini-program-test/pages/logs/logs.js @@ -0,0 +1,18 @@ +// logs.js +const util = require('../../utils/util.js') + +Page({ + data: { + logs: [] + }, + onLoad() { + this.setData({ + logs: (wx.getStorageSync('logs') || []).map(log => { + return { + date: util.formatTime(new Date(log)), + timeStamp: log + } + }) + }) + } +}) diff --git a/mini-program-test/pages/logs/logs.json b/mini-program-test/pages/logs/logs.json new file mode 100755 index 000000000..3ee76c183 --- /dev/null +++ b/mini-program-test/pages/logs/logs.json @@ -0,0 +1,4 @@ +{ + "navigationBarTitleText": "查看启动日志", + "usingComponents": {} +} \ No newline at end of file diff --git a/mini-program-test/pages/logs/logs.wxml b/mini-program-test/pages/logs/logs.wxml new file mode 100755 index 000000000..0b6b6456f --- /dev/null +++ b/mini-program-test/pages/logs/logs.wxml @@ -0,0 +1,6 @@ + + + + {{index + 1}}. {{log.date}} + + diff --git a/mini-program-test/pages/logs/logs.wxss b/mini-program-test/pages/logs/logs.wxss new file mode 100755 index 000000000..94d4b88a2 --- /dev/null +++ b/mini-program-test/pages/logs/logs.wxss @@ -0,0 +1,8 @@ +.log-list { + display: flex; + flex-direction: column; + padding: 40rpx; +} +.log-item { + margin: 10rpx; +} diff --git a/mini-program-test/project.config.json b/mini-program-test/project.config.json new file mode 100755 index 000000000..2fbe955c6 --- /dev/null +++ b/mini-program-test/project.config.json @@ -0,0 +1,75 @@ +{ + "description": "项目配置文件", + "packOptions": { + "ignore": [ + { + "type": "file", + "value": ".eslintrc.js" + } + ] + }, + "setting": { + "bundle": false, + "userConfirmedBundleSwitch": false, + "urlCheck": true, + "scopeDataCheck": false, + "coverView": true, + "es6": true, + "postcss": true, + "compileHotReLoad": false, + "lazyloadPlaceholderEnable": false, + "preloadBackgroundData": false, + "minified": true, + "autoAudits": false, + "newFeature": false, + "uglifyFileName": false, + "uploadWithSourceMap": true, + "useIsolateContext": true, + "nodeModules": false, + "enhance": true, + "useMultiFrameRuntime": true, + "useApiHook": true, + "useApiHostProcess": true, + "showShadowRootInWxmlPanel": true, + "packNpmManually": false, + "enableEngineNative": false, + "packNpmRelationList": [], + "minifyWXSS": true, + "showES6CompileOption": false, + "minifyWXML": true + }, + "compileType": "miniprogram", + "libVersion": "2.19.4", + "appid": "wx44d047d87e6284d8", + "appid1": "wx63c280fe3248a3e7", + "projectname": "mini-program-test", + "debugOptions": { + "hidedInDevtools": [] + }, + "scripts": {}, + "staticServerOptions": { + "baseURL": "", + "servePath": "" + }, + "isGameTourist": false, + "condition": { + "search": { + "list": [] + }, + "conversation": { + "list": [] + }, + "game": { + "list": [] + }, + "plugin": { + "list": [] + }, + "gamePlugin": { + "list": [] + }, + "miniprogram": { + "list": [] + } + } +} \ No newline at end of file diff --git a/mini-program-test/sitemap.json b/mini-program-test/sitemap.json new file mode 100755 index 000000000..ca02add20 --- /dev/null +++ b/mini-program-test/sitemap.json @@ -0,0 +1,7 @@ +{ + "desc": "关于本文件的更多信息,请参考文档 https://developers.weixin.qq.com/miniprogram/dev/framework/sitemap.html", + "rules": [{ + "action": "allow", + "page": "*" + }] +} \ No newline at end of file diff --git a/mini-program-test/utils/common.js b/mini-program-test/utils/common.js new file mode 100755 index 000000000..a52d72520 --- /dev/null +++ b/mini-program-test/utils/common.js @@ -0,0 +1,3 @@ +module.exports = { + baseurl: "http://127.0.0.1:28080" +} diff --git a/mini-program-test/utils/util.js b/mini-program-test/utils/util.js new file mode 100755 index 000000000..764bc2ce2 --- /dev/null +++ b/mini-program-test/utils/util.js @@ -0,0 +1,19 @@ +const formatTime = date => { + const year = date.getFullYear() + const month = date.getMonth() + 1 + const day = date.getDate() + const hour = date.getHours() + const minute = date.getMinutes() + const second = date.getSeconds() + + return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}` +} + +const formatNumber = n => { + n = n.toString() + return n[1] ? n : `0${n}` +} + +module.exports = { + formatTime +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialUnbindReqVO.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialUnbindReqVO.java index 71cdc6fbb..096164ed7 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialUnbindReqVO.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialUnbindReqVO.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth; -import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum; +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; import cn.iocoder.yudao.framework.common.validation.InEnum; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserProfileController.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserProfileController.java index 523359b08..48236a7ab 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserProfileController.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserProfileController.java @@ -8,15 +8,16 @@ import cn.iocoder.yudao.adminserver.modules.system.convert.user.SysUserConvert; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysDeptDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysPostDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.permission.SysRoleDO; -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; import cn.iocoder.yudao.adminserver.modules.system.service.dept.SysDeptService; import cn.iocoder.yudao.adminserver.modules.system.service.dept.SysPostService; import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService; import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysRoleService; -import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService; import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO; import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO; +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService; import cn.iocoder.yudao.coreservice.modules.system.service.user.SysUserCoreService; +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import io.swagger.annotations.Api; @@ -77,7 +78,7 @@ public class SysUserProfileController { resp.setPosts(SysUserConvert.INSTANCE.convertList02(posts)); } // 获得社交用户信息 - List socialUsers = socialService.getSocialUserList(user.getId()); + List socialUsers = socialService.getSocialUserList(user.getId(), UserTypeEnum.ADMIN); resp.setSocialUsers(SysUserConvert.INSTANCE.convertList03(socialUsers)); return success(resp); } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/convert/user/SysUserConvert.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/convert/user/SysUserConvert.java index dbe5b365f..d25ce02ad 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/convert/user/SysUserConvert.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/convert/user/SysUserConvert.java @@ -7,7 +7,7 @@ import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.user.*; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysDeptDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysPostDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.permission.SysRoleDO; -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO; import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysAuthServiceImplTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysAuthServiceImplTest.java index 51e59dc0a..c09a194aa 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysAuthServiceImplTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysAuthServiceImplTest.java @@ -7,11 +7,11 @@ import cn.iocoder.yudao.adminserver.modules.system.enums.logger.SysLoginResultEn import cn.iocoder.yudao.adminserver.modules.system.service.auth.impl.SysAuthServiceImpl; import cn.iocoder.yudao.adminserver.modules.system.service.common.SysCaptchaService; import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService; -import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService; import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService; import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO; import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService; import cn.iocoder.yudao.coreservice.modules.system.service.logger.SysLoginLogCoreService; +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService; import cn.iocoder.yudao.coreservice.modules.system.service.user.SysUserCoreService; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.security.core.LoginUser; diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialServiceTest.java index badaf0e76..cec84c1ed 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialServiceTest.java @@ -1,11 +1,11 @@ package cn.iocoder.yudao.adminserver.modules.system.service.social; import cn.iocoder.yudao.adminserver.BaseDbAndRedisUnitTest; -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; -import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social.SysSocialUserMapper; -import cn.iocoder.yudao.adminserver.modules.system.dal.redis.social.SysSocialAuthUserRedisDAO; -import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum; -import cn.iocoder.yudao.adminserver.modules.system.service.social.impl.SysSocialServiceImpl; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO; +import cn.iocoder.yudao.coreservice.modules.system.dal.mysql.social.SysSocialUserMapper; +import cn.iocoder.yudao.coreservice.modules.system.dal.redis.social.SysSocialAuthUserRedisDAO; +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; +import cn.iocoder.yudao.coreservice.modules.system.service.social.impl.SysSocialServiceImpl; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import com.xkcoding.justauth.AuthRequestFactory; import me.zhyd.oauth.model.AuthUser; @@ -23,6 +23,7 @@ import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; import static org.junit.jupiter.api.Assertions.assertEquals; +// TODO @timfruit:这个单元测试,挪到 yudao-core-service /** * {@link SysSocialServiceImpl} 的单元测试类 * @@ -53,7 +54,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest { // mock 方法 // 调用 - socialService.bindSocialUser(userId, type, authUser); + socialService.bindSocialUser(userId, type, authUser, UserTypeEnum.ADMIN); // 断言 List socialUsers = socialUserMapper.selectList("user_id", userId); assertEquals(1, socialUsers.size()); @@ -78,7 +79,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest { // mock 方法 // 调用 - socialService.bindSocialUser(userId, type, authUser); + socialService.bindSocialUser(userId, type, authUser, UserTypeEnum.ADMIN); // 断言 List socialUsers = socialUserMapper.selectList("user_id", userId); assertEquals(1, socialUsers.size()); @@ -103,7 +104,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest { // mock 方法 // 调用 - socialService.bindSocialUser(userId, type, authUser); + socialService.bindSocialUser(userId, type, authUser, UserTypeEnum.ADMIN); // 断言 List socialUsers = socialUserMapper.selectList("user_id", userId); assertEquals(1, socialUsers.size()); @@ -140,7 +141,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest { String newUnionId = oldSocialUser.getUnionId(); // 调用 - socialService.unbindOldSocialUser(userId, type, newUnionId); + socialService.unbindOldSocialUser(userId, type, newUnionId, UserTypeEnum.ADMIN); // 断言 assertEquals(1L, socialUserMapper.selectCount(null).longValue()); } @@ -163,7 +164,7 @@ public class SysSocialServiceTest extends BaseDbAndRedisUnitTest { String newUnionId = randomString(10); // 调用 - socialService.unbindOldSocialUser(userId, type, newUnionId); + socialService.unbindOldSocialUser(userId, type, newUnionId, UserTypeEnum.ADMIN); // 断言 assertEquals(0L, socialUserMapper.selectCount(null).longValue()); } diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/SysSocialService.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/SysSocialService.java index ad72a5780..8a8df27d6 100644 --- a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/SysSocialService.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/SysSocialService.java @@ -59,7 +59,7 @@ public interface SysSocialService { * @param userId 用户编号 * @return 社交用户列表 */ - List getSocialUserList(Long userId,UserTypeEnum userTypeEnum); + List getSocialUserList(Long userId, UserTypeEnum userTypeEnum); /** * 绑定社交用户 diff --git a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/impl/SysSocialServiceImpl.java b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/impl/SysSocialServiceImpl.java index d1419c44f..bde40c3c4 100644 --- a/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/impl/SysSocialServiceImpl.java +++ b/yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/impl/SysSocialServiceImpl.java @@ -87,7 +87,7 @@ public class SysSocialServiceImpl implements SysSocialService { @Override @Transactional - public void bindSocialUser(Long userId, Integer type, AuthUser authUser,UserTypeEnum userTypeEnum) { + public void bindSocialUser(Long userId, Integer type, AuthUser authUser, UserTypeEnum userTypeEnum) { // 获得 unionId 对应的 SysSocialUserDO 列表 String unionId = getAuthUserUnionId(authUser); List socialUsers = this.getAllSocialUserList(type, unionId, userTypeEnum); diff --git a/yudao-user-server/src/main/resources/application-local.yaml b/yudao-user-server/src/main/resources/application-local.yaml index 1e904c3ee..c39a7cb1b 100644 --- a/yudao-user-server/src/main/resources/application-local.yaml +++ b/yudao-user-server/src/main/resources/application-local.yaml @@ -156,7 +156,6 @@ yudao: pay-notify-url: http://niubi.natapp1.cc/api/pay/order/notify refund-notify-url: http://niubi.natapp1.cc/api/pay/refund/notify - justauth: enabled: true type: @@ -169,10 +168,12 @@ justauth: extend: enum-class: cn.iocoder.yudao.coreservice.modules.system.compent.justauth.AuthExtendSource config: - WECHAT_MINI_PROGRAM: # 微信小程序 扩展 + WECHAT_MINI_PROGRAM: # 微信小程序 https://www.yuque.com/docs/share/88e3d30a-6830-45fc-8c25-dae485aef3aa?#%20%E3%80%8A%E5%B0%8F%E7%A8%8B%E5%BA%8F%E6%8E%88%E6%9D%83%E7%99%BB%E5%BD%95%E3%80%8B request-class: cn.iocoder.yudao.coreservice.modules.system.compent.justauth.AuthWeChatMiniProgramRequest client-id: wx44d047d87e6284d8 client-secret: 21c3b7a8a51ee1b8f5cf875848ed4466 +# client-id: wx63c280fe3248a3e7 # TODO 芋艿:自己的测试,后续可以删除 +# client-secret: 6f270509224a7ae1296bbf1c8cb97aed ignore-check-redirect-uri: true ignore-check-state: true cache: diff --git a/更新日志.md b/更新日志.md index f66a804cd..cd0c94006 100644 --- a/更新日志.md +++ b/更新日志.md @@ -15,6 +15,7 @@ TODO * 支付 * 用户前台的社交登陆 +* 用户前台的修改手机、修改密码、忘记密码 ## [v1.1.0] 2021.10.25