|
|
@@ -43,6 +43,9 @@ import vip.xiaonuo.auth.modular.third.mapper.AuthThirdMapper;
|
|
|
import vip.xiaonuo.auth.modular.third.param.AuthThirdCallbackParam;
|
|
|
import vip.xiaonuo.auth.modular.third.param.AuthThirdRenderParam;
|
|
|
import vip.xiaonuo.auth.modular.third.param.AuthThirdUserPageParam;
|
|
|
+import vip.xiaonuo.auth.modular.third.request.JfcloudCustomAuthRequest;
|
|
|
+import vip.xiaonuo.auth.modular.third.request.config.JfcloudAuthSource;
|
|
|
+import vip.xiaonuo.auth.modular.third.request.model.JfcloudAuthUser;
|
|
|
import vip.xiaonuo.auth.modular.third.result.AuthThirdRenderResult;
|
|
|
import vip.xiaonuo.auth.modular.third.service.AuthThirdService;
|
|
|
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
|
|
|
@@ -67,6 +70,12 @@ public class AuthThirdServiceImpl extends ServiceImpl<AuthThirdMapper, AuthThird
|
|
|
private static final String SNOWY_THIRD_WECHAT_CLIENT_SECRET_KEY = "SNOWY_THIRD_WECHAT_CLIENT_SECRET";
|
|
|
private static final String SNOWY_THIRD_WECHAT_REDIRECT_URL_KEY = "SNOWY_THIRD_WECHAT_REDIRECT_URL";
|
|
|
|
|
|
+ // OAUTH_第三方客户端信息
|
|
|
+ private static final String OAUTH_THIRD_CLIENT_ID_KEY = "OAUTH_THIRD_CLIENT_ID";
|
|
|
+ private static final String OAUTH_THIRD_CLIENT_SECRET_KEY = "OAUTH_THIRD_CLIENT_SECRET";
|
|
|
+ private static final String OAUTH_THIRD_REDIRECT_URL_KEY = "OAUTH_THIRD_REDIRECT_URL";
|
|
|
+
|
|
|
+
|
|
|
@Resource
|
|
|
private DevConfigApi devConfigApi;
|
|
|
|
|
|
@@ -81,22 +90,16 @@ public class AuthThirdServiceImpl extends ServiceImpl<AuthThirdMapper, AuthThird
|
|
|
|
|
|
@Override
|
|
|
public AuthThirdRenderResult render(AuthThirdRenderParam authThirdRenderParam) {
|
|
|
-
|
|
|
// 获取请求
|
|
|
AuthRequest authRequest = this.getAuthRequest(authThirdRenderParam.getPlatform());
|
|
|
-
|
|
|
// 获取状态
|
|
|
String state = AuthStateUtils.createState();
|
|
|
-
|
|
|
// 构造授权地址
|
|
|
String authorizeUrl = authRequest.authorize(state);
|
|
|
-
|
|
|
// 构造结果
|
|
|
AuthThirdRenderResult authThirdRenderResult = new AuthThirdRenderResult();
|
|
|
-
|
|
|
// 返回授权地址
|
|
|
authThirdRenderResult.setAuthorizeUrl(authorizeUrl);
|
|
|
-
|
|
|
// 返回状态码
|
|
|
authThirdRenderResult.setState(state);
|
|
|
return authThirdRenderResult;
|
|
|
@@ -106,40 +109,36 @@ public class AuthThirdServiceImpl extends ServiceImpl<AuthThirdMapper, AuthThird
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
@Override
|
|
|
public String callback(AuthThirdCallbackParam authThirdCallbackParam, AuthCallback authCallback) {
|
|
|
-
|
|
|
- // 获取请求
|
|
|
- AuthRequest authRequest = this.getAuthRequest(authThirdCallbackParam.getPlatform());
|
|
|
-
|
|
|
- // 执行请求
|
|
|
- AuthResponse<AuthUser> authResponse = authRequest.login(authCallback);
|
|
|
- if (authResponse.ok()) {
|
|
|
-
|
|
|
- // 授权的用户信息
|
|
|
- AuthUser authUser = authResponse.getData();
|
|
|
-
|
|
|
- // 获取第三方用户id
|
|
|
- String uuid = authUser.getUuid();
|
|
|
-
|
|
|
- // 获取第三方用户来源
|
|
|
- String source = authUser.getSource();
|
|
|
-
|
|
|
- // 根据第三方用户id和用户来源获取用户信息
|
|
|
- AuthThirdUser authThirdUser = this.getOne(new LambdaQueryWrapper<AuthThirdUser>().eq(AuthThirdUser::getThirdId, uuid)
|
|
|
- .eq(AuthThirdUser::getCategory, source));
|
|
|
-
|
|
|
- // 定义系统用户id
|
|
|
- String userId;
|
|
|
- if(ObjectUtil.isEmpty(authThirdUser)) {
|
|
|
-
|
|
|
- // 如果用户不存在,则绑定用户并登录
|
|
|
- userId = this.bindUser(authUser);
|
|
|
+ AuthResponse<AuthUser> authResponse = null;
|
|
|
+ try {
|
|
|
+ // 获取请求
|
|
|
+ AuthRequest authRequest = this.getAuthRequest(authThirdCallbackParam.getPlatform());
|
|
|
+ // 执行请求
|
|
|
+ authResponse = authRequest.login(authCallback);
|
|
|
+ if (authResponse.ok()) {
|
|
|
+ // 授权的用户信息
|
|
|
+ AuthUser authUser = authResponse.getData();
|
|
|
+ // 获取第三方用户id
|
|
|
+ String uuid = authUser.getUuid();
|
|
|
+ // 获取第三方用户来源
|
|
|
+ String source = authUser.getSource();
|
|
|
+ // 根据第三方用户id和用户来源获取用户信息
|
|
|
+ AuthThirdUser authThirdUser = this.getOne(new LambdaQueryWrapper<AuthThirdUser>().eq(AuthThirdUser::getThirdId, uuid).eq(AuthThirdUser::getCategory, source));
|
|
|
+ // 定义系统用户id
|
|
|
+ String userId;
|
|
|
+ if (ObjectUtil.isEmpty(authThirdUser)) {
|
|
|
+ // 如果用户不存在,则绑定用户并登录
|
|
|
+ userId = this.bindUser(authUser);
|
|
|
+ } else {
|
|
|
+ // 否则直接获取用户id登录
|
|
|
+ userId = authThirdUser.getUserId();
|
|
|
+ }
|
|
|
+ // TODO 此处使用PC端执行B端登录,返回token
|
|
|
+ return authService.doLoginById(userId, AuthDeviceTypeEnum.PC.getValue(), SaClientTypeEnum.B.getValue());
|
|
|
} else {
|
|
|
- // 否则直接获取用户id登录
|
|
|
- userId = authThirdUser.getUserId();
|
|
|
+ throw new CommonException("第三方登录授权回调失败,原因:{}", authResponse.getMsg());
|
|
|
}
|
|
|
- // TODO 此处使用PC端执行B端登录,返回token
|
|
|
- return authService.doLoginById(userId, AuthDeviceTypeEnum.PC.getValue(), SaClientTypeEnum.B.getValue());
|
|
|
- } else {
|
|
|
+ } catch (Exception e) {
|
|
|
throw new CommonException("第三方登录授权回调失败,原因:{}", authResponse.getMsg());
|
|
|
}
|
|
|
}
|
|
|
@@ -147,17 +146,15 @@ public class AuthThirdServiceImpl extends ServiceImpl<AuthThirdMapper, AuthThird
|
|
|
@Override
|
|
|
public Page<AuthThirdUser> page(AuthThirdUserPageParam authThirdUserPageParam) {
|
|
|
QueryWrapper<AuthThirdUser> queryWrapper = new QueryWrapper<AuthThirdUser>().checkSqlInjection();
|
|
|
- if(ObjectUtil.isNotEmpty(authThirdUserPageParam.getCategory())) {
|
|
|
+ if (ObjectUtil.isNotEmpty(authThirdUserPageParam.getCategory())) {
|
|
|
queryWrapper.lambda().eq(AuthThirdUser::getCategory, authThirdUserPageParam.getCategory());
|
|
|
}
|
|
|
- if(ObjectUtil.isNotEmpty(authThirdUserPageParam.getSearchKey())) {
|
|
|
- queryWrapper.and(q -> q.lambda().like(AuthThirdUser::getName, authThirdUserPageParam.getSearchKey())
|
|
|
- .or().like(AuthThirdUser::getNickname, authThirdUserPageParam.getSearchKey()));
|
|
|
+ if (ObjectUtil.isNotEmpty(authThirdUserPageParam.getSearchKey())) {
|
|
|
+ queryWrapper.and(q -> q.lambda().like(AuthThirdUser::getName, authThirdUserPageParam.getSearchKey()).or().like(AuthThirdUser::getNickname, authThirdUserPageParam.getSearchKey()));
|
|
|
}
|
|
|
- if(ObjectUtil.isAllNotEmpty(authThirdUserPageParam.getSortField(), authThirdUserPageParam.getSortOrder())) {
|
|
|
+ if (ObjectUtil.isAllNotEmpty(authThirdUserPageParam.getSortField(), authThirdUserPageParam.getSortOrder())) {
|
|
|
CommonSortOrderEnum.validate(authThirdUserPageParam.getSortOrder());
|
|
|
- queryWrapper.orderBy(true, authThirdUserPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
|
|
|
- StrUtil.toUnderlineCase(authThirdUserPageParam.getSortField()));
|
|
|
+ queryWrapper.orderBy(true, authThirdUserPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()), StrUtil.toUnderlineCase(authThirdUserPageParam.getSortField()));
|
|
|
} else {
|
|
|
queryWrapper.lambda().orderByDesc(AuthThirdUser::getCreateTime);
|
|
|
}
|
|
|
@@ -171,10 +168,20 @@ public class AuthThirdServiceImpl extends ServiceImpl<AuthThirdMapper, AuthThird
|
|
|
* @date 2022/7/9 14:58
|
|
|
*/
|
|
|
private String bindUser(AuthUser authUser) {
|
|
|
- // TODO 此处固定绑定超管
|
|
|
- SaBaseLoginUser saBaseLoginUser = loginUserApi.getUserByAccount("admin");
|
|
|
- if(ObjectUtil.isEmpty(saBaseLoginUser)) {
|
|
|
- throw new CommonException("第三方登录失败,无法绑定账号admin,原因:账户admin不存在");
|
|
|
+ String phone = null;
|
|
|
+ if (authUser instanceof JfcloudAuthUser) {
|
|
|
+ phone = ((JfcloudAuthUser) authUser).getPhone();
|
|
|
+ }
|
|
|
+ SaBaseLoginUser saBaseLoginUser = null;
|
|
|
+ if (!ObjectUtil.isEmpty(phone)) {
|
|
|
+ // 根据手机号号码获取用户
|
|
|
+ saBaseLoginUser = loginUserApi.getUserByPhone(phone);
|
|
|
+ } else {
|
|
|
+ // 此处固定绑定超管
|
|
|
+ saBaseLoginUser = loginUserApi.getUserByAccount("superAdmin");
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isEmpty(saBaseLoginUser)) {
|
|
|
+ throw new CommonException("第三方登录失败,无法绑定账号superAdmin,原因:账户superAdmin不存在");
|
|
|
}
|
|
|
AuthThirdUser authThirdUser = new AuthThirdUser();
|
|
|
authThirdUser.setThirdId(authUser.getUuid());
|
|
|
@@ -182,7 +189,10 @@ public class AuthThirdServiceImpl extends ServiceImpl<AuthThirdMapper, AuthThird
|
|
|
authThirdUser.setAvatar(authUser.getAvatar());
|
|
|
authThirdUser.setName(authUser.getUsername());
|
|
|
authThirdUser.setNickname(authUser.getNickname());
|
|
|
- authThirdUser.setGender(authUser.getGender().getDesc());
|
|
|
+ authThirdUser.setEmail(authUser.getEmail());
|
|
|
+ authThirdUser.setPhone(phone);
|
|
|
+ String gender = authUser.getGender() != null ? authUser.getGender().getDesc() : "未知";
|
|
|
+ authThirdUser.setGender(gender);
|
|
|
authThirdUser.setCategory(authUser.getSource());
|
|
|
authThirdUser.setExtJson(JSONUtil.toJsonStr(authUser.getRawUserInfo()));
|
|
|
this.save(authThirdUser);
|
|
|
@@ -192,6 +202,7 @@ public class AuthThirdServiceImpl extends ServiceImpl<AuthThirdMapper, AuthThird
|
|
|
/**
|
|
|
* 创建授权请求
|
|
|
*
|
|
|
+ * @author jackzhou 添加更多认证支持 2025-11-10
|
|
|
* @author xuyuxiang
|
|
|
* @date 2022/7/8 16:48
|
|
|
**/
|
|
|
@@ -202,19 +213,16 @@ public class AuthThirdServiceImpl extends ServiceImpl<AuthThirdMapper, AuthThird
|
|
|
AuthThirdPlatformEnum.validate(source);
|
|
|
if (source.equals(AuthThirdPlatformEnum.GITEE.getValue())) {
|
|
|
// GITEE登录
|
|
|
- authRequest = new AuthGiteeRequest(AuthConfig.builder()
|
|
|
- .clientId(devConfigApi.getValueByKey(SNOWY_THIRD_GITEE_CLIENT_ID_KEY))
|
|
|
- .clientSecret(devConfigApi.getValueByKey(SNOWY_THIRD_GITEE_CLIENT_SECRET_KEY))
|
|
|
- .redirectUri(devConfigApi.getValueByKey(SNOWY_THIRD_GITEE_REDIRECT_URL_KEY))
|
|
|
- .build());
|
|
|
+ authRequest = new AuthGiteeRequest(AuthConfig.builder().clientId(devConfigApi.getValueByKey(SNOWY_THIRD_GITEE_CLIENT_ID_KEY)).clientSecret(devConfigApi.getValueByKey(SNOWY_THIRD_GITEE_CLIENT_SECRET_KEY)).redirectUri(devConfigApi.getValueByKey(SNOWY_THIRD_GITEE_REDIRECT_URL_KEY)).build());
|
|
|
}
|
|
|
- if(source.equals(AuthThirdPlatformEnum.WECHAT.getValue())){
|
|
|
+ if (source.equals(AuthThirdPlatformEnum.WECHAT.getValue())) {
|
|
|
// 微信登录
|
|
|
- authRequest = new AuthWeChatOpenRequest(AuthConfig.builder()
|
|
|
- .clientId(devConfigApi.getValueByKey(SNOWY_THIRD_WECHAT_CLIENT_ID_KEY))
|
|
|
- .clientSecret(devConfigApi.getValueByKey(SNOWY_THIRD_WECHAT_CLIENT_SECRET_KEY))
|
|
|
- .redirectUri(devConfigApi.getValueByKey(SNOWY_THIRD_WECHAT_REDIRECT_URL_KEY))
|
|
|
- .build());
|
|
|
+ authRequest = new AuthWeChatOpenRequest(AuthConfig.builder().clientId(devConfigApi.getValueByKey(SNOWY_THIRD_WECHAT_CLIENT_ID_KEY)).clientSecret(devConfigApi.getValueByKey(SNOWY_THIRD_WECHAT_CLIENT_SECRET_KEY)).redirectUri(devConfigApi.getValueByKey(SNOWY_THIRD_WECHAT_REDIRECT_URL_KEY)).build());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (source.equals(AuthThirdPlatformEnum.OAUTH.getValue())) {
|
|
|
+ //OAUTH
|
|
|
+ authRequest = new JfcloudCustomAuthRequest(AuthConfig.builder().clientId(devConfigApi.getValueByKey(OAUTH_THIRD_CLIENT_ID_KEY)).clientSecret(devConfigApi.getValueByKey(OAUTH_THIRD_CLIENT_SECRET_KEY)).redirectUri(devConfigApi.getValueByKey(OAUTH_THIRD_REDIRECT_URL_KEY)).build(), JfcloudAuthSource.ZHONG_NAN);
|
|
|
}
|
|
|
return authRequest;
|
|
|
}
|