update 优化 !pr382 修复一些问题 完事流程逻辑

This commit is contained in:
疯狂的狮子Li
2023-07-02 16:13:14 +08:00
parent 6b14778691
commit 06588f3ad4
6 changed files with 90 additions and 154 deletions

View File

@ -103,22 +103,21 @@ public class AuthController {
}
/**
* 第三方登录回调业务处理
* 绑定授权
* @param loginBody
* 第三方登录回调业务处理 绑定授权
*
* @param loginBody 请求体
* @return 结果
*/
@SuppressWarnings("unchecked")
@PostMapping("/social/callback")
public R<LoginVo> socialLogin(@RequestBody LoginBody loginBody) {
// 获取第三方登录信息
AuthResponse<AuthUser> response = SocialUtils.loginAuth(loginBody, socialProperties);
AuthUser authUserData = response.getData();
// 判断授权响应是否成功
if (!response.ok()) {
return R.fail(response.getMsg());
}
return loginService.sociaRegister(authUserData);
public R<LoginVo> socialCallback(@RequestBody LoginBody loginBody) {
// 获取第三方登录信息
AuthResponse<AuthUser> response = SocialUtils.loginAuth(loginBody, socialProperties);
AuthUser authUserData = response.getData();
// 判断授权响应是否成功
if (!response.ok()) {
return R.fail(response.getMsg());
}
return loginService.sociaRegister(authUserData);
}

View File

@ -67,13 +67,13 @@ public class SysLoginService {
private final SysUserMapper userMapper;
/**
* 绑定第三方用户
*
* @param authUserData 授权响应实体
* @return 统一响应实体
*/
public R<LoginVo> sociaRegister(AuthUser authUserData ){
public R<LoginVo> sociaRegister(AuthUser authUserData) {
SysSocialBo bo = new SysSocialBo();
bo.setUserId(LoginHelper.getUserId());
bo.setAuthId(authUserData.getSource() + authUserData.getUuid());
@ -123,9 +123,9 @@ public class SysLoginService {
private SysUserVo loadUserByUsername(String tenantId, String username) {
SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>()
.select(SysUser::getUserName, SysUser::getStatus)
.eq(TenantHelper.isEnable(), SysUser::getTenantId, tenantId)
.eq(SysUser::getUserName, username));
.select(SysUser::getUserName, SysUser::getStatus)
.eq(TenantHelper.isEnable(), SysUser::getTenantId, tenantId)
.eq(SysUser::getUserName, username));
if (ObjectUtil.isNull(user)) {
log.info("登录用户:{} 不存在.", username);
throw new UserException("user.not.exists", username);
@ -225,7 +225,7 @@ public class SysLoginService {
log.info("登录租户:{} 已被停用.", tenantId);
throw new TenantException("tenant.blocked");
} else if (ObjectUtil.isNotNull(tenant.getExpireTime())
&& new Date().after(tenant.getExpireTime())) {
&& new Date().after(tenant.getExpireTime())) {
log.info("登录租户:{} 已超过有效期.", tenantId);
throw new TenantException("tenant.expired");
}

View File

@ -11,17 +11,21 @@ import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthUser;
import org.dromara.common.core.constant.Constants;
import org.dromara.common.core.domain.model.LoginBody;
import org.dromara.common.core.domain.model.SocialLogin;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.enums.UserStatus;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.exception.user.UserException;
import org.dromara.common.core.utils.MessageUtils;
import org.dromara.common.core.utils.ValidatorUtils;
import org.dromara.common.core.validate.auth.SocialGroup;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.social.config.properties.SocialProperties;
import org.dromara.common.social.utils.SocialUtils;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.system.domain.SysClient;
import org.dromara.system.domain.SysUser;
import org.dromara.system.domain.vo.SysSocialVo;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.mapper.SysUserMapper;
import org.dromara.system.service.ISysSocialService;
import org.dromara.web.domain.vo.LoginVo;
@ -37,7 +41,7 @@ import org.springframework.stereotype.Service;
@Slf4j
@Service("social" + IAuthStrategy.BASE_NAME)
@RequiredArgsConstructor
public class socialAuthStrategy implements IAuthStrategy {
public class SocialAuthStrategy implements IAuthStrategy {
private final SocialProperties socialProperties;
private final ISysSocialService sysSocialService;
@ -52,13 +56,14 @@ public class socialAuthStrategy implements IAuthStrategy {
/**
* 登录-第三方授权登录
* @param clientId 客户端id
*
* @param clientId 客户端id
* @param loginBody 登录信息
* @param client 客户端信息
* @param client 客户端信息
*/
@Override
public LoginVo login(String clientId, LoginBody loginBody, SysClient client) {
AuthResponse<AuthUser> response = SocialUtils.loginAuth(loginBody,socialProperties);
AuthResponse<AuthUser> response = SocialUtils.loginAuth(loginBody, socialProperties);
if (!response.ok()) {
throw new ServiceException(response.getMsg());
}
@ -66,30 +71,19 @@ public class socialAuthStrategy implements IAuthStrategy {
SysSocialVo social = sysSocialService.selectByAuthId(authUserData.getSource() + authUserData.getUuid());
if (!ObjectUtil.isNotNull(social)) {
throw new ServiceException("你还没有绑定第三方账号,绑定后才可以登录!");
}//验证授权表里面的租户id是否包含当前租户id
if (ObjectUtil.isNotNull(social) && StrUtil.isNotBlank(social.getTenantId())
&& !social.getTenantId().contains(loginBody.getTenantId())) {
}
// 验证授权表里面的租户id是否包含当前租户id
String tenantId = social.getTenantId();
if (ObjectUtil.isNotNull(social) && StrUtil.isNotBlank(tenantId)
&& !tenantId.contains(loginBody.getTenantId())) {
throw new ServiceException("对不起,你没有权限登录当前租户!");
}
return loadinUser(social, client);
}
/**
* 登录用户信息
*
* @param social
* @param client
* @return
*/
private LoginVo loadinUser(SysSocialVo social, SysClient client) {
SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>()
.eq(SysUser::getUserId, social.getUserId()));
SocialLogin loginUser = new SocialLogin();
loginUser.setUserId(user.getUserId());
loginUser.setTenantId(user.getTenantId());
loginUser.setUsername(user.getUserName());
loginUser.setUserType(user.getUserType());
// 执行登录
// 查找用户
SysUserVo user = loadUser(tenantId, social.getUserId());
// 此处可根据登录用户的数据不同 自行创建 loginUser 属性不够用继承扩展就行了
LoginUser loginUser = loginService.buildLoginUser(user);
SaLoginModel model = new SaLoginModel();
model.setDevice(client.getDeviceType());
// 自定义分配 不同用户体系 不同 token 授权时间 不设置默认走全局 yml 配置
@ -106,4 +100,22 @@ public class socialAuthStrategy implements IAuthStrategy {
return loginVo;
}
private SysUserVo loadUser(String tenantId, Long userId) {
SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>()
.select(SysUser::getUserName, SysUser::getStatus)
.eq(TenantHelper.isEnable(), SysUser::getTenantId, tenantId)
.eq(SysUser::getUserId, userId));
if (ObjectUtil.isNull(user)) {
log.info("登录用户:{} 不存在.", "");
throw new UserException("user.not.exists", "");
} else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
log.info("登录用户:{} 已被停用.", "");
throw new UserException("user.blocked", "");
}
if (TenantHelper.isEnable()) {
return userMapper.selectTenantUserByUserName(user.getUserName(), tenantId);
}
return userMapper.selectUserByUserName(user.getUserName());
}
}