Commit fcf1c228c58a727497bcc005f4ef5f06d733647e
1 parent
d194e147
初步完成微信公众号登录
Showing
28 changed files
with
275 additions
and
144 deletions
yudao-admin-server/pom.xml
| ... | ... | @@ -122,12 +122,6 @@ |
| 122 | 122 | <artifactId>screw-core</artifactId> <!-- 实现数据库文档 --> |
| 123 | 123 | </dependency> |
| 124 | 124 | |
| 125 | - <!-- TODO 后续看情况,进行调整 --> | |
| 126 | - <dependency> | |
| 127 | - <groupId>com.xkcoding.justauth</groupId> | |
| 128 | - <artifactId>justauth-spring-boot-starter</artifactId> | |
| 129 | - <version>1.4.0</version> | |
| 130 | - </dependency> | |
| 131 | 125 | </dependencies> |
| 132 | 126 | |
| 133 | 127 | <build> | ... | ... |
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 |
| 8 | 8 | import cn.iocoder.yudao.adminserver.modules.system.service.auth.SysAuthService; |
| 9 | 9 | import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService; |
| 10 | 10 | import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysRoleService; |
| 11 | -import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService; | |
| 12 | 11 | import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO; |
| 12 | +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService; | |
| 13 | 13 | import cn.iocoder.yudao.coreservice.modules.system.service.user.SysUserCoreService; |
| 14 | 14 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 15 | +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | |
| 15 | 16 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 16 | 17 | import cn.iocoder.yudao.framework.common.util.collection.SetUtils; |
| 17 | 18 | import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; |
| ... | ... | @@ -132,7 +133,7 @@ public class SysAuthController { |
| 132 | 133 | @DeleteMapping("/social-unbind") |
| 133 | 134 | @ApiOperation("取消社交绑定") |
| 134 | 135 | public CommonResult<Boolean> socialUnbind(@RequestBody SysAuthSocialUnbindReqVO reqVO) { |
| 135 | - socialService.unbindSocialUser(getLoginUserId(), reqVO.getType(), reqVO.getUnionId()); | |
| 136 | + socialService.unbindSocialUser(getLoginUserId(), reqVO.getType(), reqVO.getUnionId(), UserTypeEnum.ADMIN); | |
| 136 | 137 | return CommonResult.success(true); |
| 137 | 138 | } |
| 138 | 139 | ... | ... |
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialBindReqVO.java
| 1 | 1 | package cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum; | |
| 3 | +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; | |
| 4 | 4 | import cn.iocoder.yudao.framework.common.validation.InEnum; |
| 5 | 5 | import io.swagger.annotations.ApiModel; |
| 6 | 6 | import io.swagger.annotations.ApiModelProperty; | ... | ... |
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialLogin2ReqVO.java
| 1 | 1 | package cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum; | |
| 3 | +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; | |
| 4 | 4 | import cn.iocoder.yudao.framework.common.validation.InEnum; |
| 5 | 5 | import io.swagger.annotations.ApiModel; |
| 6 | 6 | import io.swagger.annotations.ApiModelProperty; | ... | ... |
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/vo/auth/SysAuthSocialLoginReqVO.java
| 1 | 1 | package cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum; | |
| 3 | +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; | |
| 4 | 4 | import cn.iocoder.yudao.framework.common.validation.InEnum; |
| 5 | 5 | import io.swagger.annotations.ApiModel; |
| 6 | 6 | import io.swagger.annotations.ApiModelProperty; | ... | ... |
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/SysRedisKeyConstants.java
| ... | ... | @@ -19,12 +19,5 @@ public interface SysRedisKeyConstants { |
| 19 | 19 | "captcha_code:%s", // 参数为 uuid |
| 20 | 20 | STRING, String.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC); |
| 21 | 21 | |
| 22 | - RedisKeyDefine SOCIAL_AUTH_USER = new RedisKeyDefine("社交的授权用户", | |
| 23 | - "social_auth_user:%d:%s", // 参数为 type,code | |
| 24 | - STRING, AuthUser.class, Duration.ofDays(1)); | |
| 25 | - | |
| 26 | - RedisKeyDefine SOCIAL_AUTH_STATE = new RedisKeyDefine("社交的 state", | |
| 27 | - "social_auth_state:%s", // 参数为 state | |
| 28 | - STRING, String.class, Duration.ofHours(24)); // 值为 state | |
| 29 | 22 | |
| 30 | 23 | } | ... | ... |
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/enums/SysErrorCodeConstants.java
| ... | ... | @@ -90,8 +90,6 @@ public interface SysErrorCodeConstants { |
| 90 | 90 | ErrorCode ERROR_CODE_NOT_EXISTS = new ErrorCode(1002013000, "错误码不存在"); |
| 91 | 91 | ErrorCode ERROR_CODE_DUPLICATE = new ErrorCode(1002013001, "已经存在编码为【{}】的错误码"); |
| 92 | 92 | |
| 93 | - // ========== 社交模块 1002014000 ========== | |
| 94 | - ErrorCode SOCIAL_AUTH_FAILURE = new ErrorCode(1002014000, "社交授权失败,原因是:{}"); | |
| 95 | - ErrorCode SOCIAL_UNBIND_NOT_SELF = new ErrorCode(1002014001, "社交解绑失败,非当前用户绑定"); | |
| 93 | + | |
| 96 | 94 | |
| 97 | 95 | } | ... | ... |
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 |
| 6 | 6 | import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth.SysAuthSocialLogin2ReqVO; |
| 7 | 7 | import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth.SysAuthSocialLoginReqVO; |
| 8 | 8 | import cn.iocoder.yudao.adminserver.modules.system.convert.auth.SysAuthConvert; |
| 9 | -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; | |
| 10 | 9 | import cn.iocoder.yudao.adminserver.modules.system.enums.logger.SysLoginLogTypeEnum; |
| 11 | 10 | import cn.iocoder.yudao.adminserver.modules.system.enums.logger.SysLoginResultEnum; |
| 12 | 11 | import cn.iocoder.yudao.adminserver.modules.system.service.auth.SysAuthService; |
| 13 | 12 | import cn.iocoder.yudao.adminserver.modules.system.service.common.SysCaptchaService; |
| 14 | 13 | import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService; |
| 15 | -import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService; | |
| 16 | 14 | import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService; |
| 15 | +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO; | |
| 17 | 16 | import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO; |
| 18 | 17 | import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService; |
| 19 | 18 | import cn.iocoder.yudao.coreservice.modules.system.service.logger.SysLoginLogCoreService; |
| 20 | 19 | import cn.iocoder.yudao.coreservice.modules.system.service.logger.dto.SysLoginLogCreateReqDTO; |
| 20 | +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService; | |
| 21 | 21 | import cn.iocoder.yudao.coreservice.modules.system.service.user.SysUserCoreService; |
| 22 | 22 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 23 | 23 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| ... | ... | @@ -75,6 +75,8 @@ public class SysAuthServiceImpl implements SysAuthService { |
| 75 | 75 | @Resource |
| 76 | 76 | private SysSocialService socialService; |
| 77 | 77 | |
| 78 | + private static final UserTypeEnum userTypeEnum = UserTypeEnum.ADMIN; | |
| 79 | + | |
| 78 | 80 | @Override |
| 79 | 81 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { |
| 80 | 82 | // 获取 username 对应的 SysUserDO |
| ... | ... | @@ -198,7 +200,7 @@ public class SysAuthServiceImpl implements SysAuthService { |
| 198 | 200 | |
| 199 | 201 | // 如果未绑定 SysSocialUserDO 用户,则无法自动登录,进行报错 |
| 200 | 202 | String unionId = socialService.getAuthUserUnionId(authUser); |
| 201 | - List<SysSocialUserDO> socialUsers = socialService.getAllSocialUserList(reqVO.getType(), unionId); | |
| 203 | + List<SysSocialUserDO> socialUsers = socialService.getAllSocialUserList(reqVO.getType(), unionId, userTypeEnum); | |
| 202 | 204 | if (CollUtil.isEmpty(socialUsers)) { |
| 203 | 205 | throw exception(AUTH_THIRD_LOGIN_NOT_BIND); |
| 204 | 206 | } |
| ... | ... | @@ -216,7 +218,7 @@ public class SysAuthServiceImpl implements SysAuthService { |
| 216 | 218 | loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 |
| 217 | 219 | |
| 218 | 220 | // 绑定社交用户(更新) |
| 219 | - socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser); | |
| 221 | + socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum); | |
| 220 | 222 | |
| 221 | 223 | // 缓存登录用户到 Redis 中,返回 sessionId 编号 |
| 222 | 224 | return userSessionCoreService.createUserSession(loginUser, userIp, userAgent); |
| ... | ... | @@ -233,7 +235,7 @@ public class SysAuthServiceImpl implements SysAuthService { |
| 233 | 235 | loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 |
| 234 | 236 | |
| 235 | 237 | // 绑定社交用户(新增) |
| 236 | - socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser); | |
| 238 | + socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum); | |
| 237 | 239 | |
| 238 | 240 | // 缓存登录用户到 Redis 中,返回 sessionId 编号 |
| 239 | 241 | return userSessionCoreService.createUserSession(loginUser, userIp, userAgent); |
| ... | ... | @@ -246,7 +248,7 @@ public class SysAuthServiceImpl implements SysAuthService { |
| 246 | 248 | Assert.notNull(authUser, "授权用户不为空"); |
| 247 | 249 | |
| 248 | 250 | // 绑定社交用户(新增) |
| 249 | - socialService.bindSocialUser(userId, reqVO.getType(), authUser); | |
| 251 | + socialService.bindSocialUser(userId, reqVO.getType(), authUser, userTypeEnum); | |
| 250 | 252 | } |
| 251 | 253 | |
| 252 | 254 | @Override |
| ... | ... | @@ -267,7 +269,7 @@ public class SysAuthServiceImpl implements SysAuthService { |
| 267 | 269 | reqDTO.setLogType(SysLoginLogTypeEnum.LOGOUT_SELF.getType()); |
| 268 | 270 | reqDTO.setTraceId(TracerUtils.getTraceId()); |
| 269 | 271 | reqDTO.setUserId(userId); |
| 270 | - reqDTO.setUserType(UserTypeEnum.ADMIN.getValue()); | |
| 272 | + reqDTO.setUserType(userTypeEnum.getValue()); | |
| 271 | 273 | reqDTO.setUsername(username); |
| 272 | 274 | reqDTO.setUserAgent(ServletUtils.getUserAgent()); |
| 273 | 275 | reqDTO.setUserIp(ServletUtils.getClientIP()); | ... | ... |
yudao-core-service/pom.xml
| ... | ... | @@ -85,6 +85,12 @@ |
| 85 | 85 | <groupId>com.google.guava</groupId> |
| 86 | 86 | <artifactId>guava</artifactId> |
| 87 | 87 | </dependency> |
| 88 | + | |
| 89 | + <dependency> | |
| 90 | + <groupId>com.xkcoding.justauth</groupId> | |
| 91 | + <artifactId>justauth-spring-boot-starter</artifactId> | |
| 92 | + <version>1.4.0</version> | |
| 93 | + </dependency> | |
| 88 | 94 | </dependencies> |
| 89 | 95 | |
| 90 | 96 | </project> | ... | ... |
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/social/SysSocialUserDO.java renamed to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/dataobject/social/SysSocialUserDO.java
| 1 | -package cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social; | |
| 1 | +package cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | ... | ... |
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/social/SysSocialUserMapper.java renamed to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/social/SysSocialUserMapper.java
| 1 | -package cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social; | |
| 1 | +package cn.iocoder.yudao.coreservice.modules.system.dal.mysql.social; | |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; | |
| 3 | +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO; | |
| 4 | 4 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 5 | 5 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| 6 | 6 | import org.apache.ibatis.annotations.Mapper; | ... | ... |
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; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.redis.core.RedisKeyDefine; |
| 4 | 4 | import cn.iocoder.yudao.framework.security.core.LoginUser; |
| 5 | +import me.zhyd.oauth.model.AuthUser; | |
| 6 | + | |
| 7 | +import java.time.Duration; | |
| 5 | 8 | |
| 6 | 9 | import static cn.iocoder.yudao.framework.redis.core.RedisKeyDefine.KeyTypeEnum.STRING; |
| 7 | 10 | |
| ... | ... | @@ -16,4 +19,11 @@ public interface SysRedisKeyCoreConstants { |
| 16 | 19 | "login_user:%s", // 参数为 sessionId |
| 17 | 20 | STRING, LoginUser.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC); |
| 18 | 21 | |
| 22 | + RedisKeyDefine SOCIAL_AUTH_USER = new RedisKeyDefine("社交的授权用户", | |
| 23 | + "social_auth_user:%d:%s", // 参数为 type,code | |
| 24 | + STRING, AuthUser.class, Duration.ofDays(1)); | |
| 25 | + | |
| 26 | + RedisKeyDefine SOCIAL_AUTH_STATE = new RedisKeyDefine("社交的 state", | |
| 27 | + "social_auth_state:%s", // 参数为 state | |
| 28 | + STRING, String.class, Duration.ofHours(24)); // 值为 state | |
| 19 | 29 | } | ... | ... |
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/redis/social/SysSocialAuthUserRedisDAO.java renamed to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/social/SysSocialAuthUserRedisDAO.java
| 1 | -package cn.iocoder.yudao.adminserver.modules.system.dal.redis.social; | |
| 1 | +package cn.iocoder.yudao.coreservice.modules.system.dal.redis.social; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 4 | 4 | import me.zhyd.oauth.model.AuthCallback; |
| ... | ... | @@ -8,10 +8,11 @@ import org.springframework.stereotype.Repository; |
| 8 | 8 | |
| 9 | 9 | import javax.annotation.Resource; |
| 10 | 10 | |
| 11 | -import static cn.iocoder.yudao.adminserver.modules.system.dal.redis.SysRedisKeyConstants.SOCIAL_AUTH_USER; | |
| 11 | +import static cn.iocoder.yudao.coreservice.modules.system.dal.redis.SysRedisKeyCoreConstants.SOCIAL_AUTH_USER; | |
| 12 | + | |
| 12 | 13 | |
| 13 | 14 | /** |
| 14 | - * 社交 {@link me.zhyd.oauth.model.AuthUser} 的 RedisDAO | |
| 15 | + * 社交 {@link AuthUser} 的 RedisDAO | |
| 15 | 16 | * |
| 16 | 17 | * @author 芋道源码 |
| 17 | 18 | */ | ... | ... |
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/SysErrorCodeConstants.java
| ... | ... | @@ -14,4 +14,9 @@ public interface SysErrorCodeConstants { |
| 14 | 14 | ErrorCode SMS_SEND_MOBILE_TEMPLATE_PARAM_MISS = new ErrorCode(1006000001, "模板参数({})缺失"); |
| 15 | 15 | ErrorCode SMS_SEND_TEMPLATE_NOT_EXISTS = new ErrorCode(1006000000, "短信模板不存在"); |
| 16 | 16 | |
| 17 | + | |
| 18 | + // ========== 社交模块 1006001000 ========== | |
| 19 | + ErrorCode SOCIAL_AUTH_FAILURE = new ErrorCode(1006001000, "社交授权失败,原因是:{}"); | |
| 20 | + ErrorCode SOCIAL_UNBIND_NOT_SELF = new ErrorCode(1006001001, "社交解绑失败,非当前用户绑定"); | |
| 21 | + | |
| 17 | 22 | } | ... | ... |
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/enums/social/SysSocialTypeEnum.java renamed to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/enums/social/SysSocialTypeEnum.java
| 1 | -package cn.iocoder.yudao.adminserver.modules.system.enums.social; | |
| 1 | +package cn.iocoder.yudao.coreservice.modules.system.enums.social; | |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.ListUtil; |
| 4 | 4 | import cn.hutool.core.util.ArrayUtil; |
| ... | ... | @@ -21,11 +21,19 @@ public enum SysSocialTypeEnum implements IntArrayValuable { |
| 21 | 21 | GITEE(10, "GITEE"), // https://gitee.com/api/v5/oauth_doc#/ |
| 22 | 22 | DINGTALK(20, "DINGTALK"), // https://developers.dingtalk.com/document/app/obtain-identity-credentials |
| 23 | 23 | WECHAT_ENTERPRISE(30, "WECHAT_ENTERPRISE"), // https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html |
| 24 | + /** | |
| 25 | + * 微信公众平台 - H5 | |
| 26 | + */ | |
| 27 | + WECHAT_MP(12, "WECHAT_MP"), // https://www.cnblogs.com/juewuzhe/p/11905461.html | |
| 28 | + /** | |
| 29 | + * 微信开放平台 - 小程序 | |
| 30 | + */ | |
| 31 | + 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 | |
| 24 | 32 | ; |
| 25 | 33 | |
| 26 | 34 | public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(SysSocialTypeEnum::getType).toArray(); |
| 27 | 35 | |
| 28 | - public static final List<Integer> WECHAT_ALL = ListUtil.toList(WECHAT_ENTERPRISE.type); | |
| 36 | + public static final List<Integer> WECHAT_ALL = ListUtil.toList(WECHAT_ENTERPRISE.type, WECHAT_MP.type, WECHAT_OPEN.type); | |
| 29 | 37 | |
| 30 | 38 | /** |
| 31 | 39 | * 类型 | ... | ... |
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialService.java renamed to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/SysSocialService.java
| 1 | -package cn.iocoder.yudao.adminserver.modules.system.service.social; | |
| 1 | +package cn.iocoder.yudao.coreservice.modules.system.service.social; | |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.util.StrUtil; |
| 4 | -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; | |
| 5 | -import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum; | |
| 4 | +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO; | |
| 5 | +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; | |
| 6 | +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | |
| 6 | 7 | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| 7 | 8 | import me.zhyd.oauth.model.AuthUser; |
| 8 | 9 | |
| ... | ... | @@ -49,7 +50,7 @@ public interface SysSocialService { |
| 49 | 50 | * @param unionId 社交平台的 unionId |
| 50 | 51 | * @return 社交用户列表 |
| 51 | 52 | */ |
| 52 | - List<SysSocialUserDO> getAllSocialUserList(Integer type, String unionId); | |
| 53 | + List<SysSocialUserDO> getAllSocialUserList(Integer type, String unionId, UserTypeEnum userTypeEnum); | |
| 53 | 54 | |
| 54 | 55 | /** |
| 55 | 56 | * 获得指定用户的社交用户列表 |
| ... | ... | @@ -57,7 +58,7 @@ public interface SysSocialService { |
| 57 | 58 | * @param userId 用户编号 |
| 58 | 59 | * @return 社交用户列表 |
| 59 | 60 | */ |
| 60 | - List<SysSocialUserDO> getSocialUserList(Long userId); | |
| 61 | + List<SysSocialUserDO> getSocialUserList(Long userId,UserTypeEnum userTypeEnum); | |
| 61 | 62 | |
| 62 | 63 | /** |
| 63 | 64 | * 绑定社交用户 |
| ... | ... | @@ -66,7 +67,7 @@ public interface SysSocialService { |
| 66 | 67 | * @param type 社交平台的类型 {@link SysSocialTypeEnum} |
| 67 | 68 | * @param authUser 授权用户 |
| 68 | 69 | */ |
| 69 | - void bindSocialUser(Long userId, Integer type, AuthUser authUser); | |
| 70 | + void bindSocialUser(Long userId, Integer type, AuthUser authUser, UserTypeEnum userTypeEnum); | |
| 70 | 71 | |
| 71 | 72 | /** |
| 72 | 73 | * 取消绑定社交用户 |
| ... | ... | @@ -75,6 +76,6 @@ public interface SysSocialService { |
| 75 | 76 | * @param type 社交平台的类型 {@link SysSocialTypeEnum} |
| 76 | 77 | * @param unionId 社交平台的 unionId |
| 77 | 78 | */ |
| 78 | - void unbindSocialUser(Long userId, Integer type, String unionId); | |
| 79 | + void unbindSocialUser(Long userId, Integer type, String unionId,UserTypeEnum userTypeEnum); | |
| 79 | 80 | |
| 80 | 81 | } | ... | ... |
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/impl/SysSocialServiceImpl.java renamed to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/impl/SysSocialServiceImpl.java
| 1 | -package cn.iocoder.yudao.adminserver.modules.system.service.social.impl; | |
| 1 | +package cn.iocoder.yudao.coreservice.modules.system.service.social.impl; | |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | -import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; | |
| 5 | -import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social.SysSocialUserMapper; | |
| 6 | -import cn.iocoder.yudao.adminserver.modules.system.dal.redis.social.SysSocialAuthUserRedisDAO; | |
| 7 | -import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum; | |
| 8 | -import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService; | |
| 4 | +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO; | |
| 5 | +import cn.iocoder.yudao.coreservice.modules.system.dal.mysql.social.SysSocialUserMapper; | |
| 6 | +import cn.iocoder.yudao.coreservice.modules.system.dal.redis.social.SysSocialAuthUserRedisDAO; | |
| 7 | +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; | |
| 8 | +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService; | |
| 9 | 9 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 10 | 10 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 11 | 11 | import cn.iocoder.yudao.framework.common.util.http.HttpUtils; |
| ... | ... | @@ -25,8 +25,8 @@ import javax.validation.Valid; |
| 25 | 25 | import java.util.List; |
| 26 | 26 | import java.util.Objects; |
| 27 | 27 | |
| 28 | -import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.SOCIAL_AUTH_FAILURE; | |
| 29 | -import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.SOCIAL_UNBIND_NOT_SELF; | |
| 28 | +import static cn.iocoder.yudao.coreservice.modules.system.enums.SysErrorCodeConstants.SOCIAL_AUTH_FAILURE; | |
| 29 | +import static cn.iocoder.yudao.coreservice.modules.system.enums.SysErrorCodeConstants.SOCIAL_UNBIND_NOT_SELF; | |
| 30 | 30 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 31 | 31 | import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; |
| 32 | 32 | |
| ... | ... | @@ -75,25 +75,25 @@ public class SysSocialServiceImpl implements SysSocialService { |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | @Override |
| 78 | - public List<SysSocialUserDO> getAllSocialUserList(Integer type, String unionId) { | |
| 78 | + public List<SysSocialUserDO> getAllSocialUserList(Integer type, String unionId,UserTypeEnum userTypeEnum) { | |
| 79 | 79 | List<Integer> types = SysSocialTypeEnum.getRelationTypes(type); |
| 80 | - return socialUserMapper.selectListByTypeAndUnionId(UserTypeEnum.ADMIN.getValue(), types, unionId); | |
| 80 | + return socialUserMapper.selectListByTypeAndUnionId(userTypeEnum.getValue(), types, unionId); | |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | @Override |
| 84 | - public List<SysSocialUserDO> getSocialUserList(Long userId) { | |
| 85 | - return socialUserMapper.selectListByUserId(UserTypeEnum.ADMIN.getValue(), userId); | |
| 84 | + public List<SysSocialUserDO> getSocialUserList(Long userId,UserTypeEnum userTypeEnum) { | |
| 85 | + return socialUserMapper.selectListByUserId(userTypeEnum.getValue(), userId); | |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | @Override |
| 89 | 89 | @Transactional |
| 90 | - public void bindSocialUser(Long userId, Integer type, AuthUser authUser) { | |
| 90 | + public void bindSocialUser(Long userId, Integer type, AuthUser authUser,UserTypeEnum userTypeEnum) { | |
| 91 | 91 | // 获得 unionId 对应的 SysSocialUserDO 列表 |
| 92 | 92 | String unionId = getAuthUserUnionId(authUser); |
| 93 | - List<SysSocialUserDO> socialUsers = this.getAllSocialUserList(type, unionId); | |
| 93 | + List<SysSocialUserDO> socialUsers = this.getAllSocialUserList(type, unionId, userTypeEnum); | |
| 94 | 94 | |
| 95 | 95 | // 逻辑一:如果 userId 之前绑定过该 type 的其它账号,需要进行解绑 |
| 96 | - this.unbindOldSocialUser(userId, type, unionId); | |
| 96 | + this.unbindOldSocialUser(userId, type, unionId, userTypeEnum); | |
| 97 | 97 | |
| 98 | 98 | // 逻辑二:如果 socialUsers 指定的 userId 改变,需要进行更新 |
| 99 | 99 | // 例如说,一个微信 unionId 对应了多个社交账号,结果其中有个关联了新的 userId,则其它也要跟着修改 |
| ... | ... | @@ -112,7 +112,7 @@ public class SysSocialServiceImpl implements SysSocialService { |
| 112 | 112 | .nickname(authUser.getNickname()).avatar(authUser.getAvatar()).rawUserInfo(toJsonString(authUser.getRawUserInfo())) |
| 113 | 113 | .build(); |
| 114 | 114 | if (socialUser == null) { |
| 115 | - saveSocialUser.setUserId(userId).setUserType(UserTypeEnum.ADMIN.getValue()) | |
| 115 | + saveSocialUser.setUserId(userId).setUserType(userTypeEnum.getValue()) | |
| 116 | 116 | .setType(type).setOpenid(authUser.getUuid()).setUnionId(unionId); |
| 117 | 117 | socialUserMapper.insert(saveSocialUser); |
| 118 | 118 | } else { |
| ... | ... | @@ -122,9 +122,9 @@ public class SysSocialServiceImpl implements SysSocialService { |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | @Override |
| 125 | - public void unbindSocialUser(Long userId, Integer type, String unionId) { | |
| 125 | + public void unbindSocialUser(Long userId, Integer type, String unionId, UserTypeEnum userTypeEnum) { | |
| 126 | 126 | // 获得 unionId 对应的所有 SysSocialUserDO 社交用户 |
| 127 | - List<SysSocialUserDO> socialUsers = this.getAllSocialUserList(type, unionId); | |
| 127 | + List<SysSocialUserDO> socialUsers = this.getAllSocialUserList(type, unionId, userTypeEnum); | |
| 128 | 128 | if (CollUtil.isEmpty(socialUsers)) { |
| 129 | 129 | return; |
| 130 | 130 | } |
| ... | ... | @@ -140,10 +140,10 @@ public class SysSocialServiceImpl implements SysSocialService { |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | @VisibleForTesting |
| 143 | - public void unbindOldSocialUser(Long userId, Integer type, String newUnionId) { | |
| 143 | + public void unbindOldSocialUser(Long userId, Integer type, String newUnionId, UserTypeEnum userTypeEnum) { | |
| 144 | 144 | List<Integer> types = SysSocialTypeEnum.getRelationTypes(type); |
| 145 | 145 | List<SysSocialUserDO> oldSocialUsers = socialUserMapper.selectListByTypeAndUserId( |
| 146 | - UserTypeEnum.ADMIN.getValue(), types, userId); | |
| 146 | + userTypeEnum.getValue(), types, userId); | |
| 147 | 147 | // 如果新老的 unionId 是一致的,说明无需解绑 |
| 148 | 148 | if (CollUtil.isEmpty(oldSocialUsers) || Objects.equals(newUnionId, oldSocialUsers.get(0).getUnionId())) { |
| 149 | 149 | return; | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/enums/social/SysSocialTypeEnum.java deleted
| 1 | -package cn.iocoder.yudao.userserver.modules.member.enums.social; | |
| 2 | - | |
| 3 | -import cn.hutool.core.collection.ListUtil; | |
| 4 | -import cn.hutool.core.util.ArrayUtil; | |
| 5 | -import cn.iocoder.yudao.framework.common.core.IntArrayValuable; | |
| 6 | -import lombok.AllArgsConstructor; | |
| 7 | -import lombok.Getter; | |
| 8 | - | |
| 9 | -import java.util.Arrays; | |
| 10 | -import java.util.List; | |
| 11 | - | |
| 12 | -/** | |
| 13 | - * 社交平台的类型枚举 | |
| 14 | - * | |
| 15 | - * @author 芋道源码 | |
| 16 | - */ | |
| 17 | -@Getter | |
| 18 | -@AllArgsConstructor | |
| 19 | -public enum SysSocialTypeEnum implements IntArrayValuable { | |
| 20 | - | |
| 21 | - GITEE(10, "GITEE"), // https://gitee.com/api/v5/oauth_doc#/ | |
| 22 | - DINGTALK(20, "DINGTALK"), // https://developers.dingtalk.com/document/app/obtain-identity-credentials | |
| 23 | - WECHAT_ENTERPRISE(30, "WECHAT_ENTERPRISE"), // https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html | |
| 24 | - ; | |
| 25 | - | |
| 26 | - public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(SysSocialTypeEnum::getType).toArray(); | |
| 27 | - | |
| 28 | - public static final List<Integer> WECHAT_ALL = ListUtil.toList(WECHAT_ENTERPRISE.type); | |
| 29 | - | |
| 30 | - /** | |
| 31 | - * 类型 | |
| 32 | - */ | |
| 33 | - private final Integer type; | |
| 34 | - /** | |
| 35 | - * 类型的标识 | |
| 36 | - */ | |
| 37 | - private final String source; | |
| 38 | - | |
| 39 | - @Override | |
| 40 | - public int[] array() { | |
| 41 | - return ARRAYS; | |
| 42 | - } | |
| 43 | - | |
| 44 | - public static SysSocialTypeEnum valueOfType(Integer type) { | |
| 45 | - return ArrayUtil.firstMatch(o -> o.getType().equals(type), values()); | |
| 46 | - } | |
| 47 | - | |
| 48 | - public static List<Integer> getRelationTypes(Integer type) { | |
| 49 | - if (WECHAT_ALL.contains(type)) { | |
| 50 | - return WECHAT_ALL; | |
| 51 | - } | |
| 52 | - return ListUtil.toList(type); | |
| 53 | - } | |
| 54 | - | |
| 55 | -} |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java
| 1 | 1 | package cn.iocoder.yudao.userserver.modules.system.controller.auth; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService; | |
| 4 | +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | |
| 3 | 5 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 6 | +import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; | |
| 4 | 7 | import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.*; |
| 5 | 8 | import cn.iocoder.yudao.userserver.modules.system.service.auth.SysAuthService; |
| 6 | 9 | import cn.iocoder.yudao.userserver.modules.system.service.sms.SysSmsCodeService; |
| 10 | +import com.alibaba.fastjson.JSON; | |
| 7 | 11 | import io.swagger.annotations.Api; |
| 8 | 12 | import io.swagger.annotations.ApiImplicitParam; |
| 9 | 13 | import io.swagger.annotations.ApiImplicitParams; |
| ... | ... | @@ -13,11 +17,13 @@ import org.springframework.validation.annotation.Validated; |
| 13 | 17 | import org.springframework.web.bind.annotation.*; |
| 14 | 18 | |
| 15 | 19 | import javax.annotation.Resource; |
| 20 | +import javax.servlet.http.HttpServletRequest; | |
| 16 | 21 | import javax.validation.Valid; |
| 17 | 22 | |
| 18 | 23 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 19 | 24 | import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP; |
| 20 | 25 | import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getUserAgent; |
| 26 | +import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; | |
| 21 | 27 | |
| 22 | 28 | @Api(tags = "认证") |
| 23 | 29 | @RestController |
| ... | ... | @@ -30,6 +36,9 @@ public class SysAuthController { |
| 30 | 36 | private SysAuthService authService; |
| 31 | 37 | @Resource |
| 32 | 38 | private SysSmsCodeService smsCodeService; |
| 39 | + @Resource | |
| 40 | + private SysSocialService socialService; | |
| 41 | + | |
| 33 | 42 | |
| 34 | 43 | @PostMapping("/login") |
| 35 | 44 | @ApiOperation("使用手机 + 密码登录") |
| ... | ... | @@ -70,42 +79,47 @@ public class SysAuthController { |
| 70 | 79 | }) |
| 71 | 80 | public CommonResult<String> socialAuthRedirect(@RequestParam("type") Integer type, |
| 72 | 81 | @RequestParam("redirectUri") String redirectUri) { |
| 73 | -// return CommonResult.success(socialService.getAuthorizeUrl(type, redirectUri)); | |
| 74 | - return null; | |
| 82 | + return CommonResult.success(socialService.getAuthorizeUrl(type, redirectUri)); | |
| 83 | + } | |
| 84 | + | |
| 85 | + @GetMapping("/social-login-get") | |
| 86 | + @ApiOperation("社交登录,使用 code 授权码") | |
| 87 | + @ResponseBody | |
| 88 | + @Deprecated | |
| 89 | + public CommonResult<MbrAuthSocialLoginReqVO> socialLoginGet(HttpServletRequest request,String code,String state) { | |
| 90 | + // 返回结果 | |
| 91 | + MbrAuthSocialLoginReqVO reqVO = MbrAuthSocialLoginReqVO.builder().state(state).code(code).build(); | |
| 92 | + reqVO.setType(12); | |
| 93 | + System.out.println(JSON.toJSON(reqVO)); | |
| 94 | + return success(reqVO); | |
| 75 | 95 | } |
| 76 | 96 | |
| 77 | 97 | @PostMapping("/social-login") |
| 78 | 98 | @ApiOperation("社交登录,使用 code 授权码") |
| 79 | - public CommonResult<SysAuthLoginRespVO> socialLogin(@RequestBody @Valid MbrAuthSocialLoginReqVO reqVO) { | |
| 80 | -// String token = authService.socialLogin(reqVO, getClientIP(), getUserAgent()); | |
| 81 | -// // 返回结果 | |
| 82 | -// return success(MbrAuthLoginRespVO.builder().token(token).build()); | |
| 83 | - return null; | |
| 99 | + public CommonResult<SysAuthLoginRespVO> socialLogin(@RequestBody @Valid MbrAuthSocialLoginReqVO reqVO) { | |
| 100 | + String token = authService.socialLogin(reqVO, getClientIP(), getUserAgent()); | |
| 101 | + return success(SysAuthLoginRespVO.builder().token(token).build()); | |
| 84 | 102 | } |
| 85 | 103 | |
| 86 | 104 | @PostMapping("/social-login2") |
| 87 | 105 | @ApiOperation("社交登录,使用 code 授权码 + 账号密码") |
| 88 | 106 | public CommonResult<SysAuthLoginRespVO> socialLogin2(@RequestBody @Valid MbrAuthSocialLogin2ReqVO reqVO) { |
| 89 | -// String token = authService.socialLogin2(reqVO, getClientIP(), getUserAgent()); | |
| 90 | -// // 返回结果 | |
| 91 | -// return success(MbrAuthLoginRespVO.builder().token(token).build()); | |
| 92 | - return null; | |
| 107 | + String token = authService.socialLogin2(reqVO, getClientIP(), getUserAgent()); | |
| 108 | + return success(SysAuthLoginRespVO.builder().token(token).build()); | |
| 93 | 109 | } |
| 94 | 110 | |
| 95 | 111 | @PostMapping("/social-bind") |
| 96 | 112 | @ApiOperation("社交绑定,使用 code 授权码") |
| 97 | 113 | public CommonResult<Boolean> socialBind(@RequestBody @Valid MbrAuthSocialBindReqVO reqVO) { |
| 98 | -// authService.socialBind(getLoginUserId(), reqVO); | |
| 99 | -// return CommonResult.success(true); | |
| 100 | - return null; | |
| 114 | + authService.socialBind(getLoginUserId(), reqVO); | |
| 115 | + return CommonResult.success(true); | |
| 101 | 116 | } |
| 102 | 117 | |
| 103 | 118 | @DeleteMapping("/social-unbind") |
| 104 | 119 | @ApiOperation("取消社交绑定") |
| 105 | 120 | public CommonResult<Boolean> socialUnbind(@RequestBody MbrAuthSocialUnbindReqVO reqVO) { |
| 106 | -// socialService.unbindSocialUser(getLoginUserId(), reqVO.getType(), reqVO.getUnionId()); | |
| 107 | -// return CommonResult.success(true); | |
| 108 | - return null; | |
| 121 | + socialService.unbindSocialUser(getLoginUserId(), reqVO.getType(), reqVO.getUnionId(), UserTypeEnum.MEMBER); | |
| 122 | + return CommonResult.success(true); | |
| 109 | 123 | } |
| 110 | 124 | |
| 111 | 125 | } | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialBindReqVO.java
| 1 | 1 | package cn.iocoder.yudao.userserver.modules.system.controller.auth.vo; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; | |
| 3 | 4 | import cn.iocoder.yudao.framework.common.validation.InEnum; |
| 4 | -import cn.iocoder.yudao.userserver.modules.member.enums.social.SysSocialTypeEnum; | |
| 5 | 5 | import io.swagger.annotations.ApiModel; |
| 6 | 6 | import io.swagger.annotations.ApiModelProperty; |
| 7 | 7 | import lombok.AllArgsConstructor; | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialLogin2ReqVO.java
| 1 | 1 | package cn.iocoder.yudao.userserver.modules.system.controller.auth.vo; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; | |
| 3 | 4 | import cn.iocoder.yudao.framework.common.validation.InEnum; |
| 4 | -import cn.iocoder.yudao.userserver.modules.member.enums.social.SysSocialTypeEnum; | |
| 5 | 5 | import io.swagger.annotations.ApiModel; |
| 6 | 6 | import io.swagger.annotations.ApiModelProperty; |
| 7 | 7 | import lombok.AllArgsConstructor; | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialLoginReqVO.java
| 1 | 1 | package cn.iocoder.yudao.userserver.modules.system.controller.auth.vo; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; | |
| 3 | 4 | import cn.iocoder.yudao.framework.common.validation.InEnum; |
| 4 | -import cn.iocoder.yudao.userserver.modules.member.enums.social.SysSocialTypeEnum; | |
| 5 | 5 | import io.swagger.annotations.ApiModel; |
| 6 | 6 | import io.swagger.annotations.ApiModelProperty; |
| 7 | 7 | import lombok.AllArgsConstructor; | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialUnbindReqVO.java
| 1 | 1 | package cn.iocoder.yudao.userserver.modules.system.controller.auth.vo; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum; | |
| 3 | 4 | import cn.iocoder.yudao.framework.common.validation.InEnum; |
| 4 | -import cn.iocoder.yudao.userserver.modules.member.enums.social.SysSocialTypeEnum; | |
| 5 | 5 | import io.swagger.annotations.ApiModel; |
| 6 | 6 | import io.swagger.annotations.ApiModelProperty; |
| 7 | 7 | import lombok.AllArgsConstructor; | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/enums/SysErrorCodeConstants.java
| ... | ... | @@ -14,6 +14,7 @@ public interface SysErrorCodeConstants { |
| 14 | 14 | ErrorCode AUTH_LOGIN_USER_DISABLED = new ErrorCode(1005000001, "登录失败,账号被禁用"); |
| 15 | 15 | ErrorCode AUTH_LOGIN_FAIL_UNKNOWN = new ErrorCode(1005000002, "登录失败"); // 登录失败的兜底,未知原因 |
| 16 | 16 | ErrorCode AUTH_TOKEN_EXPIRED = new ErrorCode(1005000003, "Token 已经过期"); |
| 17 | + ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1005000004, "未绑定账号,需要进行绑定"); | |
| 17 | 18 | |
| 18 | 19 | // ========== SMS CODE 模块 1005001000 ========== |
| 19 | 20 | ErrorCode USER_SMS_CODE_NOT_FOUND = new ErrorCode(1005001000, "验证码不存在"); |
| ... | ... | @@ -22,4 +23,7 @@ public interface SysErrorCodeConstants { |
| 22 | 23 | ErrorCode USER_SMS_CODE_NOT_CORRECT = new ErrorCode(1005001003, "验证码不正确"); |
| 23 | 24 | ErrorCode USER_SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY = new ErrorCode(1005001004, "超过每日短信发送数量"); |
| 24 | 25 | ErrorCode USER_SMS_CODE_SEND_TOO_FAST = new ErrorCode(1005001005, "短信发送过于频率"); |
| 26 | + | |
| 27 | + // ========== 用户模块 1005002000 ========== | |
| 28 | + ErrorCode USER_NOT_EXISTS = new ErrorCode(1005002001, "用户不存在"); | |
| 25 | 29 | } | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysAuthService.java
| 1 | 1 | package cn.iocoder.yudao.userserver.modules.system.service.auth; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.security.core.service.SecurityAuthFrameworkService; |
| 4 | -import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthLoginReqVO; | |
| 5 | -import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthSmsLoginReqVO; | |
| 4 | +import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.*; | |
| 6 | 5 | |
| 7 | 6 | import javax.validation.Valid; |
| 8 | 7 | |
| ... | ... | @@ -35,4 +34,33 @@ public interface SysAuthService extends SecurityAuthFrameworkService { |
| 35 | 34 | */ |
| 36 | 35 | String smsLogin(@Valid SysAuthSmsLoginReqVO reqVO, String userIp, String userAgent); |
| 37 | 36 | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * 社交登录,使用 code 授权码 | |
| 40 | + * | |
| 41 | + * @param reqVO 登录信息 | |
| 42 | + * @param userIp 用户 IP | |
| 43 | + * @param userAgent 用户 UA | |
| 44 | + * @return 身份令牌,使用 JWT 方式 | |
| 45 | + */ | |
| 46 | + String socialLogin(@Valid MbrAuthSocialLoginReqVO reqVO, String userIp, String userAgent); | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * 社交登录,使用 code 授权码 + 账号密码 | |
| 50 | + * | |
| 51 | + * @param reqVO 登录信息 | |
| 52 | + * @param userIp 用户 IP | |
| 53 | + * @param userAgent 用户 UA | |
| 54 | + * @return 身份令牌,使用 JWT 方式 | |
| 55 | + */ | |
| 56 | + String socialLogin2(@Valid MbrAuthSocialLogin2ReqVO reqVO, String userIp, String userAgent); | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * 社交绑定,使用 code 授权码 | |
| 60 | + * | |
| 61 | + * @param userId 用户编号 | |
| 62 | + * @param reqVO 绑定信息 | |
| 63 | + */ | |
| 64 | + void socialBind(Long userId, @Valid MbrAuthSocialBindReqVO reqVO); | |
| 65 | + | |
| 38 | 66 | } | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.userserver.modules.system.service.auth.impl; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.collection.CollUtil; | |
| 3 | 4 | import cn.hutool.core.lang.Assert; |
| 4 | 5 | import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO; |
| 6 | +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO; | |
| 7 | +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO; | |
| 5 | 8 | import cn.iocoder.yudao.coreservice.modules.system.enums.logger.SysLoginLogTypeEnum; |
| 6 | 9 | import cn.iocoder.yudao.coreservice.modules.system.enums.logger.SysLoginResultEnum; |
| 7 | 10 | import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService; |
| 8 | 11 | import cn.iocoder.yudao.coreservice.modules.system.service.logger.SysLoginLogCoreService; |
| 9 | 12 | import cn.iocoder.yudao.coreservice.modules.system.service.logger.dto.SysLoginLogCreateReqDTO; |
| 13 | +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService; | |
| 10 | 14 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 11 | 15 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 12 | 16 | import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; |
| 13 | 17 | import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; |
| 14 | 18 | import cn.iocoder.yudao.framework.security.core.LoginUser; |
| 15 | 19 | import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService; |
| 16 | -import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthLoginReqVO; | |
| 17 | -import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.SysAuthSmsLoginReqVO; | |
| 20 | +import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.*; | |
| 18 | 21 | import cn.iocoder.yudao.userserver.modules.system.convert.auth.SysAuthConvert; |
| 19 | 22 | import cn.iocoder.yudao.userserver.modules.system.enums.sms.SysSmsSceneEnum; |
| 20 | 23 | import cn.iocoder.yudao.userserver.modules.system.service.auth.SysAuthService; |
| 21 | 24 | import cn.iocoder.yudao.userserver.modules.system.service.sms.SysSmsCodeService; |
| 22 | 25 | import lombok.extern.slf4j.Slf4j; |
| 26 | +import me.zhyd.oauth.model.AuthUser; | |
| 23 | 27 | import org.springframework.context.annotation.Lazy; |
| 24 | 28 | import org.springframework.security.authentication.AuthenticationManager; |
| 25 | 29 | import org.springframework.security.authentication.BadCredentialsException; |
| ... | ... | @@ -33,6 +37,8 @@ import org.springframework.stereotype.Service; |
| 33 | 37 | import org.springframework.transaction.annotation.Transactional; |
| 34 | 38 | |
| 35 | 39 | import javax.annotation.Resource; |
| 40 | +import javax.validation.Valid; | |
| 41 | +import java.util.List; | |
| 36 | 42 | import java.util.Objects; |
| 37 | 43 | |
| 38 | 44 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| ... | ... | @@ -59,6 +65,9 @@ public class SysAuthServiceImpl implements SysAuthService { |
| 59 | 65 | private SysLoginLogCoreService loginLogCoreService; |
| 60 | 66 | @Resource |
| 61 | 67 | private SysUserSessionCoreService userSessionCoreService; |
| 68 | + @Resource | |
| 69 | + private SysSocialService socialService; | |
| 70 | + private static final UserTypeEnum userTypeEnum = UserTypeEnum.MEMBER; | |
| 62 | 71 | |
| 63 | 72 | @Override |
| 64 | 73 | public UserDetails loadUserByUsername(String mobile) throws UsernameNotFoundException { |
| ... | ... | @@ -99,6 +108,65 @@ public class SysAuthServiceImpl implements SysAuthService { |
| 99 | 108 | return userSessionCoreService.createUserSession(loginUser, userIp, userAgent); |
| 100 | 109 | } |
| 101 | 110 | |
| 111 | + @Override | |
| 112 | + public String socialLogin(MbrAuthSocialLoginReqVO reqVO, String userIp, String userAgent) { | |
| 113 | + // 使用 code 授权码,进行登录 | |
| 114 | + AuthUser authUser = socialService.getAuthUser(reqVO.getType(), reqVO.getCode(), reqVO.getState()); | |
| 115 | + org.springframework.util.Assert.notNull(authUser, "授权用户不为空"); | |
| 116 | + | |
| 117 | + // 如果未绑定 SysSocialUserDO 用户,则无法自动登录,进行报错 | |
| 118 | + String unionId = socialService.getAuthUserUnionId(authUser); | |
| 119 | + List<SysSocialUserDO> socialUsers = socialService.getAllSocialUserList(reqVO.getType(), unionId, userTypeEnum); | |
| 120 | + if (CollUtil.isEmpty(socialUsers)) { | |
| 121 | + throw exception(AUTH_THIRD_LOGIN_NOT_BIND); | |
| 122 | + } | |
| 123 | + | |
| 124 | + // 自动登录 | |
| 125 | + MbrUserDO user = userService.getUser(socialUsers.get(0).getUserId()); | |
| 126 | + if (user == null) { | |
| 127 | + throw exception(USER_NOT_EXISTS); | |
| 128 | + } | |
| 129 | + this.createLoginLog(user.getMobile(), SysLoginLogTypeEnum.LOGIN_SOCIAL, SysLoginResultEnum.SUCCESS); | |
| 130 | + | |
| 131 | + // 创建 LoginUser 对象 | |
| 132 | + LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user); | |
| 133 | + // TODO 芋艿:需要改造下,增加各种登录方式 | |
| 134 | +// loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 | |
| 135 | + | |
| 136 | + // 绑定社交用户(更新) | |
| 137 | + socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum); | |
| 138 | + | |
| 139 | + // 缓存登录用户到 Redis 中,返回 sessionId 编号 | |
| 140 | + return userSessionCoreService.createUserSession(loginUser, userIp, userAgent); | |
| 141 | + } | |
| 142 | + | |
| 143 | + @Override | |
| 144 | + public String socialLogin2(MbrAuthSocialLogin2ReqVO reqVO, String userIp, String userAgent) { | |
| 145 | + // 使用 code 授权码,进行登录 | |
| 146 | + AuthUser authUser = socialService.getAuthUser(reqVO.getType(), reqVO.getCode(), reqVO.getState()); | |
| 147 | + org.springframework.util.Assert.notNull(authUser, "授权用户不为空"); | |
| 148 | + | |
| 149 | + // 使用账号密码,进行登录。 | |
| 150 | + LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword()); | |
| 151 | +// loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 | |
| 152 | + | |
| 153 | + // 绑定社交用户(新增) | |
| 154 | + socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum); | |
| 155 | + | |
| 156 | + // 缓存登录用户到 Redis 中,返回 sessionId 编号 | |
| 157 | + return userSessionCoreService.createUserSession(loginUser, userIp, userAgent); | |
| 158 | + } | |
| 159 | + | |
| 160 | + @Override | |
| 161 | + public void socialBind(Long userId, MbrAuthSocialBindReqVO reqVO) { | |
| 162 | + // 使用 code 授权码,进行登录 | |
| 163 | + AuthUser authUser = socialService.getAuthUser(reqVO.getType(), reqVO.getCode(), reqVO.getState()); | |
| 164 | + org.springframework.util.Assert.notNull(authUser, "授权用户不为空"); | |
| 165 | + | |
| 166 | + // 绑定社交用户(新增) | |
| 167 | + socialService.bindSocialUser(userId, reqVO.getType(), authUser, userTypeEnum); | |
| 168 | + } | |
| 169 | + | |
| 102 | 170 | private LoginUser login0(String username, String password) { |
| 103 | 171 | final SysLoginLogTypeEnum logTypeEnum = SysLoginLogTypeEnum.LOGIN_USERNAME; |
| 104 | 172 | // 用户验证 |
| ... | ... | @@ -207,7 +275,7 @@ public class SysAuthServiceImpl implements SysAuthService { |
| 207 | 275 | reqDTO.setLogType(SysLoginLogTypeEnum.LOGOUT_SELF.getType()); |
| 208 | 276 | reqDTO.setTraceId(TracerUtils.getTraceId()); |
| 209 | 277 | reqDTO.setUserId(userId); |
| 210 | - reqDTO.setUserType(UserTypeEnum.MEMBER.getValue()); | |
| 278 | + reqDTO.setUserType(userTypeEnum.getValue()); | |
| 211 | 279 | reqDTO.setUsername(username); |
| 212 | 280 | reqDTO.setUserAgent(ServletUtils.getUserAgent()); |
| 213 | 281 | reqDTO.setUserIp(ServletUtils.getClientIP()); | ... | ... |
yudao-user-server/src/main/resources/application-dev.yaml
| ... | ... | @@ -138,3 +138,30 @@ yudao: |
| 138 | 138 | - ${spring.boot.admin.context-path}/** # 不处理 Spring Boot Admin 的请求 |
| 139 | 139 | - ${management.endpoints.web.base-path}/** # 不处理 Actuator 的请求 |
| 140 | 140 | demo: true # 开启演示模式 |
| 141 | + | |
| 142 | + | |
| 143 | +justauth: | |
| 144 | + enabled: true | |
| 145 | + type: | |
| 146 | + GITEE: # Gitee | |
| 147 | + client-id: ee61f0374a4c6c404a8717094caa7a410d76950e45ff60348015830c519ba5c1 | |
| 148 | + client-secret: 7c044a5671be3b051414db0cf2cec6ad702dd298d2416ba24ceaf608e6fa26f9 | |
| 149 | + ignore-check-redirect-uri: true | |
| 150 | + DINGTALK: # 钉钉 | |
| 151 | + client-id: dingvrnreaje3yqvzhxg | |
| 152 | + client-secret: i8E6iZyDvZj51JIb0tYsYfVQYOks9Cq1lgryEjFRqC79P3iJcrxEwT6Qk2QvLrLI | |
| 153 | + ignore-check-redirect-uri: true | |
| 154 | + WECHAT_ENTERPRISE: # 企业微信 | |
| 155 | + client-id: wwd411c69a39ad2e54 | |
| 156 | + client-secret: 1wTb7hYxnpT2TUbIeHGXGo7T0odav1ic10mLdyyATOw | |
| 157 | + agent-id: 1000004 | |
| 158 | + ignore-check-redirect-uri: true | |
| 159 | + WECHAT_MP: # 微信公众平台 - H5 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index | |
| 160 | + client-id: wxa5a05b85ac361f96 | |
| 161 | + client-secret: 247073c7cebb67f27f0e769195c2a57e | |
| 162 | + ignore-check-redirect-uri: true | |
| 163 | + cache: | |
| 164 | + type: REDIS | |
| 165 | + prefix: 'social_auth_state:' # 缓存前缀,目前只对 Redis 缓存生效,默认 JUSTAUTH::STATE:: | |
| 166 | + timeout: 24h # 超时时长,目前只对 Redis 缓存生效,默认 3 分钟 | |
| 167 | + | ... | ... |
yudao-user-server/src/main/resources/application-local.yaml
| ... | ... | @@ -140,3 +140,29 @@ yudao: |
| 140 | 140 | - ${spring.boot.admin.context-path}/** # 不处理 Spring Boot Admin 的请求 |
| 141 | 141 | - ${management.endpoints.web.base-path}/** # 不处理 Actuator 的请求 |
| 142 | 142 | demo: false # 关闭演示模式 |
| 143 | + | |
| 144 | + | |
| 145 | +justauth: | |
| 146 | + enabled: true | |
| 147 | + type: | |
| 148 | + GITEE: # Gitee | |
| 149 | + client-id: ee61f0374a4c6c404a8717094caa7a410d76950e45ff60348015830c519ba5c1 | |
| 150 | + client-secret: 7c044a5671be3b051414db0cf2cec6ad702dd298d2416ba24ceaf608e6fa26f9 | |
| 151 | + ignore-check-redirect-uri: true | |
| 152 | + DINGTALK: # 钉钉 | |
| 153 | + client-id: dingvrnreaje3yqvzhxg | |
| 154 | + client-secret: i8E6iZyDvZj51JIb0tYsYfVQYOks9Cq1lgryEjFRqC79P3iJcrxEwT6Qk2QvLrLI | |
| 155 | + ignore-check-redirect-uri: true | |
| 156 | + WECHAT_ENTERPRISE: # 企业微信 | |
| 157 | + client-id: wwd411c69a39ad2e54 | |
| 158 | + client-secret: 1wTb7hYxnpT2TUbIeHGXGo7T0odav1ic10mLdyyATOw | |
| 159 | + agent-id: 1000004 | |
| 160 | + ignore-check-redirect-uri: true | |
| 161 | + WECHAT_MP: # 微信公众平台 - H5 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index | |
| 162 | + client-id: wxa5a05b85ac361f96 | |
| 163 | + client-secret: 247073c7cebb67f27f0e769195c2a57e | |
| 164 | + ignore-check-redirect-uri: true | |
| 165 | + cache: | |
| 166 | + type: REDIS | |
| 167 | + prefix: 'social_auth_state:' # 缓存前缀,目前只对 Redis 缓存生效,默认 JUSTAUTH::STATE:: | |
| 168 | + timeout: 24h # 超时时长,目前只对 Redis 缓存生效,默认 3 分钟 | |
| 143 | 169 | \ No newline at end of file | ... | ... |