mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-09-17 23:46:12 +08:00
Gitee授权登录成功
This commit is contained in:
@@ -5,6 +5,7 @@ import me.zhyd.oauth.model.AuthResponse;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import plus.bookshelf.Common.Error.BusinessErrorCode;
|
||||
@@ -15,8 +16,9 @@ import plus.bookshelf.Dao.Mapper.ThirdPartyUserAuthDOMapper;
|
||||
import plus.bookshelf.Dao.Mapper.ThirdPartyUserDOMapper;
|
||||
import plus.bookshelf.Service.Model.UserModel;
|
||||
import plus.bookshelf.Service.Service.ThirdPartyUserService;
|
||||
import plus.bookshelf.Service.Service.UserService;
|
||||
|
||||
import java.util.Map;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Service
|
||||
public class ThirdPartyUserServiceImpl implements ThirdPartyUserService {
|
||||
@@ -30,6 +32,16 @@ public class ThirdPartyUserServiceImpl implements ThirdPartyUserService {
|
||||
@Autowired
|
||||
UserServiceImpl userService;
|
||||
|
||||
@Autowired
|
||||
RedisTemplate redisTemplate;
|
||||
|
||||
/**
|
||||
* 第三方登录
|
||||
*
|
||||
* @param authResponse
|
||||
* @return
|
||||
* @throws BusinessException
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public UserModel loginCallback(AuthResponse authResponse) throws BusinessException {
|
||||
@@ -53,14 +65,16 @@ public class ThirdPartyUserServiceImpl implements ThirdPartyUserService {
|
||||
|
||||
if (existThirdPartyUserDO != null) {
|
||||
// 之前已经授权登录过
|
||||
// 更新数据库中的第三方登录信息
|
||||
currentThirdPartyUserDO.setId(existThirdPartyUserDO.getId());
|
||||
thirdPartyUserDOMapper.updateByPrimaryKeySelective(currentThirdPartyUserDO);
|
||||
|
||||
// 检查第三方账号有无绑定到系统账号
|
||||
ThirdPartyUserAuthDO thirdPartyUserAuthDO = thirdPartyUserAuthDOMapper.selectByThirdPartyUserId(currentThirdPartyUserDO.getId());
|
||||
ThirdPartyUserAuthDO thirdPartyUserAuthDO = thirdPartyUserAuthDOMapper.selectByThirdPartyUserId(existThirdPartyUserDO.getId());
|
||||
if (thirdPartyUserAuthDO != null) {
|
||||
// 已经绑定到系统账号
|
||||
|
||||
// 更新数据库中的第三方登录信息
|
||||
currentThirdPartyUserDO.setId(existThirdPartyUserDO.getId());
|
||||
thirdPartyUserDOMapper.updateByPrimaryKeySelective(currentThirdPartyUserDO);
|
||||
|
||||
// 取得用户信息,并登录
|
||||
Integer userId = thirdPartyUserAuthDO.getUserId();
|
||||
UserModel userModel = userService.getUserById(userId);
|
||||
@@ -71,8 +85,6 @@ public class ThirdPartyUserServiceImpl implements ThirdPartyUserService {
|
||||
}
|
||||
} else {
|
||||
// 之前未授权登录过
|
||||
// 将新的用户信息插入到数据库
|
||||
thirdPartyUserDOMapper.insertSelective(currentThirdPartyUserDO);
|
||||
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "第三方登录失败,该第三方账号未绑定到系统账号,请先绑定");
|
||||
}
|
||||
} else {
|
||||
@@ -81,6 +93,70 @@ public class ThirdPartyUserServiceImpl implements ThirdPartyUserService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Boolean bindThirdPartAccountCallback(AuthResponse authResponse, String token) throws BusinessException {
|
||||
int code = authResponse.getCode();
|
||||
if (code == 5008) {
|
||||
// 第三方平台拒绝授权
|
||||
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "绑定失败,用户已取消第三方授权或第三方平台拒绝授权");
|
||||
} else if (code == 5009) {
|
||||
// 授权过期
|
||||
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "第三方授权过期,请尝试重新绑定");
|
||||
}
|
||||
if (code == 2000) {
|
||||
// 回调成功
|
||||
// 将回调结果转换为 Data Object
|
||||
ThirdPartyUserDO currentThirdPartyUserDO = getThirdPartyUserDOFromAuthData(authResponse.getData());
|
||||
|
||||
// 根据 uuid + source 唯一确定一个平台的用户 refer: https://justauth.wiki/features/integrate-existing-systems/
|
||||
String uuid = currentThirdPartyUserDO.getUuid();
|
||||
String source = currentThirdPartyUserDO.getSource();
|
||||
ThirdPartyUserDO existThirdPartyUserDO = thirdPartyUserDOMapper.selectByUuidAndSource(uuid, source);
|
||||
|
||||
if (existThirdPartyUserDO == null) {
|
||||
// 之前未授权过
|
||||
|
||||
// 获取当前登录用户信息
|
||||
UserModel userModel = userService.getUserByToken(redisTemplate, token);
|
||||
if (userModel == null) {
|
||||
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "绑定失败,用户未登录");
|
||||
}
|
||||
|
||||
// 将用户账号与第三方账号信息插入到数据库
|
||||
int affectRows = thirdPartyUserDOMapper.insertSelective(currentThirdPartyUserDO);
|
||||
|
||||
// 判断是否插入成功
|
||||
if (affectRows > 0) {
|
||||
// 用户第三方账号保存到数据库表中的主键id
|
||||
Integer lastInsertId = thirdPartyUserDOMapper.getLastInsertId();
|
||||
|
||||
// 在 Auth 表中建立 用户 和第三方授权的联系
|
||||
ThirdPartyUserAuthDO thirdPartyUserAuthDO = new ThirdPartyUserAuthDO();
|
||||
thirdPartyUserAuthDO.setThirdPartyUserId(lastInsertId);
|
||||
thirdPartyUserAuthDO.setUserId(userModel.getId());
|
||||
int affectRows2 = thirdPartyUserAuthDOMapper.insert(thirdPartyUserAuthDO);
|
||||
// 判断是否插入成功
|
||||
if (affectRows2 > 0) {
|
||||
// 绑定成功
|
||||
return true;
|
||||
} else {
|
||||
throw new BusinessException(BusinessErrorCode.UNKNOWN_ERROR, "绑定失败,系统错误");
|
||||
}
|
||||
} else {
|
||||
// 第三方账号信息插入失败
|
||||
throw new BusinessException(BusinessErrorCode.UNKNOWN_ERROR, "绑定失败,系统错误");
|
||||
}
|
||||
} else {
|
||||
// 之前已经授权过
|
||||
throw new BusinessException(BusinessErrorCode.THIRD_PARTY_ACCOUNT_ALREADY_BOUND, "绑定失败,该账号已被其他账号绑定");
|
||||
}
|
||||
} else {
|
||||
// 未知错误
|
||||
throw new BusinessException(BusinessErrorCode.UNKNOWN_ERROR, "未知错误,绑定失败");
|
||||
}
|
||||
}
|
||||
|
||||
private ThirdPartyUserDO getThirdPartyUserDOFromAuthData(Object authData) {
|
||||
AuthUser data = (AuthUser) authData;
|
||||
String uuid = data.getUuid();
|
||||
|
Reference in New Issue
Block a user