Commit 8aa45406fdaaaf4b8c1604d31b65c020d8ff8a0d

Authored by timfruit
1 parent 8dbd6143

拓展授权登录抽取成单独的starter,拓展配置和默认配置齐平

Showing 23 changed files with 288 additions and 99 deletions
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/SysAuthController.java
... ... @@ -9,7 +9,7 @@ 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 11 import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO;
12   -import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService;
  12 +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialCoreService;
13 13 import cn.iocoder.yudao.coreservice.modules.system.service.user.SysUserCoreService;
14 14 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
15 15 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
... ... @@ -50,7 +50,7 @@ public class SysAuthController {
50 50 @Resource
51 51 private SysPermissionService permissionService;
52 52 @Resource
53   - private SysSocialService socialService;
  53 + private SysSocialCoreService socialService;
54 54  
55 55 @PostMapping("/login")
56 56 @ApiOperation("使用账号密码登录")
... ...
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserProfileController.java
... ... @@ -15,7 +15,7 @@ import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysRoleSer
15 15 import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService;
16 16 import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO;
17 17 import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO;
18   -import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService;
  18 +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialCoreService;
19 19 import cn.iocoder.yudao.coreservice.modules.system.service.user.SysUserCoreService;
20 20 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
21 21 import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
... ... @@ -56,7 +56,7 @@ public class SysUserProfileController {
56 56 @Resource
57 57 private SysRoleService roleService;
58 58 @Resource
59   - private SysSocialService socialService;
  59 + private SysSocialCoreService socialService;
60 60  
61 61 @GetMapping("/get")
62 62 @ApiOperation("获得登录用户信息")
... ...
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java
... ... @@ -17,7 +17,7 @@ import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO
17 17 import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService;
18 18 import cn.iocoder.yudao.coreservice.modules.system.service.logger.SysLoginLogCoreService;
19 19 import cn.iocoder.yudao.coreservice.modules.system.service.logger.dto.SysLoginLogCreateReqDTO;
20   -import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService;
  20 +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialCoreService;
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;
... ... @@ -73,10 +73,9 @@ public class SysAuthServiceImpl implements SysAuthService {
73 73 @Resource
74 74 private SysUserSessionCoreService userSessionCoreService;
75 75 @Resource
76   - private SysSocialService socialService;
  76 + private SysSocialCoreService socialService;
77 77  
78   - // TODO @timfruit:静态枚举类,需要都大写,例如说 USER_TYPE_ENUM;静态变量,放在普通变量前面;这个实践不错哈。
79   - private static final UserTypeEnum userTypeEnum = UserTypeEnum.ADMIN;
  78 + private static final UserTypeEnum USER_TYPE_ENUM = UserTypeEnum.ADMIN;
80 79  
81 80 @Override
82 81 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
... ... @@ -201,7 +200,7 @@ public class SysAuthServiceImpl implements SysAuthService {
201 200  
202 201 // 如果未绑定 SysSocialUserDO 用户,则无法自动登录,进行报错
203 202 String unionId = socialService.getAuthUserUnionId(authUser);
204   - List<SysSocialUserDO> socialUsers = socialService.getAllSocialUserList(reqVO.getType(), unionId, userTypeEnum);
  203 + List<SysSocialUserDO> socialUsers = socialService.getAllSocialUserList(reqVO.getType(), unionId, USER_TYPE_ENUM);
205 204 if (CollUtil.isEmpty(socialUsers)) {
206 205 throw exception(AUTH_THIRD_LOGIN_NOT_BIND);
207 206 }
... ... @@ -218,7 +217,7 @@ public class SysAuthServiceImpl implements SysAuthService {
218 217 loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表
219 218  
220 219 // 绑定社交用户(更新)
221   - socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum);
  220 + socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, USER_TYPE_ENUM);
222 221  
223 222 // 缓存登录用户到 Redis 中,返回 sessionId 编号
224 223 return userSessionCoreService.createUserSession(loginUser, userIp, userAgent);
... ... @@ -235,7 +234,7 @@ public class SysAuthServiceImpl implements SysAuthService {
235 234 loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表
236 235  
237 236 // 绑定社交用户(新增)
238   - socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum);
  237 + socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, USER_TYPE_ENUM);
239 238  
240 239 // 缓存登录用户到 Redis 中,返回 sessionId 编号
241 240 return userSessionCoreService.createUserSession(loginUser, userIp, userAgent);
... ... @@ -248,7 +247,7 @@ public class SysAuthServiceImpl implements SysAuthService {
248 247 Assert.notNull(authUser, "授权用户不为空");
249 248  
250 249 // 绑定社交用户(新增)
251   - socialService.bindSocialUser(userId, reqVO.getType(), authUser, userTypeEnum);
  250 + socialService.bindSocialUser(userId, reqVO.getType(), authUser, USER_TYPE_ENUM);
252 251 }
253 252  
254 253 @Override
... ... @@ -269,7 +268,7 @@ public class SysAuthServiceImpl implements SysAuthService {
269 268 reqDTO.setLogType(SysLoginLogTypeEnum.LOGOUT_SELF.getType());
270 269 reqDTO.setTraceId(TracerUtils.getTraceId());
271 270 reqDTO.setUserId(userId);
272   - reqDTO.setUserType(userTypeEnum.getValue());
  271 + reqDTO.setUserType(USER_TYPE_ENUM.getValue());
273 272 reqDTO.setUsername(username);
274 273 reqDTO.setUserAgent(ServletUtils.getUserAgent());
275 274 reqDTO.setUserIp(ServletUtils.getClientIP());
... ...
yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysAuthServiceImplTest.java
... ... @@ -11,7 +11,7 @@ import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService;
11 11 import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO;
12 12 import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService;
13 13 import cn.iocoder.yudao.coreservice.modules.system.service.logger.SysLoginLogCoreService;
14   -import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService;
  14 +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialCoreService;
15 15 import cn.iocoder.yudao.coreservice.modules.system.service.user.SysUserCoreService;
16 16 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
17 17 import cn.iocoder.yudao.framework.security.core.LoginUser;
... ... @@ -66,7 +66,7 @@ public class SysAuthServiceImplTest extends BaseDbUnitTest {
66 66 @MockBean
67 67 private SysUserSessionCoreService userSessionCoreService;
68 68 @MockBean
69   - private SysSocialService socialService;
  69 + private SysSocialCoreService socialService;
70 70  
71 71 @Test
72 72 public void testLoadUserByUsername_success() {
... ...
yudao-core-service/pom.xml
... ... @@ -36,6 +36,10 @@
36 36 <groupId>cn.iocoder.boot</groupId>
37 37 <artifactId>yudao-spring-boot-starter-biz-pay</artifactId>
38 38 </dependency>
  39 + <dependency>
  40 + <groupId>cn.iocoder.boot</groupId>
  41 + <artifactId>yudao-spring-boot-starter-biz-social</artifactId>
  42 + </dependency>
39 43  
40 44 <!-- Web 相关 -->
41 45 <dependency>
... ... @@ -96,12 +100,6 @@
96 100 <artifactId>guava</artifactId>
97 101 </dependency>
98 102  
99   - <!-- 三方云服务相关 -->
100   - <dependency>
101   - <groupId>com.xkcoding.justauth</groupId>
102   - <artifactId>justauth-spring-boot-starter</artifactId>
103   - </dependency>
104   -
105 103 </dependencies>
106 104  
107 105 </project>
... ...
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/social/SysSocialUserMapper.java renamed to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/mysql/social/SysSocialUserCoreMapper.java
... ... @@ -8,9 +8,8 @@ import org.apache.ibatis.annotations.Mapper;
8 8 import java.util.Collection;
9 9 import java.util.List;
10 10  
11   -// TODO @timfruit:SysSocialUserCoreMapper 改名,方便区分
12 11 @Mapper
13   -public interface SysSocialUserMapper extends BaseMapperX<SysSocialUserDO> {
  12 +public interface SysSocialUserCoreMapper extends BaseMapperX<SysSocialUserDO> {
14 13  
15 14 default List<SysSocialUserDO> selectListByTypeAndUnionId(Integer userType, Collection<Integer> types, String unionId) {
16 15 return selectList(new QueryWrapper<SysSocialUserDO>().eq("user_type", userType)
... ...
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/dal/redis/social/SysSocialAuthUserRedisDAO.java
... ... @@ -10,9 +10,8 @@ import javax.annotation.Resource;
10 10  
11 11 import static cn.iocoder.yudao.coreservice.modules.system.dal.redis.SysRedisKeyCoreConstants.SOCIAL_AUTH_USER;
12 12  
13   -// TODO @timfruit,这里的 AuthUser 还是保留全路径,主要想体现出来,不是自己定义的
14 13 /**
15   - * 社交 {@link AuthUser} 的 RedisDAO
  14 + * 社交 {@link me.zhyd.oauth.model.AuthUser} 的 RedisDAO
16 15 *
17 16 * @author 芋道源码
18 17 */
... ...
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/SysSocialService.java renamed to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/SysSocialCoreService.java
... ... @@ -15,8 +15,7 @@ import java.util.List;
15 15 *
16 16 * @author 芋道源码
17 17 */
18   -// TODO @timfruit:SysSocialCoreService 改名,方便区分
19   -public interface SysSocialService {
  18 +public interface SysSocialCoreService {
20 19  
21 20 /**
22 21 * 获得社交平台的授权 URL
... ... @@ -50,6 +49,7 @@ public interface SysSocialService {
50 49 * @param type 社交平台的类型 {@link SysSocialTypeEnum}
51 50 * @param unionId 社交平台的 unionId
52 51 * @return 社交用户列表
  52 + * @param userTypeEnum 全局用户类型
53 53 */
54 54 List<SysSocialUserDO> getAllSocialUserList(Integer type, String unionId, UserTypeEnum userTypeEnum);
55 55  
... ... @@ -58,6 +58,7 @@ public interface SysSocialService {
58 58 *
59 59 * @param userId 用户编号
60 60 * @return 社交用户列表
  61 + * @param userTypeEnum 全局用户类型
61 62 */
62 63 List<SysSocialUserDO> getSocialUserList(Long userId, UserTypeEnum userTypeEnum);
63 64  
... ... @@ -67,6 +68,7 @@ public interface SysSocialService {
67 68 * @param userId 用户编号
68 69 * @param type 社交平台的类型 {@link SysSocialTypeEnum}
69 70 * @param authUser 授权用户
  71 + * @param userTypeEnum 全局用户类型
70 72 */
71 73 void bindSocialUser(Long userId, Integer type, AuthUser authUser, UserTypeEnum userTypeEnum);
72 74  
... ... @@ -76,8 +78,8 @@ public interface SysSocialService {
76 78 * @param userId 用户编号
77 79 * @param type 社交平台的类型 {@link SysSocialTypeEnum}
78 80 * @param unionId 社交平台的 unionId
  81 + * @param userTypeEnum 全局用户类型
79 82 */
80   - void unbindSocialUser(Long userId, Integer type, String unionId,UserTypeEnum userTypeEnum);
81   - // TODO @timfruit:逗号后面要有空格;缺少了 @userTypeEnum 的注释,都补充下哈。
  83 + void unbindSocialUser(Long userId, Integer type, String unionId, UserTypeEnum userTypeEnum);
82 84  
83 85 }
... ...
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/impl/SysSocialServiceImpl.java renamed to yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/service/social/impl/SysSocialCoreServiceImpl.java
... ... @@ -2,15 +2,15 @@ package cn.iocoder.yudao.coreservice.modules.system.service.social.impl;
2 2  
3 3 import cn.hutool.core.collection.CollUtil;
4 4 import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO;
5   -import cn.iocoder.yudao.coreservice.modules.system.dal.mysql.social.SysSocialUserMapper;
  5 +import cn.iocoder.yudao.coreservice.modules.system.dal.mysql.social.SysSocialUserCoreMapper;
6 6 import cn.iocoder.yudao.coreservice.modules.system.dal.redis.social.SysSocialAuthUserRedisDAO;
7 7 import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum;
8   -import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService;
  8 +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialCoreService;
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;
  12 +import cn.iocoder.yudao.framework.social.core.YudaoAuthRequestFactory;
12 13 import com.google.common.annotations.VisibleForTesting;
13   -import com.xkcoding.justauth.AuthRequestFactory;
14 14 import lombok.extern.slf4j.Slf4j;
15 15 import me.zhyd.oauth.model.AuthCallback;
16 16 import me.zhyd.oauth.model.AuthResponse;
... ... @@ -38,21 +38,21 @@ import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString
38 38 @Service
39 39 @Validated
40 40 @Slf4j
41   -public class SysSocialServiceImpl implements SysSocialService {
  41 +public class SysSocialCoreServiceImpl implements SysSocialCoreService {
42 42  
43 43 @Resource
44   - private AuthRequestFactory authRequestFactory;
  44 + private YudaoAuthRequestFactory yudaoAuthRequestFactory;
45 45  
46 46 @Resource
47 47 private SysSocialAuthUserRedisDAO authSocialUserRedisDAO;
48 48  
49 49 @Resource
50   - private SysSocialUserMapper socialUserMapper;
  50 + private SysSocialUserCoreMapper socialUserMapper;
51 51  
52 52 @Override
53 53 public String getAuthorizeUrl(Integer type, String redirectUri) {
54 54 // 获得对应的 AuthRequest 实现
55   - AuthRequest authRequest = authRequestFactory.get(SysSocialTypeEnum.valueOfType(type).getSource());
  55 + AuthRequest authRequest = yudaoAuthRequestFactory.get(SysSocialTypeEnum.valueOfType(type).getSource());
56 56 // 生成跳转地址
57 57 String authorizeUri = authRequest.authorize(AuthStateUtils.createState());
58 58 return HttpUtils.replaceUrlQuery(authorizeUri, "redirect_uri", redirectUri);
... ... @@ -161,7 +161,7 @@ public class SysSocialServiceImpl implements SysSocialService {
161 161 * @return 授权的用户
162 162 */
163 163 private AuthUser getAuthUser0(Integer type, AuthCallback authCallback) {
164   - AuthRequest authRequest = authRequestFactory.get(SysSocialTypeEnum.valueOfType(type).getSource());
  164 + AuthRequest authRequest = yudaoAuthRequestFactory.get(SysSocialTypeEnum.valueOfType(type).getSource());
165 165 AuthResponse<?> authResponse = authRequest.login(authCallback);
166 166 log.info("[getAuthUser0][请求社交平台 type({}) request({}) response({})]", type, toJsonString(authCallback),
167 167 toJsonString(authResponse));
... ...
yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialServiceTest.java renamed to yudao-core-service/src/test/java/cn/iocoder/yudao/coreservice/modules/system/service/social/SysSocialCoreServiceTest.java
1   -package cn.iocoder.yudao.adminserver.modules.system.service.social;
  1 +package cn.iocoder.yudao.coreservice.modules.system.service.social;
2 2  
3   -import cn.iocoder.yudao.adminserver.BaseDbAndRedisUnitTest;
  3 +import cn.iocoder.yudao.coreservice.BaseDbAndRedisUnitTest;
4 4 import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO;
5   -import cn.iocoder.yudao.coreservice.modules.system.dal.mysql.social.SysSocialUserMapper;
  5 +import cn.iocoder.yudao.coreservice.modules.system.dal.mysql.social.SysSocialUserCoreMapper;
6 6 import cn.iocoder.yudao.coreservice.modules.system.dal.redis.social.SysSocialAuthUserRedisDAO;
7 7 import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum;
8   -import cn.iocoder.yudao.coreservice.modules.system.service.social.impl.SysSocialServiceImpl;
  8 +import cn.iocoder.yudao.coreservice.modules.system.service.social.impl.SysSocialCoreServiceImpl;
9 9 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
10 10 import com.xkcoding.justauth.AuthRequestFactory;
11 11 import me.zhyd.oauth.model.AuthUser;
... ... @@ -23,20 +23,19 @@ import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId
23 23 import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
24 24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 25  
26   -// TODO @timfruit:这个单元测试,挪到 yudao-core-service
27 26 /**
28   - * {@link SysSocialServiceImpl} 的单元测试类
  27 + * {@link SysSocialCoreServiceImpl} 的单元测试类
29 28 *
30 29 * @author 芋道源码
31 30 */
32   -@Import({SysSocialServiceImpl.class, SysSocialAuthUserRedisDAO.class})
33   -public class SysSocialServiceTest extends BaseDbAndRedisUnitTest {
  31 +@Import({SysSocialCoreServiceImpl.class, SysSocialAuthUserRedisDAO.class})
  32 +public class SysSocialCoreServiceTest extends BaseDbAndRedisUnitTest {
34 33  
35 34 @Resource
36   - private SysSocialServiceImpl socialService;
  35 + private SysSocialCoreServiceImpl socialService;
37 36  
38 37 @Resource
39   - private SysSocialUserMapper socialUserMapper;
  38 + private SysSocialUserCoreMapper socialUserMapper;
40 39  
41 40 @MockBean
42 41 private AuthRequestFactory authRequestFactory;
... ...
yudao-dependencies/pom.xml
... ... @@ -351,6 +351,12 @@
351 351 </dependency>
352 352  
353 353 <dependency>
  354 + <groupId>cn.iocoder.boot</groupId>
  355 + <artifactId>yudao-spring-boot-starter-biz-social</artifactId>
  356 + <version>${revision}</version>
  357 + </dependency>
  358 +
  359 + <dependency>
354 360 <groupId>org.projectlombok</groupId>
355 361 <artifactId>lombok</artifactId>
356 362 <version>${lombok.version}</version>
... ...
yudao-framework/pom.xml
... ... @@ -31,6 +31,7 @@
31 31 <module>yudao-spring-boot-starter-biz-pay</module>
32 32 <module>yudao-spring-boot-starter-biz-weixin</module>
33 33 <module>yudao-spring-boot-starter-extension</module>
  34 + <module>yudao-spring-boot-starter-biz-social</module>
34 35 </modules>
35 36  
36 37 <artifactId>yudao-framework</artifactId>
... ...
yudao-framework/yudao-spring-boot-starter-biz-social/pom.xml 0 → 100644
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project xmlns="http://maven.apache.org/POM/4.0.0"
  3 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5 + <parent>
  6 + <groupId>cn.iocoder.boot</groupId>
  7 + <artifactId>yudao-framework</artifactId>
  8 + <version>${revision}</version>
  9 + </parent>
  10 + <packaging>jar</packaging>
  11 + <modelVersion>4.0.0</modelVersion>
  12 +
  13 + <artifactId>yudao-spring-boot-starter-biz-social</artifactId>
  14 + <name>${artifactId}</name>
  15 +
  16 + <dependencies>
  17 + <dependency>
  18 + <groupId>cn.iocoder.boot</groupId>
  19 + <artifactId>yudao-common</artifactId>
  20 + </dependency>
  21 + <!-- Spring 核心 -->
  22 + <dependency>
  23 + <groupId>org.springframework.boot</groupId>
  24 + <artifactId>spring-boot-starter-aop</artifactId>
  25 + </dependency>
  26 + <!-- Web 相关 -->
  27 + <dependency>
  28 + <groupId>cn.iocoder.boot</groupId>
  29 + <artifactId>yudao-spring-boot-starter-web</artifactId>
  30 + </dependency>
  31 + <!-- spring boot 配置所需依赖 -->
  32 + <dependency>
  33 + <groupId>org.springframework.boot</groupId>
  34 + <artifactId>spring-boot-configuration-processor</artifactId>
  35 + <optional>true</optional>
  36 + </dependency>
  37 + <!-- 三方云服务相关 -->
  38 + <dependency>
  39 + <groupId>com.xkcoding.justauth</groupId>
  40 + <artifactId>justauth-spring-boot-starter</artifactId>
  41 + </dependency>
  42 + <dependency>
  43 + <groupId>cn.iocoder.boot</groupId>
  44 + <artifactId>yudao-spring-boot-starter-redis</artifactId>
  45 + </dependency>
  46 +
  47 + </dependencies>
  48 +
  49 +
  50 +</project>
0 51 \ No newline at end of file
... ...
yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/config/YudaoSocialAutoConfiguration.java 0 → 100644
  1 +package cn.iocoder.yudao.framework.social.config;
  2 +
  3 +import cn.iocoder.yudao.framework.social.core.YudaoAuthRequestFactory;
  4 +import com.xkcoding.justauth.autoconfigure.JustAuthProperties;
  5 +import lombok.extern.slf4j.Slf4j;
  6 +import me.zhyd.oauth.cache.AuthStateCache;
  7 +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  8 +import org.springframework.boot.context.properties.EnableConfigurationProperties;
  9 +import org.springframework.context.annotation.Bean;
  10 +import org.springframework.context.annotation.Configuration;
  11 +
  12 +/**
  13 + * 社交自动装配类
  14 + *
  15 + * @author timfruit
  16 + * @date 2021-10-30
  17 + */
  18 +@Slf4j
  19 +@Configuration
  20 +@EnableConfigurationProperties(JustAuthProperties.class)
  21 +public class YudaoSocialAutoConfiguration {
  22 +
  23 + @Bean
  24 + @ConditionalOnProperty(prefix = "justauth", value = "enabled", havingValue = "true", matchIfMissing = true)
  25 + public YudaoAuthRequestFactory yudaoAuthRequestFactory(JustAuthProperties properties, AuthStateCache authStateCache) {
  26 + return new YudaoAuthRequestFactory(properties, authStateCache);
  27 + }
  28 +
  29 +}
... ...
yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/YudaoAuthRequestFactory.java 0 → 100644
  1 +package cn.iocoder.yudao.framework.social.core;
  2 +
  3 +import cn.hutool.core.util.EnumUtil;
  4 +import cn.hutool.core.util.StrUtil;
  5 +import cn.iocoder.yudao.framework.social.core.enums.AuthExtendSource;
  6 +import cn.iocoder.yudao.framework.social.core.request.AuthWeChatMiniProgramRequest;
  7 +import com.xkcoding.http.config.HttpConfig;
  8 +import com.xkcoding.justauth.AuthRequestFactory;
  9 +import com.xkcoding.justauth.autoconfigure.JustAuthProperties;
  10 +import me.zhyd.oauth.cache.AuthStateCache;
  11 +import me.zhyd.oauth.config.AuthConfig;
  12 +import me.zhyd.oauth.config.AuthSource;
  13 +import me.zhyd.oauth.request.AuthRequest;
  14 +import org.springframework.util.CollectionUtils;
  15 +
  16 +import java.net.InetSocketAddress;
  17 +import java.net.Proxy;
  18 +import java.util.Map;
  19 +
  20 +/**
  21 + * 第三方授权拓展request工厂类
  22 + *
  23 + * @author timfruit
  24 + * @date 2021-10-31
  25 + */
  26 +public class YudaoAuthRequestFactory extends AuthRequestFactory {
  27 + protected JustAuthProperties properties;
  28 + protected AuthStateCache authStateCache;
  29 +
  30 + public YudaoAuthRequestFactory(JustAuthProperties properties, AuthStateCache authStateCache) {
  31 + super(properties, authStateCache);
  32 + this.properties = properties;
  33 + this.authStateCache = authStateCache;
  34 + }
  35 +
  36 + /**
  37 + * 返回AuthRequest对象
  38 + *
  39 + * @param source {@link AuthSource}
  40 + * @return {@link AuthRequest}
  41 + */
  42 + public AuthRequest get(String source) {
  43 + //先尝试获取自定义扩展的
  44 + AuthRequest authRequest = getExtendRequest(source);
  45 +
  46 + if (authRequest == null) {
  47 + authRequest = super.get(source);
  48 + }
  49 +
  50 + return authRequest;
  51 + }
  52 +
  53 +
  54 + protected AuthRequest getExtendRequest(String source) {
  55 + AuthExtendSource authExtendSource;
  56 +
  57 + try {
  58 + authExtendSource = EnumUtil.fromString(AuthExtendSource.class, source.toUpperCase());
  59 + } catch (IllegalArgumentException e) {
  60 + // 无自定义匹配
  61 + return null;
  62 + }
  63 +
  64 + // 拓展配置和默认配置齐平,properties放在一起
  65 + AuthConfig config = properties.getType().get(authExtendSource.name());
  66 + // 找不到对应关系,直接返回空
  67 + if (config == null) {
  68 + return null;
  69 + }
  70 +
  71 + // 配置 http config
  72 + configureHttpConfig(authExtendSource.name(), config, properties.getHttpConfig());
  73 +
  74 + switch (authExtendSource) {
  75 + case WECHAT_MINI_PROGRAM:
  76 + return new AuthWeChatMiniProgramRequest(config, authStateCache);
  77 + default:
  78 + return null;
  79 + }
  80 + }
  81 +
  82 + /**
  83 + * 配置 http 相关的配置
  84 + *
  85 + * @param authSource {@link AuthSource}
  86 + * @param authConfig {@link AuthConfig}
  87 + */
  88 + protected void configureHttpConfig(String authSource, AuthConfig authConfig, JustAuthProperties.JustAuthHttpConfig httpConfig) {
  89 + if (null == httpConfig) {
  90 + return;
  91 + }
  92 + Map<String, JustAuthProperties.JustAuthProxyConfig> proxyConfigMap = httpConfig.getProxy();
  93 + if (CollectionUtils.isEmpty(proxyConfigMap)) {
  94 + return;
  95 + }
  96 + JustAuthProperties.JustAuthProxyConfig proxyConfig = proxyConfigMap.get(authSource);
  97 +
  98 + if (null == proxyConfig) {
  99 + return;
  100 + }
  101 +
  102 + authConfig.setHttpConfig(HttpConfig.builder()
  103 + .timeout(httpConfig.getTimeout())
  104 + .proxy(new Proxy(Proxy.Type.valueOf(proxyConfig.getType()), new InetSocketAddress(proxyConfig.getHostname(), proxyConfig.getPort())))
  105 + .build());
  106 + }
  107 +
  108 +}
... ...
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthExtendSource.java renamed to yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/enums/AuthExtendSource.java
1   -package cn.iocoder.yudao.coreservice.modules.system.compent.justauth;
  1 +package cn.iocoder.yudao.framework.social.core.enums;
2 2  
3 3 import me.zhyd.oauth.config.AuthSource;
4 4  
... ...
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthExtendToken.java renamed to yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/model/AuthExtendToken.java
1   -package cn.iocoder.yudao.coreservice.modules.system.compent.justauth;
  1 +package cn.iocoder.yudao.framework.social.core.model;
2 2  
3 3 import lombok.*;
4 4 import me.zhyd.oauth.model.AuthToken;
5 5  
6 6 /**
7   - * TODO @timfruit:类注释
  7 + * 授权所需的token 拓展类
  8 + *
8 9 * @author timfruit
9 10 * @date 2021-10-29
10 11 */
... ...
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/system/compent/justauth/AuthWeChatMiniProgramRequest.java renamed to yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/request/AuthWeChatMiniProgramRequest.java
1   -package cn.iocoder.yudao.coreservice.modules.system.compent.justauth;
  1 +package cn.iocoder.yudao.framework.social.core.request;
2 2  
3   -import com.alibaba.fastjson.JSONObject;
  3 +import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
  4 +import cn.iocoder.yudao.framework.social.core.enums.AuthExtendSource;
  5 +import cn.iocoder.yudao.framework.social.core.model.AuthExtendToken;
  6 +import lombok.Data;
4 7 import me.zhyd.oauth.cache.AuthStateCache;
5 8 import me.zhyd.oauth.config.AuthConfig;
6 9 import me.zhyd.oauth.exception.AuthException;
... ... @@ -11,9 +14,6 @@ import me.zhyd.oauth.request.AuthDefaultRequest;
11 14 import me.zhyd.oauth.utils.HttpUtils;
12 15 import me.zhyd.oauth.utils.UrlBuilder;
13 16  
14   -// TODO @timfruit:新建一个 yudao-spring-boot-starter-biz-social 包,把这个拓展拿进去哈。另外,可以思考下。
15   -// 1. application-local.yaml 的配置里,justauth.extend.enum-class 能否不配置,而是自动配置好
16   -// 2. application-local.yaml 的配置里,justauth.extend.extend.config.WECHAT_MINI_PROGRAM 有办法和 justauth.type.WECHAT_MP 持平
17 17 /**
18 18 * 微信小程序登陆
19 19 *
... ... @@ -34,14 +34,14 @@ public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest {
34 34 protected AuthToken getAccessToken(AuthCallback authCallback) {
35 35 // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
36 36 String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl(authCallback.getCode()));
37   - JSONObject accessTokenObject = JSONObject.parseObject(response); // TODO @timfruit:使用 JsonUtils,项目尽量避免直接使用某个 json 库
  37 + CodeSessionResponse accessTokenObject = JsonUtils.parseObject(response, CodeSessionResponse.class);
38 38  
39 39 this.checkResponse(accessTokenObject);
40 40  
41 41 AuthExtendToken token = new AuthExtendToken();
42   - token.setMiniSessionKey(accessTokenObject.getString("session_key"));
43   - token.setOpenId(accessTokenObject.getString("openid"));
44   - token.setUnionId(accessTokenObject.getString("unionid"));
  42 + token.setMiniSessionKey(accessTokenObject.session_key);
  43 + token.setOpenId(accessTokenObject.openid);
  44 + token.setUnionId(accessTokenObject.unionid);
45 45 return token;
46 46 }
47 47  
... ... @@ -64,10 +64,9 @@ public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest {
64 64 *
65 65 * @param object 请求响应内容
66 66 */
67   - private void checkResponse(JSONObject object) {
68   - int code = object.getIntValue("errcode");
69   - if(code != 0){ // TODO @timfruit:if (code != 0) { ,注意空格
70   - throw new AuthException(object.getIntValue("errcode"), object.getString("errmsg"));
  67 + private void checkResponse(CodeSessionResponse object) {
  68 + if (object.errcode != 0) {
  69 + throw new AuthException(object.errcode, object.errmsg);
71 70 }
72 71 }
73 72  
... ... @@ -87,4 +86,13 @@ public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest {
87 86 .build();
88 87 }
89 88  
  89 + @Data
  90 + private static class CodeSessionResponse {
  91 + private int errcode;
  92 + private String errmsg;
  93 + private String session_key;
  94 + private String openid;
  95 + private String unionid;
  96 + }
  97 +
90 98 }
... ...
yudao-framework/yudao-spring-boot-starter-biz-social/src/main/resources/META-INF/spring.factories 0 → 100644
  1 +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  2 + cn.iocoder.yudao.framework.social.config.YudaoSocialAutoConfiguration
... ...
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;
  3 +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialCoreService;
4 4 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
5 5 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
6 6 import cn.iocoder.yudao.userserver.modules.system.controller.auth.vo.*;
... ... @@ -36,7 +36,7 @@ public class SysAuthController {
36 36 @Resource
37 37 private SysSmsCodeService smsCodeService;
38 38 @Resource
39   - private SysSocialService socialService;
  39 + private SysSocialCoreService socialService;
40 40  
41 41  
42 42 @PostMapping("/login")
... ...
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java
... ... @@ -9,7 +9,7 @@ import cn.iocoder.yudao.coreservice.modules.system.enums.logger.SysLoginResultEn
9 9 import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService;
10 10 import cn.iocoder.yudao.coreservice.modules.system.service.logger.SysLoginLogCoreService;
11 11 import cn.iocoder.yudao.coreservice.modules.system.service.logger.dto.SysLoginLogCreateReqDTO;
12   -import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService;
  12 +import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialCoreService;
13 13 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
14 14 import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
15 15 import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
... ... @@ -64,8 +64,8 @@ public class SysAuthServiceImpl implements SysAuthService {
64 64 @Resource
65 65 private SysUserSessionCoreService userSessionCoreService;
66 66 @Resource
67   - private SysSocialService socialService;
68   - private static final UserTypeEnum userTypeEnum = UserTypeEnum.MEMBER;
  67 + private SysSocialCoreService socialService;
  68 + private static final UserTypeEnum USER_TYPE_ENUM = UserTypeEnum.MEMBER;
69 69  
70 70 @Override
71 71 public UserDetails loadUserByUsername(String mobile) throws UsernameNotFoundException {
... ... @@ -114,7 +114,7 @@ public class SysAuthServiceImpl implements SysAuthService {
114 114  
115 115 // 如果未绑定 SysSocialUserDO 用户,则无法自动登录,进行报错
116 116 String unionId = socialService.getAuthUserUnionId(authUser);
117   - List<SysSocialUserDO> socialUsers = socialService.getAllSocialUserList(reqVO.getType(), unionId, userTypeEnum);
  117 + List<SysSocialUserDO> socialUsers = socialService.getAllSocialUserList(reqVO.getType(), unionId, USER_TYPE_ENUM);
118 118 if (CollUtil.isEmpty(socialUsers)) {
119 119 throw exception(AUTH_THIRD_LOGIN_NOT_BIND);
120 120 }
... ... @@ -130,7 +130,7 @@ public class SysAuthServiceImpl implements SysAuthService {
130 130 LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user);
131 131  
132 132 // 绑定社交用户(更新)
133   - socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum);
  133 + socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, USER_TYPE_ENUM);
134 134  
135 135 // 缓存登录用户到 Redis 中,返回 sessionId 编号
136 136 return userSessionCoreService.createUserSession(loginUser, userIp, userAgent);
... ... @@ -147,7 +147,7 @@ public class SysAuthServiceImpl implements SysAuthService {
147 147 // loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表
148 148  
149 149 // 绑定社交用户(新增)
150   - socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum);
  150 + socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, USER_TYPE_ENUM);
151 151  
152 152 // 缓存登录用户到 Redis 中,返回 sessionId 编号
153 153 return userSessionCoreService.createUserSession(loginUser, userIp, userAgent);
... ... @@ -160,7 +160,7 @@ public class SysAuthServiceImpl implements SysAuthService {
160 160 org.springframework.util.Assert.notNull(authUser, "授权用户不为空");
161 161  
162 162 // 绑定社交用户(新增)
163   - socialService.bindSocialUser(userId, reqVO.getType(), authUser, userTypeEnum);
  163 + socialService.bindSocialUser(userId, reqVO.getType(), authUser, USER_TYPE_ENUM);
164 164 }
165 165  
166 166 private LoginUser login0(String username, String password) {
... ... @@ -271,7 +271,7 @@ public class SysAuthServiceImpl implements SysAuthService {
271 271 reqDTO.setLogType(SysLoginLogTypeEnum.LOGOUT_SELF.getType());
272 272 reqDTO.setTraceId(TracerUtils.getTraceId());
273 273 reqDTO.setUserId(userId);
274   - reqDTO.setUserType(userTypeEnum.getValue());
  274 + reqDTO.setUserType(USER_TYPE_ENUM.getValue());
275 275 reqDTO.setUsername(username);
276 276 reqDTO.setUserAgent(ServletUtils.getUserAgent());
277 277 reqDTO.setUserIp(ServletUtils.getClientIP());
... ...
yudao-user-server/src/main/resources/application-dev.yaml
... ... @@ -145,24 +145,16 @@ yudao:
145 145  
146 146 justauth:
147 147 enabled: true
148   - type: # TODO @timfruit:GITEE、DINGTALK、WECHAT_ENTERPRISE 这个几个,对于用户端是不需要的哈,可以删除噢
149   - GITEE: # Gitee
150   - client-id: ee61f0374a4c6c404a8717094caa7a410d76950e45ff60348015830c519ba5c1
151   - client-secret: 7c044a5671be3b051414db0cf2cec6ad702dd298d2416ba24ceaf608e6fa26f9
152   - ignore-check-redirect-uri: true
153   - DINGTALK: # 钉钉
154   - client-id: dingvrnreaje3yqvzhxg
155   - client-secret: i8E6iZyDvZj51JIb0tYsYfVQYOks9Cq1lgryEjFRqC79P3iJcrxEwT6Qk2QvLrLI
156   - ignore-check-redirect-uri: true
157   - WECHAT_ENTERPRISE: # 企业微信
158   - client-id: wwd411c69a39ad2e54
159   - client-secret: 1wTb7hYxnpT2TUbIeHGXGo7T0odav1ic10mLdyyATOw
160   - agent-id: 1000004
161   - ignore-check-redirect-uri: true
162   - WECHAT_MP: # 微信公众平台 - H5 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
  148 + type:
  149 + 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
163 150 client-id: wxa5a05b85ac361f96
164 151 client-secret: 247073c7cebb67f27f0e769195c2a57e
165 152 ignore-check-redirect-uri: true
  153 + 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
  154 + client-id: wx44d047d87e6284d8
  155 + client-secret: 21c3b7a8a51ee1b8f5cf875848ed4466
  156 + ignore-check-redirect-uri: true
  157 + ignore-check-state: true
166 158 cache:
167 159 type: REDIS
168 160 prefix: 'social_auth_state:' # 缓存前缀,目前只对 Redis 缓存生效,默认 JUSTAUTH::STATE::
... ...
yudao-user-server/src/main/resources/application-local.yaml
... ... @@ -165,17 +165,13 @@ justauth:
165 165 # client-id: wx5b23ba7a5589ecbb # TODO 芋艿:自己的测试,后续可以删除
166 166 # client-secret: 2a7b3b20c537e52e74afd395eb85f61f
167 167 ignore-check-redirect-uri: true
168   - extend:
169   - enum-class: cn.iocoder.yudao.coreservice.modules.system.compent.justauth.AuthExtendSource
170   - config:
171   - 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
172   - request-class: cn.iocoder.yudao.coreservice.modules.system.compent.justauth.AuthWeChatMiniProgramRequest
173   - client-id: wx44d047d87e6284d8
174   - client-secret: 21c3b7a8a51ee1b8f5cf875848ed4466
175   -# client-id: wx63c280fe3248a3e7 # TODO 芋艿:自己的测试,后续可以删除
176   -# client-secret: 6f270509224a7ae1296bbf1c8cb97aed
177   - ignore-check-redirect-uri: true
178   - ignore-check-state: true
  168 + 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
  169 + client-id: wx44d047d87e6284d8
  170 + client-secret: 21c3b7a8a51ee1b8f5cf875848ed4466
  171 + # client-id: wx63c280fe3248a3e7 # TODO 芋艿:自己的测试,后续可以删除
  172 + # client-secret: 6f270509224a7ae1296bbf1c8cb97aed
  173 + ignore-check-redirect-uri: true
  174 + ignore-check-state: true
179 175 cache:
180 176 type: REDIS
181 177 prefix: 'social_auth_state:' # 缓存前缀,目前只对 Redis 缓存生效,默认 JUSTAUTH::STATE::
... ...