重构社交登录的时候,增加独立的社交绑定表

plp
YunaiV 4 years ago
parent 6e40469735
commit 705a5ff645

@ -22,44 +22,47 @@ public enum SocialTypeEnum implements IntArrayValuable {
* Gitee * Gitee
* https://gitee.com/api/v5/oauth_doc#/ * https://gitee.com/api/v5/oauth_doc#/
*/ */
GITEE(10, "GITEE"), GITEE(10, 1, "GITEE"),
/** /**
* *
* https://developers.dingtalk.com/document/app/obtain-identity-credentials * https://developers.dingtalk.com/document/app/obtain-identity-credentials
*/ */
DINGTALK(20, "DINGTALK"), DINGTALK(20, 2, "DINGTALK"),
/** /**
* *
* https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html * https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html
*/ */
WECHAT_ENTERPRISE(30, "WECHAT_ENTERPRISE"), WECHAT_ENTERPRISE(30, 3, "WECHAT_ENTERPRISE"),
/** /**
* - H5 * - H5
* https://www.cnblogs.com/juewuzhe/p/11905461.html * https://www.cnblogs.com/juewuzhe/p/11905461.html
*/ */
WECHAT_MP(31, "WECHAT_MP"), WECHAT_MP(31, 3, "WECHAT_MP"),
/** /**
* - PC * - PC
* https://justauth.wiki/guide/oauth/wechat_open/#_2-申请开发者资质认证 * https://justauth.wiki/guide/oauth/wechat_open/#_2-申请开发者资质认证
*/ */
WECHAT_OPEN(32, "WECHAT_OPEN"), WECHAT_OPEN(32, 3, "WECHAT_OPEN"),
/** /**
* *
* https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
*/ */
WECHAT_MINI_PROGRAM(33, "WECHAT_MINI_PROGRAM"), WECHAT_MINI_PROGRAM(33, 3, "WECHAT_MINI_PROGRAM"),
; ;
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(SocialTypeEnum::getType).toArray(); public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(SocialTypeEnum::getType).toArray();
public static final List<Integer> WECHAT_ALL = ListUtil.toList(WECHAT_ENTERPRISE.type, WECHAT_MP.type, WECHAT_OPEN.type,
WECHAT_MINI_PROGRAM.type);
/** /**
* *
*/ */
private final Integer type; private final Integer type;
/**
*
*
*
*/
private final Integer platform;
/** /**
* *
*/ */
@ -74,11 +77,4 @@ public enum SocialTypeEnum implements IntArrayValuable {
return ArrayUtil.firstMatch(o -> o.getType().equals(type), values()); return ArrayUtil.firstMatch(o -> o.getType().equals(type), values());
} }
public static List<Integer> getRelationTypes(Integer type) {
if (WECHAT_ALL.contains(type)) {
return WECHAT_ALL;
}
return ListUtil.toList(type);
}
} }

@ -38,7 +38,7 @@ public class SocialUserApiImpl implements SocialUserApi {
@Override @Override
public void checkSocialUser(Integer type, String code, String state) { public void checkSocialUser(Integer type, String code, String state) {
socialUserService.checkSocialUser(type, code, state); socialUserService.authSocialUser(type, code, state);
} }
@Override @Override

@ -0,0 +1,45 @@
package cn.iocoder.yudao.module.system.dal.dataobject.social;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
/**
*
* {@link SocialUserDO} UserDO
*
* @author
*/
@TableName(value = "system_social_user_bind", autoResultMap = true)
@Data
@EqualsAndHashCode(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SocialUserBindDO extends BaseDO {
/**
*
*/
private Long userId;
/**
*
*
* {@link UserTypeEnum}
*/
private Integer userType;
/**
*
*
* {@link SocialTypeEnum#getPlatform()}
*/
private Integer platform;
/**
*
*/
private String unionId;
}

@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.system.dal.dataobject.social;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*; import lombok.*;
@ -26,21 +27,10 @@ public class SocialUserDO extends BaseDO {
*/ */
@TableId @TableId
private Long id; private Long id;
/**
*
*/
private Long userId;
/**
*
*
* {@link UserTypeEnum}
*/
private Integer userType;
/** /**
* *
* *
* {@link UserTypeEnum} * {@link SocialTypeEnum}
*/ */
private Integer type; private Integer type;
@ -77,6 +67,15 @@ public class SocialUserDO extends BaseDO {
*/ */
private String rawUserInfo; private String rawUserInfo;
/**
* code
*/
private String code;
/**
* state
*/
private String state;
} }

@ -0,0 +1,28 @@
package cn.iocoder.yudao.module.system.dal.mysql.social;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserBindDO;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SocialUserBindMapper extends BaseMapperX<SocialUserBindDO> {
default void deleteByUserTypeAndUserIdAndPlatformAndUnionId(Integer userType, Long userId,
Integer platform, String unionId) {
delete(new LambdaQueryWrapperX<SocialUserBindDO>()
.eq(SocialUserBindDO::getUserType, userType)
.eq(SocialUserBindDO::getUserId, userId)
.eq(SocialUserBindDO::getPlatform, platform)
.eq(SocialUserBindDO::getUnionId, unionId));
}
default SocialUserBindDO selectByUserTypeAndPlatformAndUnionId(Integer userType,
Integer platform, String unionId) {
return selectOne(new LambdaQueryWrapperX<SocialUserBindDO>()
.eq(SocialUserBindDO::getUserType, userType)
.eq(SocialUserBindDO::getPlatform, platform)
.eq(SocialUserBindDO::getUnionId, unionId));
}
}

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.system.dal.mysql.social;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO; import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
@ -11,14 +12,17 @@ import java.util.List;
@Mapper @Mapper
public interface SocialUserMapper extends BaseMapperX<SocialUserDO> { public interface SocialUserMapper extends BaseMapperX<SocialUserDO> {
default List<SocialUserDO> selectListByTypeAndUnionId(Integer userType, Collection<Integer> types, String unionId) { default SocialUserDO selectByTypeAndCodeAnState(Integer type, String code, String state) {
return selectList(new QueryWrapper<SocialUserDO>().eq("user_type", userType) return selectOne(new LambdaQueryWrapper<SocialUserDO>()
.in("type", types).eq("union_id", unionId)); .eq(SocialUserDO::getType, type)
.eq(SocialUserDO::getCode, code)
.eq(SocialUserDO::getState, state));
} }
default List<SocialUserDO> selectListByTypeAndUserId(Integer userType, Collection<Integer> types, Long userId) { default SocialUserDO selectByTypeAndOpenid(Integer type, String openid) {
return selectList(new QueryWrapper<SocialUserDO>().eq("user_type", userType) return selectOne(new LambdaQueryWrapper<SocialUserDO>()
.in("type", types).eq("user_id", userId)); .eq(SocialUserDO::getType, type)
.eq(SocialUserDO::getCode, openid));
} }
default List<SocialUserDO> selectListByUserId(Integer userType, Long userId) { default List<SocialUserDO> selectListByUserId(Integer userType, Long userId) {

@ -23,10 +23,6 @@ public interface RedisKeyConstants {
"login_user:%s", // 参数为 sessionId "login_user:%s", // 参数为 sessionId
STRING, LoginUser.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC); STRING, LoginUser.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC);
RedisKeyDefine SOCIAL_AUTH_USER = new RedisKeyDefine("社交登陆的授权用户",
"social_auth_user:%d:%s", // 参数为 typecode
STRING, AuthUser.class, Duration.ofDays(1));
RedisKeyDefine SOCIAL_AUTH_STATE = new RedisKeyDefine("社交登陆的 state", // 注意,它是被 JustAuth 的 justauth.type.prefix 使用到 RedisKeyDefine SOCIAL_AUTH_STATE = new RedisKeyDefine("社交登陆的 state", // 注意,它是被 JustAuth 的 justauth.type.prefix 使用到
"social_auth_state:%s", // 参数为 state "social_auth_state:%s", // 参数为 state
STRING, String.class, Duration.ofHours(24)); // 值为 state STRING, String.class, Duration.ofHours(24)); // 值为 state

@ -1,39 +0,0 @@
package cn.iocoder.yudao.module.system.dal.redis.social;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthUser;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import static cn.iocoder.yudao.module.system.dal.redis.RedisKeyConstants.SOCIAL_AUTH_USER;
/**
* {@link me.zhyd.oauth.model.AuthUser} RedisDAO
*
* @author
*/
@Repository
public class SocialAuthUserRedisDAO {
@Resource
private StringRedisTemplate stringRedisTemplate;
public AuthUser get(Integer type, AuthCallback authCallback) {
String redisKey = formatKey(type, authCallback.getCode());
return JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), AuthUser.class);
}
public void set(Integer type, AuthCallback authCallback, AuthUser authUser) {
String redisKey = formatKey(type, authCallback.getCode());
stringRedisTemplate.opsForValue().set(redisKey, JsonUtils.toJsonString(authUser), SOCIAL_AUTH_USER.getTimeout());
}
private static String formatKey(Integer type, String code) {
return String.format(SOCIAL_AUTH_USER.getKeyTemplate(), type, code);
}
}

@ -219,8 +219,7 @@ public class AdminAuthServiceImpl implements AdminAuthService {
@Override @Override
public String socialLogin2(AuthSocialLogin2ReqVO reqVO, String userIp, String userAgent) { public String socialLogin2(AuthSocialLogin2ReqVO reqVO, String userIp, String userAgent) {
// 使用 code 授权码,进行登录 // 使用 code 授权码,进行登录
AuthUser authUser = socialUserService.getAuthUser(reqVO.getType(), reqVO.getCode(), reqVO.getState()); socialUserService.authSocialUser(reqVO.getType(), reqVO.getCode(), reqVO.getState());
Assert.notNull(authUser, "授权用户不为空");
// 使用账号密码,进行登录。 // 使用账号密码,进行登录。
LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword()); LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword());

@ -1,11 +1,9 @@
package cn.iocoder.yudao.module.system.service.social; package cn.iocoder.yudao.module.system.service.social;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException; import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO; import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import me.zhyd.oauth.model.AuthUser;
import javax.validation.Valid; import javax.validation.Valid;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@ -28,7 +26,7 @@ public interface SocialUserService {
String getAuthorizeUrl(Integer type, String redirectUri); String getAuthorizeUrl(Integer type, String redirectUri);
/** /**
* *
* {@link ServiceException} * {@link ServiceException}
* *
* @param type {@link SocialTypeEnum} * @param type {@link SocialTypeEnum}
@ -37,17 +35,7 @@ public interface SocialUserService {
* @return * @return
*/ */
@NotNull @NotNull
AuthUser getAuthUser(Integer type, String code, String state); SocialUserDO authSocialUser(Integer type, String code, String state);
/**
* unionId
*
* @param authUser
* @return unionId
*/
default String getAuthUserUnionId(AuthUser authUser) {
return StrUtil.blankToDefault(authUser.getToken().getUnionId(), authUser.getUuid());
}
/** /**
* *
@ -71,25 +59,14 @@ public interface SocialUserService {
* @param userId * @param userId
* @param userType * @param userType
* @param type {@link SocialTypeEnum} * @param type {@link SocialTypeEnum}
* @param unionId unionId * @param openid openid
*/
void unbindSocialUser(Long userId, Integer userType, Integer type, String unionId);
/**
*
* {@link ServiceException}
*
* @param type
* @param code
* @param state state
*/ */
void checkSocialUser(Integer type, String code, String state); void unbindSocialUser(Long userId, Integer userType, Integer type, String openid);
/** /**
* *
* MemberUser AdminUser id * MemberUser AdminUser id
* {@link #checkSocialUser(Integer, String, String)} * {@link ServiceException}
* {@link ServiceException}
* *
* @param userType * @param userType
* @param type * @param type

@ -1,14 +1,14 @@
package cn.iocoder.yudao.module.system.service.social; package cn.iocoder.yudao.module.system.service.social;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.http.HttpUtils; import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserBindDO;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO; import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserBindMapper;
import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserMapper; import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserMapper;
import cn.iocoder.yudao.module.system.dal.redis.social.SocialAuthUserRedisDAO;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import com.google.common.annotations.VisibleForTesting;
import com.xkcoding.justauth.AuthRequestFactory; import com.xkcoding.justauth.AuthRequestFactory;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthCallback; import me.zhyd.oauth.model.AuthCallback;
@ -22,7 +22,6 @@ import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
import java.util.Objects;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
@ -42,8 +41,7 @@ public class SocialUserServiceImpl implements SocialUserService {
private AuthRequestFactory authRequestFactory; private AuthRequestFactory authRequestFactory;
@Resource @Resource
private SocialAuthUserRedisDAO authSocialUserRedisDAO; private SocialUserBindMapper socialUserBindMapper;
@Resource @Resource
private SocialUserMapper socialUserMapper; private SocialUserMapper socialUserMapper;
@ -57,149 +55,85 @@ public class SocialUserServiceImpl implements SocialUserService {
} }
@Override @Override
public AuthUser getAuthUser(Integer type, String code, String state) { public SocialUserDO authSocialUser(Integer type, String code, String state) {
AuthCallback authCallback = buildAuthCallback(code, state); // 优先从 DB 中获取,因为 code 有且可以使用一次。
// 从缓存中获取 // 在社交登录时,当未绑定 User 时,需要绑定登录,此时需要 code 使用两次
AuthUser authUser = authSocialUserRedisDAO.get(type, authCallback); SocialUserDO socialUser = socialUserMapper.selectByTypeAndCodeAnState(type, code, state);
if (authUser != null) { if (socialUser != null) {
return authUser; return socialUser;
} }
// 请求获取 // 请求获取
authUser = this.getAuthUser0(type, authCallback); AuthUser authUser = getAuthUser(type, buildAuthCallback(code, state));
// 缓存。原因是 code 有且可以使用一次。在社交登录时,当未绑定 User 时,需要绑定登录,此时需要 code 使用两次
authSocialUserRedisDAO.set(type, authCallback, authUser);
return authUser;
}
/**
* unionId
* PC unionId
*
* @param type {@link SocialTypeEnum}
* @param unionId unionId
* @param userType
* @return
*/
private List<SocialUserDO> getAllSocialUserList(Integer type, String unionId, Integer userType) {
List<Integer> types = SocialTypeEnum.getRelationTypes(type);
return socialUserMapper.selectListByTypeAndUnionId(userType, types, unionId);
}
@Override
public List<SocialUserDO> getSocialUserList(Long userId, Integer userType) {
return socialUserMapper.selectListByUserId(userType, userId);
}
@Override
public void bindSocialUser(SocialUserBindReqDTO reqDTO) {
// 使用 code 授权
AuthUser authUser = getAuthUser(reqDTO.getType(), reqDTO.getCode(),
reqDTO.getState());
if (authUser == null) { if (authUser == null) {
throw exception(SOCIAL_USER_NOT_FOUND); throw exception(SOCIAL_USER_NOT_FOUND);
} }
// 绑定社交用户(新增) // 保存到 DB 中
bindSocialUser(reqDTO.getUserId(), reqDTO.getUserType(), socialUser = socialUserMapper.selectByTypeAndOpenid(type, authUser.getUuid());
reqDTO.getType(), authUser);
}
/**
*
* @param userId
* @param userType
* @param type {@link SocialTypeEnum}
* @param authUser
*/
@Transactional(rollbackFor = Exception.class)
protected void bindSocialUser(Long userId, Integer userType, Integer type, AuthUser authUser) {
// 获得 unionId 对应的 SocialUserDO 列表
String unionId = getAuthUserUnionId(authUser);
List<SocialUserDO> socialUsers = this.getAllSocialUserList(type, unionId, userType);
// 逻辑一:如果 userId 之前绑定过该 type 的其它账号,需要进行解绑
this.unbindOldSocialUser(userId, userType, type, unionId);
// 逻辑二:如果 socialUsers 指定的 userId 改变,需要进行更新
// 例如说,一个微信 unionId 对应了多个社交账号,结果其中有个关联了新的 userId则其它也要跟着修改
// 考虑到 socialUsers 一般比较少,直接 for 循环更新即可
socialUsers.forEach(socialUser -> {
if (Objects.equals(socialUser.getUserId(), userId)) {
return;
}
socialUserMapper.updateById(new SocialUserDO().setId(socialUser.getId()).setUserId(userId));
});
// 逻辑三:如果 authUser 不存在于 socialUsers 中,则进行新增;否则,进行更新
SocialUserDO socialUser = CollUtil.findOneByField(socialUsers, "openid", authUser.getUuid());
SocialUserDO saveSocialUser = SocialUserDO.builder() // 新增和更新的通用属性
.token(authUser.getToken().getAccessToken()).rawTokenInfo(toJsonString(authUser.getToken()))
.nickname(authUser.getNickname()).avatar(authUser.getAvatar()).rawUserInfo(toJsonString(authUser.getRawUserInfo()))
.build();
if (socialUser == null) { if (socialUser == null) {
saveSocialUser.setUserId(userId).setUserType(userType) socialUser = new SocialUserDO();
.setType(type).setOpenid(authUser.getUuid()).setUnionId(unionId); }
socialUserMapper.insert(saveSocialUser); socialUser.setOpenid(authUser.getUuid()).setToken(authUser.getToken().getAccessToken()).setRawTokenInfo((toJsonString(authUser.getToken())))
.setUnionId(StrUtil.blankToDefault(authUser.getToken().getUnionId(), authUser.getUuid())) // unionId 识别多个用户
.setNickname(authUser.getNickname()).setAvatar(authUser.getAvatar()).setRawUserInfo(toJsonString(authUser.getRawUserInfo()))
.setCode(code).setState(state); // 需要保存 code + state 字段,保证后续可查询
if (socialUser.getId() == null) {
socialUserMapper.insert(socialUser);
} else { } else {
saveSocialUser.setId(socialUser.getId()); socialUserMapper.updateById(socialUser);
socialUserMapper.updateById(saveSocialUser);
} }
return socialUser;
} }
@Override @Override
public void unbindSocialUser(Long userId, Integer userType, Integer type, String unionId) { public List<SocialUserDO> getSocialUserList(Long userId, Integer userType) {
// 获得 unionId 对应的所有 SocialUserDO 社交用户 return socialUserMapper.selectListByUserId(userType, userId);
List<SocialUserDO> socialUsers = this.getAllSocialUserList(type, unionId, userType);
if (CollUtil.isEmpty(socialUsers)) {
return;
}
// 校验,是否解绑的是非自己的
socialUsers.forEach(socialUser -> {
if (!Objects.equals(socialUser.getUserId(), userId)) {
throw exception(SOCIAL_USER_UNBIND_NOT_SELF);
}
});
// 解绑
socialUserMapper.deleteBatchIds(CollectionUtils.convertSet(socialUsers, SocialUserDO::getId));
} }
@Override @Override
public void checkSocialUser(Integer type, String code, String state) { @Transactional
AuthUser authUser = getAuthUser(type, code, state); public void bindSocialUser(SocialUserBindReqDTO reqDTO) {
if (authUser == null) { // 获得社交用户
throw exception(SOCIAL_USER_NOT_FOUND); SocialUserDO socialUser = authSocialUser(reqDTO.getType(), reqDTO.getCode(), reqDTO.getState());
} Assert.notNull(socialUser, "社交用户不能为空");
// 如果 userId 之前绑定过该 type 的其它账号,需要进行解绑
socialUserBindMapper.deleteByUserTypeAndUserIdAndPlatformAndUnionId(reqDTO.getUserType(), reqDTO.getUserId(),
SocialTypeEnum.valueOfType(socialUser.getType()).getPlatform(), socialUser.getUnionId());
// 绑定当前登录的社交用户
SocialUserBindDO socialUserBind = SocialUserBindDO.builder().userId(reqDTO.getUserId()).userType(reqDTO.getUserType())
.unionId(socialUser.getUnionId()).build();
socialUserBindMapper.insert(socialUserBind);
} }
@Override @Override
public Long getBindUserId(Integer userType, Integer type, String code, String state) { public void unbindSocialUser(Long userId, Integer userType, Integer type, String openid) {
AuthUser authUser = getAuthUser(type, code, state); // 获得 openid 对应的 SocialUserDO 社交用户
if (authUser == null) { SocialUserDO socialUser = socialUserMapper.selectByTypeAndOpenid(type, openid);
if (socialUser == null) {
throw exception(SOCIAL_USER_NOT_FOUND); throw exception(SOCIAL_USER_NOT_FOUND);
} }
// 如果未绑定 SocialUserDO 用户,则无法自动登录,进行报错 // 获得对应的社交绑定关系
String unionId = getAuthUserUnionId(authUser); socialUserBindMapper.deleteByUserTypeAndUserIdAndPlatformAndUnionId(userType, userId,
List<SocialUserDO> socialUsers = getAllSocialUserList(type, unionId, userType); SocialTypeEnum.valueOfType(socialUser.getType()).getPlatform(), socialUser.getUnionId());
if (CollUtil.isEmpty(socialUsers)) {
throw exception(AUTH_THIRD_LOGIN_NOT_BIND);
}
return socialUsers.get(0).getUserId();
} }
@VisibleForTesting @Override
public void unbindOldSocialUser(Long userId, Integer userType, Integer type, String newUnionId) { public Long getBindUserId(Integer userType, Integer type, String code, String state) {
List<Integer> types = SocialTypeEnum.getRelationTypes(type); // 获得社交用户
List<SocialUserDO> oldSocialUsers = socialUserMapper.selectListByTypeAndUserId(userType, types, userId); SocialUserDO socialUser = authSocialUser(type, code, state);
// 如果新老的 unionId 是一致的,说明无需解绑 Assert.notNull(socialUser, "社交用户不能为空");
if (CollUtil.isEmpty(oldSocialUsers) || Objects.equals(newUnionId, oldSocialUsers.get(0).getUnionId())) {
return; // 如果未绑定的社交用户,则无法自动登录,进行报错
SocialUserBindDO socialUserBind = socialUserBindMapper.selectByUserTypeAndPlatformAndUnionId(userType,
SocialTypeEnum.valueOfType(socialUser.getType()).getPlatform(), socialUser.getUnionId());
if (socialUserBind == null) {
throw exception(AUTH_THIRD_LOGIN_NOT_BIND);
} }
return socialUserBind.getUserId();
// 解绑
socialUserMapper.deleteBatchIds(CollectionUtils.convertSet(oldSocialUsers, SocialUserDO::getId));
} }
/** /**
@ -209,7 +143,7 @@ public class SocialUserServiceImpl implements SocialUserService {
* @param authCallback * @param authCallback
* @return * @return
*/ */
private AuthUser getAuthUser0(Integer type, AuthCallback authCallback) { private AuthUser getAuthUser(Integer type, AuthCallback authCallback) {
AuthRequest authRequest = authRequestFactory.get(SocialTypeEnum.valueOfType(type).getSource()); AuthRequest authRequest = authRequestFactory.get(SocialTypeEnum.valueOfType(type).getSource());
AuthResponse<?> authResponse = authRequest.login(authCallback); AuthResponse<?> authResponse = authRequest.login(authCallback);
log.info("[getAuthUser0][请求社交平台 type({}) request({}) response({})]", type, toJsonString(authCallback), log.info("[getAuthUser0][请求社交平台 type({}) request({}) response({})]", type, toJsonString(authCallback),

@ -1,32 +1,29 @@
package cn.iocoder.yudao.module.system.service.social; package cn.iocoder.yudao.module.system.service.social;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserMapper; import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserMapper;
import cn.iocoder.yudao.module.system.dal.redis.social.SocialAuthUserRedisDAO;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbAndRedisUnitTest; import cn.iocoder.yudao.framework.test.core.ut.BaseDbAndRedisUnitTest;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.xkcoding.justauth.AuthRequestFactory; import com.xkcoding.justauth.AuthRequestFactory;
import me.zhyd.oauth.model.AuthUser; import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List;
import static cn.hutool.core.util.RandomUtil.randomEle; import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.hutool.core.util.RandomUtil.randomString; import static cn.hutool.core.util.RandomUtil.randomString;
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;
@Import({SocialUserServiceImpl.class, SocialAuthUserRedisDAO.class}) @Import(SocialUserServiceImpl.class)
public class SocialUserServiceTest extends BaseDbAndRedisUnitTest { public class SocialUserServiceTest extends BaseDbAndRedisUnitTest {
@Resource @Resource
private SocialUserServiceImpl socialService; private SocialUserServiceImpl socialUserService;
@Resource @Resource
private SocialUserMapper socialUserMapper; private SocialUserMapper socialUserMapper;
@ -34,132 +31,152 @@ public class SocialUserServiceTest extends BaseDbAndRedisUnitTest {
@MockBean @MockBean
private AuthRequestFactory authRequestFactory; private AuthRequestFactory authRequestFactory;
/**
* SocialUserDO
*/
@Test @Test
public void testBindSocialUser_create() { public void testGetAuthorizeUrl() {
// mock 数据 try (MockedStatic<AuthStateUtils> authStateUtilsMock = mockStatic(AuthStateUtils.class)) {
// 准备参数 // 准备参数
Long userId = randomLongId(); Integer type = 31;
Integer type = randomEle(SocialTypeEnum.values()).getType(); String redirectUri = "sss";
AuthUser authUser = randomPojo(AuthUser.class); // mock 获得对应的 AuthRequest 实现
// mock 方法 AuthRequest authRequest = mock(AuthRequest.class);
when(authRequestFactory.get(eq("WECHAT_MP"))).thenReturn(authRequest);
// 调用 // mock 方法
socialService.bindSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, authUser); authStateUtilsMock.when(AuthStateUtils::createState).thenReturn("aoteman");
// 断言 when(authRequest.authorize(eq("aoteman"))).thenReturn("https://www.iocoder.cn?redirect_uri=yyy");
List<SocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId);
assertEquals(1, socialUsers.size()); // 调用
assertBindSocialUser(socialUsers.get(0), authUser, userId, type); String url = socialUserService.getAuthorizeUrl(type, redirectUri);
} // 断言
assertEquals("https://www.iocoder.cn/?redirect_uri=sss", url);
/** }
* SocialUserDO
*/
@Test
public void testBindSocialUser_update() {
// mock 数据
SocialUserDO dbSocialUser = randomPojo(SocialUserDO.class, socialUserDO -> {
socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
socialUserDO.setType(randomEle(SocialTypeEnum.values()).getType());
});
socialUserMapper.insert(dbSocialUser);
// 准备参数
Long userId = dbSocialUser.getUserId();
Integer type = dbSocialUser.getType();
AuthUser authUser = randomPojo(AuthUser.class);
// mock 方法
// 调用
socialService.bindSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, authUser);
// 断言
List<SocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId);
assertEquals(1, socialUsers.size());
assertBindSocialUser(socialUsers.get(0), authUser, userId, type);
} }
/** // /**
* // * 情况一,创建 SocialUserDO 的情况
*/ // */
@Test // @Test
public void testBindSocialUser_userId() { // public void testBindSocialUser_create() {
// mock 数据 // // mock 数据
SocialUserDO dbSocialUser = randomPojo(SocialUserDO.class, socialUserDO -> { // // 准备参数
socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); // Long userId = randomLongId();
socialUserDO.setType(randomEle(SocialTypeEnum.values()).getType()); // Integer type = randomEle(SocialTypeEnum.values()).getType();
}); // AuthUser authUser = randomPojo(AuthUser.class);
socialUserMapper.insert(dbSocialUser); // // mock 方法
// 准备参数 //
Long userId = randomLongId(); // // 调用
Integer type = dbSocialUser.getType(); // socialService.bindSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, authUser);
AuthUser authUser = randomPojo(AuthUser.class); // // 断言
// mock 方法 // List<SocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId);
// assertEquals(1, socialUsers.size());
// 调用 // assertBindSocialUser(socialUsers.get(0), authUser, userId, type);
socialService.bindSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, authUser); // }
// 断言 //
List<SocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId); // /**
assertEquals(1, socialUsers.size()); // * 情况二,更新 SocialUserDO 的情况
} // */
// @Test
private void assertBindSocialUser(SocialUserDO socialUser, AuthUser authUser, Long userId, // public void testBindSocialUser_update() {
Integer type) { // // mock 数据
assertEquals(authUser.getToken().getAccessToken(), socialUser.getToken()); // SocialUserDO dbSocialUser = randomPojo(SocialUserDO.class, socialUserDO -> {
assertEquals(toJsonString(authUser.getToken()), socialUser.getRawTokenInfo()); // socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
assertEquals(authUser.getNickname(), socialUser.getNickname()); // socialUserDO.setType(randomEle(SocialTypeEnum.values()).getType());
assertEquals(authUser.getAvatar(), socialUser.getAvatar()); // });
assertEquals(toJsonString(authUser.getRawUserInfo()), socialUser.getRawUserInfo()); // socialUserMapper.insert(dbSocialUser);
assertEquals(userId, socialUser.getUserId()); // // 准备参数
assertEquals(UserTypeEnum.ADMIN.getValue(), socialUser.getUserType()); // Long userId = dbSocialUser.getUserId();
assertEquals(type, socialUser.getType()); // Integer type = dbSocialUser.getType();
assertEquals(authUser.getUuid(), socialUser.getOpenid()); // AuthUser authUser = randomPojo(AuthUser.class);
assertEquals(socialService.getAuthUserUnionId(authUser), socialUser.getUnionId()); // // mock 方法
} //
// // 调用
/** // socialService.bindSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, authUser);
* unionId // // 断言
*/ // List<SocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId);
@Test // assertEquals(1, socialUsers.size());
public void testUnbindOldSocialUser_no() { // assertBindSocialUser(socialUsers.get(0), authUser, userId, type);
// mock 数据 // }
SocialUserDO oldSocialUser = randomPojo(SocialUserDO.class, socialUserDO -> { //
socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); // /**
socialUserDO.setType(randomEle(SocialTypeEnum.values()).getType()); // * 情况一和二都存在的,逻辑二的场景
}); // */
socialUserMapper.insert(oldSocialUser); // @Test
// 准备参数 // public void testBindSocialUser_userId() {
Long userId = oldSocialUser.getUserId(); // // mock 数据
Integer type = oldSocialUser.getType(); // SocialUserDO dbSocialUser = randomPojo(SocialUserDO.class, socialUserDO -> {
String newUnionId = oldSocialUser.getUnionId(); // socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
// socialUserDO.setType(randomEle(SocialTypeEnum.values()).getType());
// 调用 // });
socialService.unbindOldSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, newUnionId); // socialUserMapper.insert(dbSocialUser);
// 断言 // // 准备参数
assertEquals(1L, socialUserMapper.selectCount(null).longValue()); // Long userId = randomLongId();
} // Integer type = dbSocialUser.getType();
// AuthUser authUser = randomPojo(AuthUser.class);
// // mock 方法
/** //
* unionId // // 调用
*/ // socialService.bindSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, authUser);
@Test // // 断言
public void testUnbindOldSocialUser_yes() { // List<SocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId);
// mock 数据 // assertEquals(1, socialUsers.size());
SocialUserDO oldSocialUser = randomPojo(SocialUserDO.class, socialUserDO -> { // }
socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); //
socialUserDO.setType(randomEle(SocialTypeEnum.values()).getType()); // private void assertBindSocialUser(SocialUserDO socialUser, AuthUser authUser, Long userId,
}); // Integer type) {
socialUserMapper.insert(oldSocialUser); // assertEquals(authUser.getToken().getAccessToken(), socialUser.getToken());
// 准备参数 // assertEquals(toJsonString(authUser.getToken()), socialUser.getRawTokenInfo());
Long userId = oldSocialUser.getUserId(); // assertEquals(authUser.getNickname(), socialUser.getNickname());
Integer type = oldSocialUser.getType(); // assertEquals(authUser.getAvatar(), socialUser.getAvatar());
String newUnionId = randomString(10); // assertEquals(toJsonString(authUser.getRawUserInfo()), socialUser.getRawUserInfo());
// assertEquals(userId, socialUser.getUserId());
// 调用 // assertEquals(UserTypeEnum.ADMIN.getValue(), socialUser.getUserType());
socialService.unbindOldSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, newUnionId); // assertEquals(type, socialUser.getType());
// 断言 // assertEquals(authUser.getUuid(), socialUser.getOpenid());
assertEquals(0L, socialUserMapper.selectCount(null).longValue()); // assertEquals(socialService.getAuthUserUnionId(authUser), socialUser.getUnionId());
} // }
//
// /**
// * 情况一,如果新老的 unionId 是一致的,无需解绑
// */
// @Test
// public void testUnbindOldSocialUser_no() {
// // mock 数据
// SocialUserDO oldSocialUser = randomPojo(SocialUserDO.class, socialUserDO -> {
// socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
// socialUserDO.setType(randomEle(SocialTypeEnum.values()).getType());
// });
// socialUserMapper.insert(oldSocialUser);
// // 准备参数
// Long userId = oldSocialUser.getUserId();
// Integer type = oldSocialUser.getType();
// String newUnionId = oldSocialUser.getUnionId();
//
// // 调用
// socialService.unbindOldSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, newUnionId);
// // 断言
// assertEquals(1L, socialUserMapper.selectCount(null).longValue());
// }
//
//
// /**
// * 情况二,如果新老的 unionId 不一致的,需解绑
// */
// @Test
// public void testUnbindOldSocialUser_yes() {
// // mock 数据
// SocialUserDO oldSocialUser = randomPojo(SocialUserDO.class, socialUserDO -> {
// socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
// socialUserDO.setType(randomEle(SocialTypeEnum.values()).getType());
// });
// socialUserMapper.insert(oldSocialUser);
// // 准备参数
// Long userId = oldSocialUser.getUserId();
// Integer type = oldSocialUser.getType();
// String newUnionId = randomString(10);
//
// // 调用
// socialService.unbindOldSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, newUnionId);
// // 断言
// assertEquals(0L, socialUserMapper.selectCount(null).longValue());
// }
} }

Loading…
Cancel
Save