mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-09-24 07:19:46 +08:00
update 优化 !pr370 完成三方登录授权功能
This commit is contained in:
@ -25,7 +25,7 @@ import org.dromara.common.social.utils.SocialUtils;
|
||||
import org.dromara.common.tenant.helper.TenantHelper;
|
||||
import org.dromara.system.domain.bo.SysTenantBo;
|
||||
import org.dromara.system.domain.vo.SysTenantVo;
|
||||
import org.dromara.system.service.ISocialUserService;
|
||||
import org.dromara.system.service.ISysSocialService;
|
||||
import org.dromara.system.service.ISysConfigService;
|
||||
import org.dromara.system.service.ISysTenantService;
|
||||
import org.dromara.web.domain.vo.LoginTenantVo;
|
||||
@ -36,7 +36,6 @@ import org.dromara.web.service.SysRegisterService;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
@ -57,8 +56,7 @@ public class AuthController {
|
||||
private final SysRegisterService registerService;
|
||||
private final ISysConfigService configService;
|
||||
private final ISysTenantService tenantService;
|
||||
private final ISocialUserService socialUserService;
|
||||
|
||||
private final ISysSocialService socialUserService;
|
||||
|
||||
|
||||
/**
|
||||
@ -133,13 +131,14 @@ public class AuthController {
|
||||
|
||||
/**
|
||||
* 认证授权
|
||||
* @param source
|
||||
*
|
||||
* @param source 登录来源
|
||||
* @return 结果
|
||||
*/
|
||||
@GetMapping("/binding/{source}")
|
||||
@ResponseBody
|
||||
public R<LoginVo> authBinding(@PathVariable("source") String source, HttpServletRequest request){
|
||||
public R<String> authBinding(@PathVariable("source") String source) {
|
||||
SocialLoginConfigProperties obj = socialProperties.getType().get(source);
|
||||
if (ObjectUtil.isNull(obj)){
|
||||
if (ObjectUtil.isNull(obj)) {
|
||||
return R.fail(source + "平台账号暂不支持");
|
||||
}
|
||||
AuthRequest authRequest = SocialUtils.getAuthRequest(source,
|
||||
@ -152,16 +151,16 @@ public class AuthController {
|
||||
|
||||
/**
|
||||
* 第三方登录回调业务处理
|
||||
* @param source
|
||||
* @param callback
|
||||
* @param request
|
||||
* @return
|
||||
*
|
||||
* @param source 登录来源
|
||||
* @param callback 授权响应实体
|
||||
* @return 结果
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@GetMapping("/social-login/{source}")
|
||||
public R<String> socialLogin(@PathVariable("source") String source, AuthCallback callback, HttpServletRequest request) throws IOException {
|
||||
public R<String> socialLogin(@PathVariable("source") String source, AuthCallback callback) {
|
||||
SocialLoginConfigProperties obj = socialProperties.getType().get(source);
|
||||
if (ObjectUtil.isNull(obj)){
|
||||
if (ObjectUtil.isNull(obj)) {
|
||||
return R.fail(source + "平台账号暂不支持");
|
||||
}
|
||||
AuthRequest authRequest = SocialUtils.getAuthRequest(source,
|
||||
@ -169,16 +168,16 @@ public class AuthController {
|
||||
obj.getClientSecret(),
|
||||
obj.getRedirectUri());
|
||||
AuthResponse<AuthUser> response = authRequest.login(callback);
|
||||
return loginService.socialLogin(source, response, request);
|
||||
return loginService.socialLogin(source, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消授权
|
||||
* @param socialId
|
||||
*
|
||||
* @param socialId socialId
|
||||
*/
|
||||
@DeleteMapping(value = "/unlock/{socialId}")
|
||||
public R<Void> unlockSocial(@PathVariable Long socialId)
|
||||
{
|
||||
public R<Void> unlockSocial(@PathVariable Long socialId) {
|
||||
Boolean rows = socialUserService.deleteWithValidById(socialId);
|
||||
return rows ? R.ok() : R.fail("取消授权失败");
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhyd.oauth.model.AuthResponse;
|
||||
@ -33,20 +32,18 @@ import org.dromara.common.tenant.exception.TenantException;
|
||||
import org.dromara.common.tenant.helper.TenantHelper;
|
||||
import org.dromara.common.web.config.properties.CaptchaProperties;
|
||||
import org.dromara.system.domain.SysUser;
|
||||
import org.dromara.system.domain.bo.SocialUserBo;
|
||||
import org.dromara.system.domain.vo.SocialUserVo;
|
||||
import org.dromara.system.domain.bo.SysSocialBo;
|
||||
import org.dromara.system.domain.vo.SysSocialVo;
|
||||
import org.dromara.system.domain.vo.SysTenantVo;
|
||||
import org.dromara.system.domain.vo.SysUserVo;
|
||||
import org.dromara.system.mapper.SysUserMapper;
|
||||
import org.dromara.system.service.ISocialUserService;
|
||||
import org.dromara.system.service.ISysSocialService;
|
||||
import org.dromara.system.service.ISysPermissionService;
|
||||
import org.dromara.system.service.ISysTenantService;
|
||||
import org.dromara.system.service.ISysUserService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
@ -63,7 +60,7 @@ import java.util.function.Supplier;
|
||||
public class SysLoginService {
|
||||
|
||||
private final SysUserMapper userMapper;
|
||||
private final ISocialUserService socialUserService;
|
||||
private final ISysSocialService sysSocialService;
|
||||
private final CaptchaProperties captchaProperties;
|
||||
private final ISysPermissionService permissionService;
|
||||
private final ISysTenantService tenantService;
|
||||
@ -170,47 +167,46 @@ public class SysLoginService {
|
||||
*
|
||||
* @param source 登录来源
|
||||
* @param authUser 授权响应实体
|
||||
* @param request Http请求对象
|
||||
* @return 统一响应实体
|
||||
*/
|
||||
public R<String> socialLogin(String source, AuthResponse<AuthUser> authUser, HttpServletRequest request) {
|
||||
public R<String> socialLogin(String source, AuthResponse<AuthUser> authUser) {
|
||||
// 判断授权响应是否成功
|
||||
if (!authUser.ok()) {
|
||||
return R.fail("对不起,授权信息验证不通过,请退出重试!");
|
||||
}
|
||||
AuthUser authUserData = authUser.getData();
|
||||
SocialUserVo user = socialUserService.selectSocialUserByAuthId(authUserData.getSource() + authUserData.getUuid());
|
||||
SysSocialVo user = sysSocialService.selectByAuthId(authUserData.getSource() + authUserData.getUuid());
|
||||
if (ObjectUtil.isNotNull(user)) {
|
||||
//执行登录和记录登录信息操作
|
||||
// 执行登录和记录登录信息操作
|
||||
return loginAndRecord(user.getTenantId(), user.getUserName(), authUserData);
|
||||
} else {
|
||||
// 判断是否已登录
|
||||
if (LoginHelper.getUserId() == null) {
|
||||
return R.fail("授权失败,请先登录才能绑定");
|
||||
}
|
||||
SocialUserBo socialUserBo = new SocialUserBo();
|
||||
socialUserBo.setUserId(LoginHelper.getUserId());
|
||||
socialUserBo.setAuthId(authUserData.getSource() + authUserData.getUuid());
|
||||
socialUserBo.setSource(authUserData.getSource());
|
||||
socialUserBo.setUserName(authUserData.getUsername());
|
||||
socialUserBo.setNickName(authUserData.getNickname());
|
||||
socialUserBo.setAvatar(authUserData.getAvatar());
|
||||
socialUserBo.setOpenId(authUserData.getUuid());
|
||||
BeanUtils.copyProperties(authUserData.getToken(), socialUserBo);
|
||||
SysSocialBo bo = new SysSocialBo();
|
||||
bo.setUserId(LoginHelper.getUserId());
|
||||
bo.setAuthId(authUserData.getSource() + authUserData.getUuid());
|
||||
bo.setSource(authUserData.getSource());
|
||||
bo.setUserName(authUserData.getUsername());
|
||||
bo.setNickName(authUserData.getNickname());
|
||||
bo.setAvatar(authUserData.getAvatar());
|
||||
bo.setOpenId(authUserData.getUuid());
|
||||
BeanUtils.copyProperties(authUserData.getToken(), bo);
|
||||
|
||||
socialUserService.insertByBo(socialUserBo);
|
||||
SysUserVo lodingData = loadUserByUsername(LoginHelper.getTenantId(), LoginHelper.getUsername());
|
||||
//执行登录和记录登录信息操作
|
||||
return loginAndRecord(lodingData.getTenantId(), socialUserBo.getUserName(), authUserData);
|
||||
sysSocialService.insertByBo(bo);
|
||||
SysUserVo sysUser = loadUserByUsername(LoginHelper.getTenantId(), LoginHelper.getUsername());
|
||||
// 执行登录和记录登录信息操作
|
||||
return loginAndRecord(sysUser.getTenantId(), sysUser.getUserName(), authUserData);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行登录和记录登录信息操作
|
||||
*
|
||||
* @param tenantId 租户ID
|
||||
* @param userName 用户名
|
||||
* @param authUser 授权用户信息
|
||||
* @param tenantId 租户ID
|
||||
* @param userName 用户名
|
||||
* @param authUser 授权用户信息
|
||||
* @return 统一响应实体
|
||||
*/
|
||||
private R<String> loginAndRecord(String tenantId, String userName, AuthUser authUser) {
|
||||
|
@ -179,150 +179,63 @@ sms:
|
||||
territory: ap-guangzhou
|
||||
|
||||
|
||||
|
||||
--- # 三方授权
|
||||
justauth:
|
||||
enabled: true
|
||||
type:
|
||||
QQ:
|
||||
qq:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/qq/callback
|
||||
redirect-uri: http://localhost:80/social-login?source=qq
|
||||
union-id: false
|
||||
WEIBO:
|
||||
weibo:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/weibo/callback
|
||||
redirect-uri: http://localhost:80/social-login?source=weibo
|
||||
gitee:
|
||||
client-id: 38eaaa1b77b5e064313057a2f5745ce3a9f3e7686d9bd302c7df2f308ef6db81
|
||||
client-secret: 2e633af8780cb9fe002c4c7291b722db944402e271efb99b062811f52d7da1ff
|
||||
client-id: 914******************98
|
||||
client-secret: 02*****************ac
|
||||
redirect-uri: http://localhost:80/social-login?source=gitee
|
||||
DINGTALK:
|
||||
dingtalk:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/dingtalk/callback
|
||||
BAIDU:
|
||||
redirect-uri: http://localhost:80/social-login?source=dingtalk
|
||||
baidu:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/baidu/callback
|
||||
CSDN:
|
||||
redirect-uri: http://localhost:80/social-login?source=baidu
|
||||
csdn:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/csdn/callback
|
||||
CODING:
|
||||
redirect-uri: http://localhost:80/social-login?source=csdn
|
||||
coding:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/coding/callback
|
||||
redirect-uri: http://localhost:80/social-login?source=coding
|
||||
coding-group-name: xx
|
||||
OSCHINA:
|
||||
oschina:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/oschina/callback
|
||||
ALIPAY:
|
||||
redirect-uri: http://localhost:80/social-login?source=oschina
|
||||
alipay:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/alipay/callback
|
||||
redirect-uri: http://localhost:80/social-login?source=alipay
|
||||
alipay-public-key: MIIB**************DAQAB
|
||||
WECHAT_OPEN:
|
||||
wechat_open:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat_open/callback
|
||||
WECHAT_MP:
|
||||
redirect-uri: http://localhost:80/social-login?source=wechat_open
|
||||
wechat_mp:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat_mp/callback
|
||||
WECHAT_ENTERPRISE:
|
||||
redirect-uri: http://localhost:80/social-login?source=wechat_mp
|
||||
wechat_enterprise:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat_enterprise/callback
|
||||
redirect-uri: http://localhost:80/social-login?source=wechat_enterprise
|
||||
agent-id: 1000002
|
||||
TAOBAO:
|
||||
gitlab:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/taobao/callback
|
||||
GOOGLE:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/google/callback
|
||||
FACEBOOK:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/facebook/callback
|
||||
DOUYIN:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/douyin/callback
|
||||
LINKEDIN:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/linkedin/callback
|
||||
MICROSOFT:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/microsoft/callback
|
||||
MI:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/mi/callback
|
||||
TOUTIAO:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/toutiao/callback
|
||||
TEAMBITION:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/teambition/callback
|
||||
RENREN:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/renren/callback
|
||||
PINTEREST:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/pinterest/callback
|
||||
STACK_OVERFLOW:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/stack_overflow/callback
|
||||
stack-overflow-key: asd*********asd
|
||||
HUAWEI:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/huawei/callback
|
||||
KUJIALE:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/kujiale/callback
|
||||
GITLAB:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/gitlab/callback
|
||||
MEITUAN:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/meituan/callback
|
||||
ELEME:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/eleme/callback
|
||||
TWITTER:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/twitter/callback
|
||||
XMLY:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/xmly/callback
|
||||
# 设备唯一标识ID
|
||||
device-id: xxxxxxxxxxxxxx
|
||||
# 客户端操作系统类型,1-iOS系统,2-Android系统,3-Web
|
||||
client-os-type: 3
|
||||
# 客户端包名,如果 clientOsType 为1或2时必填。对Android客户端是包名,对IOS客户端是Bundle ID
|
||||
#pack-id: xxxx
|
||||
FEISHU:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/feishu/callback
|
||||
JD:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/jd/callback
|
||||
redirect-uri: http://localhost:80/social-login?source=gitlab
|
||||
|
@ -180,3 +180,64 @@ sms:
|
||||
sdkAppId: appid
|
||||
#地域信息默认为 ap-guangzhou 如无特殊改变可不用设置
|
||||
territory: ap-guangzhou
|
||||
|
||||
--- # 三方授权
|
||||
justauth:
|
||||
enabled: true
|
||||
type:
|
||||
qq:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://localhost:80/social-login?source=qq
|
||||
union-id: false
|
||||
weibo:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://localhost:80/social-login?source=weibo
|
||||
gitee:
|
||||
client-id: 914******************98
|
||||
client-secret: 02*****************ac
|
||||
redirect-uri: http://localhost:80/social-login?source=gitee
|
||||
dingtalk:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://localhost:80/social-login?source=dingtalk
|
||||
baidu:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://localhost:80/social-login?source=baidu
|
||||
csdn:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://localhost:80/social-login?source=csdn
|
||||
coding:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://localhost:80/social-login?source=coding
|
||||
coding-group-name: xx
|
||||
oschina:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://localhost:80/social-login?source=oschina
|
||||
alipay:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://localhost:80/social-login?source=alipay
|
||||
alipay-public-key: MIIB**************DAQAB
|
||||
wechat_open:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://localhost:80/social-login?source=wechat_open
|
||||
wechat_mp:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://localhost:80/social-login?source=wechat_mp
|
||||
wechat_enterprise:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://localhost:80/social-login?source=wechat_enterprise
|
||||
agent-id: 1000002
|
||||
gitlab:
|
||||
client-id: 10**********6
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: http://localhost:80/social-login?source=gitlab
|
||||
|
Reference in New Issue
Block a user