update 优化 支持多租户绑定相同的三方登录

This commit is contained in:
疯狂的狮子Li 2023-11-19 21:37:03 +08:00
parent 754c22f8a6
commit f2956c322e
4 changed files with 17 additions and 15 deletions

View File

@ -3,6 +3,7 @@ package org.dromara.web.service;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -78,13 +79,13 @@ public class SysLoginService {
bo.setUserName(authUserData.getUsername());
bo.setNickName(authUserData.getNickname());
// 查询是否已经绑定用户
SysSocialVo vo = sysSocialService.selectByAuthId(authId);
if (ObjectUtil.isEmpty(vo)) {
List<SysSocialVo> list = sysSocialService.selectByAuthId(authId);
if (CollUtil.isEmpty(list)) {
// 没有绑定用户, 新增用户信息
sysSocialService.insertByBo(bo);
} else {
// 更新用户信息
bo.setId(vo.getId());
bo.setId(list.get(0).getId());
sysSocialService.updateByBo(bo);
}
}

View File

@ -2,9 +2,9 @@ package org.dromara.web.service.impl;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@ -34,6 +34,9 @@ import org.dromara.web.service.IAuthStrategy;
import org.dromara.web.service.SysLoginService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
/**
* 第三方授权策略
*
@ -76,19 +79,17 @@ public class SocialAuthStrategy implements IAuthStrategy {
.executeAsync();
}
SysSocialVo social = sysSocialService.selectByAuthId(authUserData.getSource() + authUserData.getUuid());
if (!ObjectUtil.isNotNull(social)) {
List<SysSocialVo> list = sysSocialService.selectByAuthId(authUserData.getSource() + authUserData.getUuid());
if (CollUtil.isEmpty(list)) {
throw new ServiceException("你还没有绑定第三方账号,绑定后才可以登录!");
}
// 验证授权表里面的租户id是否包含当前租户id
String tenantId = social.getTenantId();
if (ObjectUtil.isNotNull(social) && StrUtil.isNotBlank(tenantId)
&& !tenantId.contains(loginBody.getTenantId())) {
Optional<SysSocialVo> opt = list.stream().filter(x -> x.getTenantId().equals(loginBody.getTenantId())).findAny();
if (opt.isEmpty()) {
throw new ServiceException("对不起,你没有权限登录当前租户!");
}
SysSocialVo social = opt.get();
// 查找用户
SysUserVo user = loadUser(tenantId, social.getUserId());
SysUserVo user = loadUser(social.getTenantId(), social.getUserId());
// 此处可根据登录用户的数据不同 自行创建 loginUser 属性不够用继承扩展就行了
LoginUser loginUser = loginService.buildLoginUser(user);

View File

@ -49,7 +49,7 @@ public interface ISysSocialService {
* @param authId 认证ID
* @return SysSocial
*/
SysSocialVo selectByAuthId(String authId);
List<SysSocialVo> selectByAuthId(String authId);
}

View File

@ -99,8 +99,8 @@ public class SysSocialServiceImpl implements ISysSocialService {
* @return 授权信息
*/
@Override
public SysSocialVo selectByAuthId(String authId) {
return baseMapper.selectVoOne(new LambdaQueryWrapper<SysSocial>().eq(SysSocial::getAuthId, authId));
public List<SysSocialVo> selectByAuthId(String authId) {
return baseMapper.selectVoList(new LambdaQueryWrapper<SysSocial>().eq(SysSocial::getAuthId, authId));
}
}