mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-09-25 19:05:14 +08:00
第三方登录回调后登陆系统用户逻辑基本完成
This commit is contained in:
@@ -11,6 +11,9 @@ html,
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: bookshelfplusFont;
|
||||
|
||||
/* 字体加载前先隐藏,不然文字会闪一下 */
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
a,
|
||||
|
@@ -63,6 +63,14 @@ router.get('/register', function (req, res) {
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/callback/:platform', function (req, res) {
|
||||
// 第三方登录回调页面
|
||||
res.render('callback', {
|
||||
title: getPageTitle("正在跳转"),
|
||||
platform: req.params.platform
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/dashboard/:group/:page', function (req, res) {
|
||||
var navbarLinks = null;
|
||||
if (req.params.group === "admin") {
|
||||
|
43
bookshelfplus-frontend/views/callback.html
Normal file
43
bookshelfplus-frontend/views/callback.html
Normal file
@@ -0,0 +1,43 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
||||
<title><%=title%></title>
|
||||
<!-- <script src="/assets/lib/jquery/3.6.0/jquery.min.js"></script> -->
|
||||
<script src="/assets/lib/axios/0.26.1/axios.min.js"></script>
|
||||
<script src="/assets/javascripts/httpRequest.js"></script>
|
||||
<script>
|
||||
// API地址
|
||||
const APIHOST = '<%= global.site.api.prefix %>';
|
||||
axios.defaults.baseURL = APIHOST;
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<p id="displayText">
|
||||
正在跳转中,请稍后...
|
||||
</p>
|
||||
<script>
|
||||
getRequest("/third-party/callback/<%=platform%>" + location.search)
|
||||
.then(function (response) {
|
||||
var axiosData = response.data;
|
||||
var status = axiosData.status;
|
||||
var data = axiosData.data;
|
||||
if (status === "success") {
|
||||
console.log(data)
|
||||
} else {
|
||||
alert(`出错啦!${data.errMsg} (错误码: ${data.errCode}) `);
|
||||
location.replace("/login");
|
||||
}
|
||||
}).catch(function (error) {
|
||||
console.log(error);
|
||||
document.getElementById("displayText").innerHTML="系统错误,请稍后再试!";
|
||||
document.getElementById("displayText").style.color="red";
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
@@ -13,7 +13,7 @@
|
||||
<br>
|
||||
<a href="/status">网站状态检测</a>
|
||||
·
|
||||
<a class="narrowHide" href="/about">关于</a>
|
||||
<a href="/about">关于</a>
|
||||
</small>
|
||||
</p>
|
||||
</div>
|
||||
|
@@ -13,7 +13,4 @@
|
||||
// API地址
|
||||
const APIHOST = '<%= global.site.api.prefix %>';
|
||||
axios.defaults.baseURL = APIHOST;
|
||||
|
||||
// 字体加载前先隐藏,不然文字会闪一下
|
||||
$("html,body").css("opacity", "0");
|
||||
</script>
|
@@ -11,7 +11,7 @@
|
||||
Target Server Version : 50726
|
||||
File Encoding : 65001
|
||||
|
||||
Date: 04/04/2022 14:26:48
|
||||
Date: 04/04/2022 17:06:31
|
||||
*/
|
||||
|
||||
SET NAMES utf8mb4;
|
||||
@@ -33,7 +33,7 @@ CREATE TABLE `book_info` (
|
||||
`thumbnail` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '缩略图',
|
||||
`author` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '作者',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 12 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 12 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of book_info
|
||||
@@ -63,7 +63,7 @@ CREATE TABLE `category_info` (
|
||||
`level` int(11) NOT NULL COMMENT '分类层级(一、二、三级)',
|
||||
`parent_id` int(11) NOT NULL DEFAULT 0 COMMENT '父分类ID',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 44 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 44 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of category_info
|
||||
@@ -134,7 +134,7 @@ CREATE TABLE `file_info` (
|
||||
`hash_sha1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
|
||||
`hash_sha256` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of file_info
|
||||
@@ -153,7 +153,7 @@ CREATE TABLE `file_object_info` (
|
||||
`file_share_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
|
||||
`additional_fields` json NULL,
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of file_object_info
|
||||
@@ -164,10 +164,12 @@ CREATE TABLE `file_object_info` (
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `third_party_user_auth_relation`;
|
||||
CREATE TABLE `third_party_user_auth_relation` (
|
||||
`id` int(11) NOT NULL COMMENT '主键',
|
||||
`user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '系统用户ID',
|
||||
`third_party_user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '社会化用户ID'
|
||||
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
|
||||
`user_id` int(11) NOT NULL COMMENT '系统用户ID',
|
||||
`third_party_user_id` int(11) NOT NULL COMMENT '社会化用户ID',
|
||||
PRIMARY KEY (`id`) USING BTREE,
|
||||
UNIQUE INDEX `relation`(`user_id`, `third_party_user_id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = DYNAMIC;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of third_party_user_auth_relation
|
||||
@@ -197,7 +199,7 @@ CREATE TABLE `third_party_user_info` (
|
||||
`oauth_token` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT 'Twitter平台用户的附带属性',
|
||||
`oauth_token_secret` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT 'Twitter平台用户的附带属性',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = 'refer: https://justauth.wiki/features/integrate-existing-systems' ROW_FORMAT = Dynamic;
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = 'refer: https://justauth.wiki/features/integrate-existing-systems' ROW_FORMAT = DYNAMIC;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of third_party_user_info
|
||||
@@ -219,7 +221,7 @@ CREATE TABLE `user_info` (
|
||||
`weixin_third_party_auth_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
|
||||
`qq_third_party_auth_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = DYNAMIC;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of user_info
|
||||
|
@@ -13,7 +13,11 @@ public enum BusinessErrorCode implements CommonError {
|
||||
USER_ALREADY_EXIST(20005, "用户已存在"),
|
||||
|
||||
// 30000开头为权限相关错误定义
|
||||
OPERATION_NOT_ALLOWED(30001, "用户没有此操作的权限");
|
||||
OPERATION_NOT_ALLOWED(30001, "用户没有此操作的权限"),
|
||||
|
||||
// 40000开头为第三方登录相关错误定义
|
||||
THIRD_PARTY_LOGIN_FAIL(40001, "第三方登录失败");
|
||||
|
||||
|
||||
|
||||
private BusinessErrorCode(int errCode, String errMsg) {
|
||||
|
@@ -13,9 +13,9 @@ public class BusinessException extends Exception implements CommonError {
|
||||
}
|
||||
|
||||
// 接收自定义errMsg的方式构造业务异常
|
||||
public BusinessException(CommonError commonError,String errMsg){
|
||||
public BusinessException(CommonError commonError, String errMsg) {
|
||||
super();
|
||||
this.commonError=commonError;
|
||||
this.commonError = commonError;
|
||||
this.commonError.setErrMsg(errMsg);
|
||||
}
|
||||
|
||||
|
@@ -3,10 +3,13 @@ package plus.bookshelf.Controller.Controller;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthCallback;
|
||||
import me.zhyd.oauth.model.AuthResponse;
|
||||
import me.zhyd.oauth.request.AuthGiteeRequest;
|
||||
import me.zhyd.oauth.request.AuthRequest;
|
||||
import me.zhyd.oauth.utils.AuthStateUtils;
|
||||
import org.apache.tomcat.jni.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -14,6 +17,16 @@ import plus.bookshelf.Common.Error.BusinessErrorCode;
|
||||
import plus.bookshelf.Common.Error.BusinessException;
|
||||
import plus.bookshelf.Common.Response.CommonReturnType;
|
||||
import plus.bookshelf.Common.ThirdParty.ThirdPartyConfig;
|
||||
import plus.bookshelf.Controller.VO.UserVO;
|
||||
import plus.bookshelf.Dao.DO.ThirdPartyUserAuthDO;
|
||||
import plus.bookshelf.Dao.DO.ThirdPartyUserDO;
|
||||
import plus.bookshelf.Dao.Mapper.ThirdPartyUserAuthDOMapper;
|
||||
import plus.bookshelf.Dao.Mapper.ThirdPartyUserDOMapper;
|
||||
import plus.bookshelf.Service.Impl.ThirdPartyUserServiceImpl;
|
||||
import plus.bookshelf.Service.Model.UserModel;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@Api(tags = "第三方登录")
|
||||
@Controller
|
||||
@@ -28,6 +41,9 @@ public class ThirdPartyController extends BaseController {
|
||||
@Autowired
|
||||
private ThirdPartyConfig thirdPartyConfig;
|
||||
|
||||
@Autowired
|
||||
ThirdPartyUserServiceImpl thirdPartyUserService;
|
||||
|
||||
@ApiOperation(value = "第三方用户登录跳转地址", notes = "传入需要登录的第三方平台(大小写均可),返回跳转url")
|
||||
@RequestMapping(value = "login", method = {RequestMethod.GET})
|
||||
@ResponseBody
|
||||
@@ -40,15 +56,26 @@ public class ThirdPartyController extends BaseController {
|
||||
@ApiOperation(value = "快捷登录回调函数", notes = "传入 code 值,进行登录")
|
||||
@RequestMapping(value = "callback/{platform}", method = {RequestMethod.GET})
|
||||
@ResponseBody
|
||||
public CommonReturnType qq(@PathVariable("platform") String platform,
|
||||
// @RequestParam Map<String,String> params,
|
||||
AuthCallback callback) throws BusinessException {
|
||||
// System.out.println(params);
|
||||
// System.out.println(platform);
|
||||
// System.out.println(params.get("code"));
|
||||
// System.out.println(params.get("state"));
|
||||
public CommonReturnType callback(@PathVariable("platform") String platform,
|
||||
// @RequestParam Map<String,String> params,
|
||||
AuthCallback callback) throws BusinessException {
|
||||
AuthRequest authRequest = getAuthRequest(platform);
|
||||
return CommonReturnType.create(authRequest.login(callback));
|
||||
AuthResponse authResponse;
|
||||
try {
|
||||
authResponse = authRequest.login(callback);
|
||||
} catch (AuthException e) {
|
||||
// [ERROR] - Failed to login with oauth authorization.
|
||||
throw new BusinessException(BusinessErrorCode.THIRD_PARTY_LOGIN_FAIL, "第三方登录失败");
|
||||
}
|
||||
|
||||
UserModel userModel = thirdPartyUserService.loginCallback(authResponse);
|
||||
UserVO userVO = UserController.convertFromService(userModel);
|
||||
|
||||
if (userModel != null) {
|
||||
String token = onLogin(userModel);
|
||||
userVO.setToken(token); // token 仅在用户登录时传一次,后面获取用户状态接口中不重复返回 token 信息
|
||||
}
|
||||
return CommonReturnType.create(userVO);
|
||||
}
|
||||
|
||||
// 创建授权request
|
||||
|
@@ -91,7 +91,7 @@ public class UserController extends BaseController {
|
||||
return CommonReturnType.create(userVO);
|
||||
}
|
||||
|
||||
private UserVO convertFromService(UserModel userModel) {
|
||||
public static UserVO convertFromService(UserModel userModel) {
|
||||
if (userModel == null) {
|
||||
return null;
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ public class ThirdPartyUserAuthDO {
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
private String userId;
|
||||
private Integer userId;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -26,7 +26,7 @@ public class ThirdPartyUserAuthDO {
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
private String thirdPartyUserId;
|
||||
private Integer thirdPartyUserId;
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
@@ -60,7 +60,7 @@ public class ThirdPartyUserAuthDO {
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public String getUserId() {
|
||||
public Integer getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
@@ -72,8 +72,8 @@ public class ThirdPartyUserAuthDO {
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId == null ? null : userId.trim();
|
||||
public void setUserId(Integer userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,7 +84,7 @@ public class ThirdPartyUserAuthDO {
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public String getThirdPartyUserId() {
|
||||
public Integer getThirdPartyUserId() {
|
||||
return thirdPartyUserId;
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ public class ThirdPartyUserAuthDO {
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public void setThirdPartyUserId(String thirdPartyUserId) {
|
||||
this.thirdPartyUserId = thirdPartyUserId == null ? null : thirdPartyUserId.trim();
|
||||
public void setThirdPartyUserId(Integer thirdPartyUserId) {
|
||||
this.thirdPartyUserId = thirdPartyUserId;
|
||||
}
|
||||
}
|
@@ -5,6 +5,14 @@ import plus.bookshelf.Dao.DO.ThirdPartyUserAuthDO;
|
||||
|
||||
@Repository // 添加这个注解,Autowired的时候idea就不会报错了
|
||||
public interface ThirdPartyUserAuthDOMapper {
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table third_party_user_auth_relation
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
int deleteByPrimaryKey(Integer id);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table third_party_user_auth_relation
|
||||
@@ -20,4 +28,44 @@ public interface ThirdPartyUserAuthDOMapper {
|
||||
* @mbg.generated
|
||||
*/
|
||||
int insertSelective(ThirdPartyUserAuthDO record);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table third_party_user_auth_relation
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
ThirdPartyUserAuthDO selectByPrimaryKey(Integer id);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table third_party_user_auth_relation
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
int updateByPrimaryKeySelective(ThirdPartyUserAuthDO record);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table third_party_user_auth_relation
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
int updateByPrimaryKey(ThirdPartyUserAuthDO record);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table third_party_user_auth_relation
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
ThirdPartyUserAuthDO selectByThirdPartyUserId(Integer id);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table third_party_user_auth_relation
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
ThirdPartyUserAuthDO selectByUserId(Integer id);
|
||||
}
|
@@ -1,6 +1,7 @@
|
||||
package plus.bookshelf.Dao.Mapper;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import plus.bookshelf.Dao.DO.ThirdPartyUserAuthDO;
|
||||
import plus.bookshelf.Dao.DO.ThirdPartyUserDO;
|
||||
|
||||
@Repository // 添加这个注解,Autowired的时候idea就不会报错了
|
||||
@@ -52,4 +53,12 @@ public interface ThirdPartyUserDOMapper {
|
||||
* @mbg.generated
|
||||
*/
|
||||
int updateByPrimaryKey(ThirdPartyUserDO record);
|
||||
|
||||
/**
|
||||
* 通过 uuid + source 查询第三方登录的用户信息(limit 1)
|
||||
* @param uuid
|
||||
* @param source
|
||||
* @return
|
||||
*/
|
||||
ThirdPartyUserDO selectByUuidAndSource(String uuid, String source);
|
||||
}
|
@@ -0,0 +1,174 @@
|
||||
package plus.bookshelf.Service.Impl;
|
||||
|
||||
import me.zhyd.oauth.enums.AuthUserGender;
|
||||
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.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import plus.bookshelf.Common.Error.BusinessErrorCode;
|
||||
import plus.bookshelf.Common.Error.BusinessException;
|
||||
import plus.bookshelf.Dao.DO.ThirdPartyUserAuthDO;
|
||||
import plus.bookshelf.Dao.DO.ThirdPartyUserDO;
|
||||
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 java.util.Map;
|
||||
|
||||
@Service
|
||||
public class ThirdPartyUserServiceImpl implements ThirdPartyUserService {
|
||||
|
||||
@Autowired
|
||||
ThirdPartyUserDOMapper thirdPartyUserDOMapper;
|
||||
|
||||
@Autowired
|
||||
ThirdPartyUserAuthDOMapper thirdPartyUserAuthDOMapper;
|
||||
|
||||
@Autowired
|
||||
UserServiceImpl userService;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public UserModel loginCallback(AuthResponse authResponse) 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) {
|
||||
// 之前已经授权登录过
|
||||
// 更新数据库中的第三方登录信息
|
||||
currentThirdPartyUserDO.setId(existThirdPartyUserDO.getId());
|
||||
thirdPartyUserDOMapper.updateByPrimaryKeySelective(currentThirdPartyUserDO);
|
||||
|
||||
// 检查第三方账号有无绑定到系统账号
|
||||
ThirdPartyUserAuthDO thirdPartyUserAuthDO = thirdPartyUserAuthDOMapper.selectByThirdPartyUserId(currentThirdPartyUserDO.getId());
|
||||
if (thirdPartyUserAuthDO != null) {
|
||||
// 已经绑定到系统账号
|
||||
// 取得用户信息,并登录
|
||||
Integer userId = thirdPartyUserAuthDO.getUserId();
|
||||
UserModel userModel = userService.getUserById(userId);
|
||||
return userModel;
|
||||
} else {
|
||||
// 未绑定到系统账号
|
||||
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "第三方登录失败,该第三方账号未绑定到系统账号,请先绑定");
|
||||
}
|
||||
} else {
|
||||
// 之前未授权登录过
|
||||
// 将新的用户信息插入到数据库
|
||||
thirdPartyUserDOMapper.insertSelective(currentThirdPartyUserDO);
|
||||
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "第三方登录失败,该第三方账号未绑定到系统账号,请先绑定");
|
||||
}
|
||||
} else {
|
||||
// 未知错误
|
||||
throw new BusinessException(BusinessErrorCode.UNKNOWN_ERROR, "未知错误,登录失败");
|
||||
}
|
||||
}
|
||||
|
||||
private ThirdPartyUserDO getThirdPartyUserDOFromAuthData(Object authData) {
|
||||
AuthUser data = (AuthUser) authData;
|
||||
String uuid = data.getUuid();
|
||||
String username = data.getUsername();
|
||||
String nickname = data.getNickname();
|
||||
String avatar = data.getAvatar();
|
||||
String blog = data.getBlog();
|
||||
String company = data.getCompany();
|
||||
String location = data.getLocation();
|
||||
String email = data.getEmail();
|
||||
String remark = data.getRemark();
|
||||
AuthUserGender gender = data.getGender();
|
||||
String source = data.getSource();
|
||||
|
||||
AuthToken token = data.getToken();
|
||||
|
||||
String accessToken = token.getAccessToken();
|
||||
int expiresIn = token.getExpireIn();
|
||||
String refreshToken = token.getRefreshToken();
|
||||
int refreshTokenExpireIn = token.getRefreshTokenExpireIn();
|
||||
String uid = token.getUid();
|
||||
String openId = token.getOpenId();
|
||||
String accessCode = token.getAccessCode();
|
||||
String unionId = token.getUnionId();
|
||||
String scope = token.getScope();
|
||||
String tokenType = token.getTokenType();
|
||||
String idToken = token.getIdToken();
|
||||
String macAlgorithm = token.getMacAlgorithm();
|
||||
String macKey = token.getMacKey();
|
||||
String code = token.getCode();
|
||||
String oauthToken = token.getOauthToken();
|
||||
String oauthTokenSecret = token.getOauthTokenSecret();
|
||||
String userId = token.getUserId();
|
||||
String screenName = token.getScreenName();
|
||||
String oauthCallbackConfirmed = token.getOauthToken();
|
||||
|
||||
// String uuid = data.get("uuid").toString();
|
||||
// String username = data.get("username").toString();
|
||||
// String nickname = data.get("nickname").toString();
|
||||
// String avatar = data.get("avatar").toString();
|
||||
// String blog = data.get("blog").toString();
|
||||
// String company = data.get("company").toString();
|
||||
// String location = data.get("location").toString();
|
||||
// String email = data.get("email").toString();
|
||||
// String remark = data.get("remark").toString();
|
||||
// String gender = data.get("gender").toString();
|
||||
// String source = data.get("source").toString();
|
||||
|
||||
// Map token = (Map) data.get("token");
|
||||
// String accessToken = token.get("accessToken").toString();
|
||||
// int expiresIn = Integer.parseInt(token.get("expiresIn").toString());
|
||||
// String refreshToken = token.get("refreshToken").toString();
|
||||
// int refreshTokenExpireIn = Integer.parseInt(token.get("refreshTokenExpireIn").toString());
|
||||
// String uid = token.get("uid").toString();
|
||||
// String openId = token.get("openId").toString();
|
||||
// String accessCode = token.get("accessCode").toString();
|
||||
// String unionId = token.get("unionId").toString();
|
||||
// String scope = token.get("scope").toString();
|
||||
// String tokenType = token.get("tokenType").toString();// token 类型,默认为 bearer
|
||||
// String idToken = token.get("idToken").toString();// id_token
|
||||
// String macAlgorithm = token.get("macAlgorithm").toString();// mac 算法
|
||||
// String macKey = token.get("macKey").toString();// mac 密钥
|
||||
// String code = token.get("code").toString();// code
|
||||
// String oauthToken = token.get("oauthToken").toString();// oauth_token
|
||||
// String oauthTokenSecret = token.get("oauthTokenSecret").toString();// oauth_token_secret
|
||||
// String userId = token.get("userId").toString();// user_id
|
||||
// String screenName = token.get("screenName").toString();// screen_name
|
||||
// String oauthCallbackConfirmed = token.get("oauthCallbackConfirmed").toString();// oauth_callback_confirmed
|
||||
|
||||
ThirdPartyUserDO thirdPartyUserDO = new ThirdPartyUserDO();
|
||||
thirdPartyUserDO.setUuid(uuid);
|
||||
thirdPartyUserDO.setSource(source);
|
||||
thirdPartyUserDO.setAccessToken(accessToken);
|
||||
thirdPartyUserDO.setExpireIn(expiresIn);
|
||||
thirdPartyUserDO.setRefreshToken(refreshToken);
|
||||
thirdPartyUserDO.setOpenId(openId);
|
||||
thirdPartyUserDO.setUid(uid);
|
||||
thirdPartyUserDO.setAccessCode(accessCode);
|
||||
thirdPartyUserDO.setUnionId(unionId);
|
||||
thirdPartyUserDO.setScope(scope);
|
||||
thirdPartyUserDO.setTokenType(tokenType);
|
||||
thirdPartyUserDO.setIdToken(idToken);
|
||||
thirdPartyUserDO.setMacAlgorithm(macAlgorithm);
|
||||
thirdPartyUserDO.setMacKey(macKey);
|
||||
thirdPartyUserDO.setCode(code);
|
||||
thirdPartyUserDO.setOauthToken(oauthToken);
|
||||
thirdPartyUserDO.setOauthTokenSecret(oauthTokenSecret);
|
||||
|
||||
return thirdPartyUserDO;
|
||||
}
|
||||
}
|
@@ -8,11 +8,14 @@ import plus.bookshelf.Common.Error.BusinessErrorCode;
|
||||
import plus.bookshelf.Common.Error.BusinessException;
|
||||
import plus.bookshelf.Common.SessionManager.RedisSessionManager;
|
||||
import plus.bookshelf.Controller.VO.UserVO;
|
||||
import plus.bookshelf.Dao.DO.ThirdPartyUserDO;
|
||||
import plus.bookshelf.Dao.DO.UserDO;
|
||||
import plus.bookshelf.Dao.Mapper.UserDOMapper;
|
||||
import plus.bookshelf.Service.Model.UserModel;
|
||||
import plus.bookshelf.Service.Service.UserService;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
|
@@ -0,0 +1,13 @@
|
||||
package plus.bookshelf.Service.Model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ThirdPartyUserAuthModel {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String userId;
|
||||
|
||||
private String thirdPartyUserId;
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
package plus.bookshelf.Service.Model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ThirdPartyUserModel {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String uuid;
|
||||
|
||||
private String source;
|
||||
|
||||
private String accessToken;
|
||||
|
||||
private Integer expireIn;
|
||||
|
||||
private String refreshToken;
|
||||
|
||||
private String openId;
|
||||
|
||||
private String uid;
|
||||
|
||||
private String accessCode;
|
||||
|
||||
private String unionId;
|
||||
|
||||
private String scope;
|
||||
|
||||
private String tokenType;
|
||||
|
||||
private String idToken;
|
||||
|
||||
private String macAlgorithm;
|
||||
|
||||
private String macKey;
|
||||
|
||||
private String code;
|
||||
|
||||
private String oauthToken;
|
||||
|
||||
private String oauthTokenSecret;
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
package plus.bookshelf.Service.Service;
|
||||
|
||||
import me.zhyd.oauth.model.AuthResponse;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import plus.bookshelf.Common.Error.BusinessException;
|
||||
import plus.bookshelf.Service.Model.UserModel;
|
||||
|
||||
public interface ThirdPartyUserService {
|
||||
|
||||
/**
|
||||
* 第三方登录完成回调函数
|
||||
* @param authResponse
|
||||
* @throws BusinessException
|
||||
* @return
|
||||
*/
|
||||
@Transactional
|
||||
UserModel loginCallback(AuthResponse authResponse) throws BusinessException;
|
||||
}
|
@@ -6,10 +6,35 @@
|
||||
WARNING - @mbg.generated
|
||||
This element is automatically generated by MyBatis Generator, do not modify.
|
||||
-->
|
||||
<result column="id" jdbcType="INTEGER" property="id" />
|
||||
<result column="user_id" jdbcType="VARCHAR" property="userId" />
|
||||
<result column="third_party_user_id" jdbcType="VARCHAR" property="thirdPartyUserId" />
|
||||
<id column="id" jdbcType="INTEGER" property="id" />
|
||||
<result column="user_id" jdbcType="INTEGER" property="userId" />
|
||||
<result column="third_party_user_id" jdbcType="INTEGER" property="thirdPartyUserId" />
|
||||
</resultMap>
|
||||
<sql id="Base_Column_List">
|
||||
<!--
|
||||
WARNING - @mbg.generated
|
||||
This element is automatically generated by MyBatis Generator, do not modify.
|
||||
-->
|
||||
id, user_id, third_party_user_id
|
||||
</sql>
|
||||
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
|
||||
<!--
|
||||
WARNING - @mbg.generated
|
||||
This element is automatically generated by MyBatis Generator, do not modify.
|
||||
-->
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from third_party_user_auth_relation
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</select>
|
||||
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
|
||||
<!--
|
||||
WARNING - @mbg.generated
|
||||
This element is automatically generated by MyBatis Generator, do not modify.
|
||||
-->
|
||||
delete from third_party_user_auth_relation
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</delete>
|
||||
<insert id="insert" parameterType="plus.bookshelf.Dao.DO.ThirdPartyUserAuthDO">
|
||||
<!--
|
||||
WARNING - @mbg.generated
|
||||
@@ -17,7 +42,7 @@
|
||||
-->
|
||||
insert into third_party_user_auth_relation (id, user_id, third_party_user_id
|
||||
)
|
||||
values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=VARCHAR}, #{thirdPartyUserId,jdbcType=VARCHAR}
|
||||
values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{thirdPartyUserId,jdbcType=INTEGER}
|
||||
)
|
||||
</insert>
|
||||
<insert id="insertSelective" parameterType="plus.bookshelf.Dao.DO.ThirdPartyUserAuthDO">
|
||||
@@ -42,11 +67,49 @@
|
||||
#{id,jdbcType=INTEGER},
|
||||
</if>
|
||||
<if test="userId != null">
|
||||
#{userId,jdbcType=VARCHAR},
|
||||
#{userId,jdbcType=INTEGER},
|
||||
</if>
|
||||
<if test="thirdPartyUserId != null">
|
||||
#{thirdPartyUserId,jdbcType=VARCHAR},
|
||||
#{thirdPartyUserId,jdbcType=INTEGER},
|
||||
</if>
|
||||
</trim>
|
||||
</insert>
|
||||
<update id="updateByPrimaryKeySelective" parameterType="plus.bookshelf.Dao.DO.ThirdPartyUserAuthDO">
|
||||
<!--
|
||||
WARNING - @mbg.generated
|
||||
This element is automatically generated by MyBatis Generator, do not modify.
|
||||
-->
|
||||
update third_party_user_auth_relation
|
||||
<set>
|
||||
<if test="userId != null">
|
||||
user_id = #{userId,jdbcType=INTEGER},
|
||||
</if>
|
||||
<if test="thirdPartyUserId != null">
|
||||
third_party_user_id = #{thirdPartyUserId,jdbcType=INTEGER},
|
||||
</if>
|
||||
</set>
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</update>
|
||||
<update id="updateByPrimaryKey" parameterType="plus.bookshelf.Dao.DO.ThirdPartyUserAuthDO">
|
||||
<!--
|
||||
WARNING - @mbg.generated
|
||||
This element is automatically generated by MyBatis Generator, do not modify.
|
||||
-->
|
||||
update third_party_user_auth_relation
|
||||
set user_id = #{userId,jdbcType=INTEGER},
|
||||
third_party_user_id = #{thirdPartyUserId,jdbcType=INTEGER}
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</update>
|
||||
<select id="selectByThirdPartyUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from third_party_user_auth_relation
|
||||
where third_party_user_id = #{thirdPartyUserId,jdbcType=INTEGER}
|
||||
</select>
|
||||
<select id="selectByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from third_party_user_auth_relation
|
||||
where user_id = #{userId,jdbcType=INTEGER}
|
||||
</select>
|
||||
</mapper>
|
@@ -277,4 +277,10 @@
|
||||
oauth_token_secret = #{oauthTokenSecret,jdbcType=VARCHAR}
|
||||
where id = #{id,jdbcType=INTEGER}
|
||||
</update>
|
||||
<select id="selectByUuidAndSource" resultMap="BaseResultMap">
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from third_party_user_info
|
||||
where uuid = #{uuid} and `source` = #{source}
|
||||
</select>
|
||||
</mapper>
|
Reference in New Issue
Block a user