优化,第三方授权登录

This commit is contained in:
thiszhc 2023-06-15 00:58:01 +08:00
parent 2d894c1309
commit 76dc239875
17 changed files with 2223 additions and 596 deletions

View File

@ -39,6 +39,7 @@
<mapstruct-plus.lombok.version>0.2.0</mapstruct-plus.lombok.version>
<lombok.version>1.18.26</lombok.version>
<bouncycastle.version>1.72</bouncycastle.version>
<justauth.version>1.16.5</justauth.version>
<!-- 离线IP地址定位库 -->
<ip2region.version>2.7.0</ip2region.version>
@ -111,6 +112,13 @@
<scope>import</scope>
</dependency>
<!-- JustAuth 的依赖配置-->
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>${justauth.version}</version>
</dependency>
<!-- common 的依赖配置-->
<dependency>
<groupId>org.dromara</groupId>

View File

@ -75,6 +75,16 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>ruoyi-common-social</artifactId>
<version>5.1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- skywalking 整合 logback -->
<!-- <dependency>-->

View File

@ -6,11 +6,15 @@ 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;
import me.zhyd.oauth.model.AuthUser;
import org.dromara.common.core.constant.Constants;
import org.dromara.common.core.constant.GlobalConstants;
import org.dromara.common.core.constant.TenantConstants;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.dto.RoleDTO;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.domain.model.XcxLoginUser;
@ -29,14 +33,21 @@ 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.bo.SysUserBo;
import org.dromara.system.domain.vo.SocialUserVo;
import org.dromara.system.domain.vo.SysTenantVo;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.mapper.SocialUserMapper;
import org.dromara.system.mapper.SysUserMapper;
import org.dromara.system.service.ISocialUserService;
import org.dromara.system.service.ISysPermissionService;
import org.dromara.system.service.ISysTenantService;
import org.dromara.system.service.ISysUserService;
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;
@ -53,6 +64,8 @@ import java.util.function.Supplier;
public class SysLoginService {
private final SysUserMapper userMapper;
private final ISocialUserService socialUserService;
private final ISysUserService userService;
private final CaptchaProperties captchaProperties;
private final ISysPermissionService permissionService;
private final ISysTenantService tenantService;
@ -154,6 +167,44 @@ public class SysLoginService {
return StpUtil.getTokenValue();
}
/**
* 社交登录
*
* @param source 登录来源
* @param authUser 授权响应实体
* @param request Http请求对象
* @return 统一响应实体
*/
public R<String> socialLogin(String source, AuthResponse<AuthUser> authUser, HttpServletRequest request) throws IOException {
// 判断授权响应是否成功
if (!authUser.ok()) {
return R.fail("对不起,授权信息验证不通过,请退出重试!");
}
AuthUser authUserData = authUser.getData();
// 查询社交用户信息判断是否已经绑定如果已经绑定则直接登录否则验证是否登录未登录则先登录再绑定
SocialUserVo user = socialUserService.selectSocialUserByAuthId(source + authUserData.getUuid());
if (ObjectUtil.isNotNull(user)) {
checkTenant(user.getTenantId());
SysUserVo dbUser = loadUserByUsername(user.getTenantId(), user.getUserName());
LoginHelper.loginByDevice(buildLoginUser(dbUser), DeviceType.SOCIAL);
recordLogininfor(dbUser.getTenantId(), user.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
recordLoginInfo(user.getUserId());
} else {
// 判断是否已登录
if (LoginHelper.getUserId() == null) {
return R.fail("授权失败,请先登录再绑定");
}
SocialUserBo socialUserBo = new SocialUserBo();
socialUserService.insertByBo(setAuthUserData(authUserData, socialUserBo));
SysUserVo lodingData = loadUserByUsername(LoginHelper.getTenantId(), LoginHelper.getUsername());
checkTenant(lodingData.getTenantId());
LoginHelper.loginByDevice(buildLoginUser(lodingData), DeviceType.SOCIAL);
recordLogininfor(lodingData.getTenantId(), socialUserBo.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
recordLoginInfo(socialUserBo.getUserId());
}
return R.ok(StpUtil.getTokenValue());
}
/**
* 退出登录
*/
@ -235,9 +286,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);
@ -253,9 +304,9 @@ public class SysLoginService {
private SysUserVo loadUserByPhonenumber(String tenantId, String phonenumber) {
SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>()
.select(SysUser::getPhonenumber, SysUser::getStatus)
.eq(TenantHelper.isEnable(), SysUser::getTenantId, tenantId)
.eq(SysUser::getPhonenumber, phonenumber));
.select(SysUser::getPhonenumber, SysUser::getStatus)
.eq(TenantHelper.isEnable(), SysUser::getTenantId, tenantId)
.eq(SysUser::getPhonenumber, phonenumber));
if (ObjectUtil.isNull(user)) {
log.info("登录用户:{} 不存在.", phonenumber);
throw new UserException("user.not.exists", phonenumber);
@ -383,10 +434,35 @@ 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");
}
}
public SocialUserBo setAuthUserData(AuthUser authUserData, SocialUserBo socialUser) {
socialUser.setUserId(LoginHelper.getUserId());
socialUser.setAuthId(authUserData.getSource() + authUserData.getUuid());
socialUser.setSource(authUserData.getSource());
socialUser.setUserName(authUserData.getUsername());
socialUser.setNickName(authUserData.getNickname());
socialUser.setAvatar(authUserData.getAvatar());
socialUser.setEmail(authUserData.getEmail());
socialUser.setOpenId(authUserData.getUuid());
socialUser.setAccessToken(authUserData.getToken().getAccessToken());
socialUser.setExpireIn(authUserData.getToken().getExpireIn());
socialUser.setRefreshToken(authUserData.getToken().getRefreshToken());
socialUser.setAccessCode(authUserData.getToken().getAccessCode());
socialUser.setUnionId(authUserData.getToken().getUnionId());
socialUser.setScope(authUserData.getToken().getScope());
socialUser.setTokenType(authUserData.getToken().getTokenType());
socialUser.setIdToken(authUserData.getToken().getIdToken());
socialUser.setMacAlgorithm(authUserData.getToken().getMacAlgorithm());
socialUser.setMacKey(authUserData.getToken().getMacKey());
socialUser.setCode(authUserData.getToken().getCode());
socialUser.setOauthToken(authUserData.getToken().getOauthToken());
socialUser.setOauthTokenSecret(authUserData.getToken().getOauthTokenSecret());
return socialUser;
}
}

View File

@ -11,6 +11,7 @@
<modules>
<module>ruoyi-common-bom</module>
<module>ruoyi-common-social</module>
<module>ruoyi-common-core</module>
<module>ruoyi-common-doc</module>
<module>ruoyi-common-excel</module>

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.dromara</groupId>
<artifactId>ruoyi-common</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ruoyi-common-social</artifactId>
<description>
ruoyi-common-auth 认证模块
</description>
<dependencies>
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>ruoyi-common-redis</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,24 @@
package org.dromara.common.social.config;
import me.zhyd.oauth.cache.AuthStateCache;
import org.dromara.common.social.config.properties.SocialProperties;
import org.dromara.common.social.utils.AuthRedisStateCache;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
/**
* Social 配置属性
* @author thiszhc
*/
@AutoConfiguration
@EnableConfigurationProperties(SocialProperties.class)
public class SocialConfig {
@Bean
public AuthStateCache redis() {
return new AuthRedisStateCache();
}
}

View File

@ -0,0 +1,39 @@
package org.dromara.common.social.config.properties;
import lombok.Data;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* Social 配置属性
* @author thiszhc
*/
@Data
@Component
@ConfigurationProperties(prefix = "justauth")
public class SocialProperties {
/**
* 是否启用
*/
private boolean enabled;
/**
* 授权类型
*/
private Map<String, ConfigProperties> type;
/**
* 授权过期时间
*/
private long timeout;
/**
* 授权缓存配置
*/
private CacheProperties cache = new CacheProperties();
}

View File

@ -0,0 +1,119 @@
package org.dromara.common.social.utils;
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.request.*;
/**
* 认证授权工具类
*
* @author thiszhc
*/
public class SocialUtils {
public static AuthRequest getAuthRequest(String source,
String clientId,
String clientSecret,
String redirectUri) throws AuthException {
AuthRequest authRequest = null;
switch (source.toLowerCase()) {
case "dingtalk" ->
authRequest = new AuthDingTalkRequest(AuthConfig.builder().
clientId(clientId).
clientSecret(clientSecret)
.redirectUri(redirectUri).
build());
case "baidu" ->
authRequest = new AuthBaiduRequest(AuthConfig.builder().
clientId(clientId).
clientSecret(clientSecret)
.redirectUri(redirectUri).
build());
case "github" ->
authRequest = new AuthGithubRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "gitee" ->
authRequest = new AuthGiteeRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "weibo" ->
authRequest = new AuthWeiboRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "coding" ->
authRequest = new AuthCodingRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "oschina" ->
authRequest = new AuthOschinaRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "alipay" ->
// 支付宝在创建回调地址时不允许使用localhost或者127.0.0.1所以这儿的回调地址使用的局域网内的ip
authRequest = new AuthAlipayRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.alipayPublicKey("").redirectUri(redirectUri).build());
case "qq" ->
authRequest = new AuthQqRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "wechat_open" -> authRequest = new AuthWeChatOpenRequest(AuthConfig.builder().clientId(clientId)
.clientSecret(clientSecret).redirectUri(redirectUri).build());
case "csdn" ->
authRequest = new AuthCsdnRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "taobao" ->
authRequest = new AuthTaobaoRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "douyin" ->
authRequest = new AuthDouyinRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "linkedin" ->
authRequest = new AuthLinkedinRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "microsoft" -> authRequest = new AuthMicrosoftRequest(AuthConfig.builder().clientId(clientId)
.clientSecret(clientSecret).redirectUri(redirectUri).build());
case "mi" ->
authRequest = new AuthMiRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "toutiao" ->
authRequest = new AuthToutiaoRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "teambition" -> authRequest = new AuthTeambitionRequest(AuthConfig.builder().clientId(clientId)
.clientSecret(clientSecret).redirectUri(redirectUri).build());
case "pinterest" -> authRequest = new AuthPinterestRequest(AuthConfig.builder().clientId(clientId)
.clientSecret(clientSecret).redirectUri(redirectUri).build());
case "renren" ->
authRequest = new AuthRenrenRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "stack_overflow" -> authRequest = new AuthStackOverflowRequest(AuthConfig.builder().clientId(clientId)
.clientSecret(clientSecret).redirectUri(redirectUri).stackOverflowKey("").build());
case "huawei" ->
authRequest = new AuthHuaweiRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "wechat_enterprise" ->
authRequest = new AuthWeChatEnterpriseQrcodeRequest(AuthConfig.builder().clientId(clientId)
.clientSecret(clientSecret).redirectUri(redirectUri).agentId("").build());
case "kujiale" ->
authRequest = new AuthKujialeRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "gitlab" ->
authRequest = new AuthGitlabRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "meituan" ->
authRequest = new AuthMeituanRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "eleme" ->
authRequest = new AuthElemeRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "wechat_mp" ->
authRequest = new AuthWeChatMpRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
case "aliyun" ->
authRequest = new AuthAliyunRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret)
.redirectUri(redirectUri).build());
default -> {
}
}
if (null == authRequest) {
throw new AuthException("未获取到有效的Auth配置");
}
return authRequest;
}
}

View File

@ -0,0 +1,105 @@
package org.dromara.system.controller.system;
import java.util.List;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.SocialUserVo;
import org.dromara.system.domain.bo.SocialUserBo;
import org.dromara.system.service.ISocialUserService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
/**
* 社会化关系
*
* @author thiszhc
* @date 2023-06-12
*/
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/socialUser")
public class SocialUserController extends BaseController {
private final ISocialUserService socialUserService;
/**
* 查询社会化关系列表
*/
@SaCheckPermission("system:user:list")
@GetMapping("/list")
public TableDataInfo<SocialUserVo> list(SocialUserBo bo, PageQuery pageQuery) {
return socialUserService.queryPageList(bo, pageQuery);
}
/**
* 导出社会化关系列表
*/
@SaCheckPermission("system:user:export")
@Log(title = "社会化关系", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(SocialUserBo bo, HttpServletResponse response) {
List<SocialUserVo> list = socialUserService.queryList(bo);
ExcelUtil.exportExcel(list, "社会化关系", SocialUserVo.class, response);
}
/**
* 获取社会化关系详细信息
*
* @param id 主键
*/
@SaCheckPermission("system:user:query")
@GetMapping("/{id}")
public R<SocialUserVo> getInfo(@NotNull(message = "主键不能为空")
@PathVariable Long id) {
return R.ok(socialUserService.queryById(id));
}
/**
* 新增社会化关系
*/
@SaCheckPermission("system:user:add")
@Log(title = "社会化关系", businessType = BusinessType.INSERT)
@RepeatSubmit()
@PostMapping()
public R<Void> add(@Validated(AddGroup.class) @RequestBody SocialUserBo bo) {
return toAjax(socialUserService.insertByBo(bo));
}
/**
* 修改社会化关系
*/
@SaCheckPermission("system:user:edit")
@Log(title = "社会化关系", businessType = BusinessType.UPDATE)
@RepeatSubmit()
@PutMapping()
public R<Void> edit(@Validated(EditGroup.class) @RequestBody SocialUserBo bo) {
return toAjax(socialUserService.updateByBo(bo));
}
/**
* 删除社会化关系
*
* @param ids 主键串
*/
@SaCheckPermission("system:user:remove")
@Log(title = "社会化关系", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
public R<Void> remove(@NotEmpty(message = "主键不能为空")
@PathVariable Long[] ids) {
return toAjax(socialUserService.deleteWithValidByIds(List.of(ids), true));
}
}

View File

@ -0,0 +1,136 @@
package org.dromara.system.domain;
import org.dromara.common.mybatis.core.domain.BaseEntity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serial;
/**
* 社会化关系对象 social_user
*
* @author thiszhc
* @date 2023-06-12
*/
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("social_user")
public class SocialUser extends BaseEntity {
@Serial
private static final long serialVersionUID = 1L;
/**
* 主键
*/
@TableId(value = "id")
private Long id;
/**
* 的唯一ID
*/
private String authId;
/**
* 用户来源
*/
private String source;
/**
* 用户的授权令牌
*/
private String accessToken;
/**
* 用户的授权令牌的有效期部分平台可能没有
*/
private int expireIn;
/**
* 刷新令牌部分平台可能没有
*/
private String refreshToken;
/**
* 用户的 open id
*/
private String openId;
/**
* 用户的 ID
*/
private Long userId;
/**
* 授权的第三方账号
*/
private String userName;
/**
* 授权的第三方昵称
*/
private String nickName;
/**
* 授权的第三方邮箱
*/
private String email;
/**
* 授权的第三方头像地址
*/
private String avatar;
/**
* 平台的授权信息部分平台可能没有
*/
private String accessCode;
/**
* 用户的 unionid
*/
private String unionId;
/**
* 授予的权限部分平台可能没有
*/
private String scope;
/**
* 个别平台的授权信息部分平台可能没有
*/
private String tokenType;
/**
* id token部分平台可能没有
*/
private String idToken;
/**
* 小米平台用户的附带属性部分平台可能没有
*/
private String macAlgorithm;
/**
* 小米平台用户的附带属性部分平台可能没有
*/
private String macKey;
/**
* 用户的授权code部分平台可能没有
*/
private String code;
/**
* Twitter平台用户的附带属性部分平台可能没有
*/
private String oauthToken;
/**
* Twitter平台用户的附带属性部分平台可能没有
*/
private String oauthTokenSecret;
}

View File

@ -0,0 +1,141 @@
package org.dromara.system.domain.bo;
import org.dromara.common.tenant.core.TenantEntity;
import org.dromara.system.domain.SocialUser;
import org.dromara.common.mybatis.core.domain.BaseEntity;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import io.github.linpeilie.annotations.AutoMapper;
import lombok.Data;
import lombok.EqualsAndHashCode;
import jakarta.validation.constraints.*;
/**
* 社会化关系业务对象 social_user
*
* @author Lion Li
* @date 2023-06-12
*/
@Data
@EqualsAndHashCode(callSuper = true)
@AutoMapper(target = SocialUser.class, reverseConvertGenerate = false)
public class SocialUserBo extends TenantEntity {
/**
* 主键
*/
@NotNull(message = "主键不能为空", groups = { EditGroup.class })
private Long id;
/**
* 的唯一ID
*/
@NotBlank(message = "的唯一ID不能为空", groups = { AddGroup.class, EditGroup.class })
private String authId;
/**
* 用户来源
*/
@NotBlank(message = "用户来源不能为空", groups = { AddGroup.class, EditGroup.class })
private String source;
/**
* 用户的授权令牌
*/
@NotBlank(message = "用户的授权令牌不能为空", groups = { AddGroup.class, EditGroup.class })
private String accessToken;
/**
* 用户的授权令牌的有效期部分平台可能没有
*/
private int expireIn;
/**
* 刷新令牌部分平台可能没有
*/
private String refreshToken;
/**
* 用户的 open id
*/
@NotBlank(message = "用户的 open id不能为空", groups = { AddGroup.class, EditGroup.class })
private String openId;
/**
* 用户的 ID
*/
@NotBlank(message = "用户的 ID不能为空", groups = { AddGroup.class, EditGroup.class })
private Long userId;
/**
* 平台的授权信息部分平台可能没有
*/
private String accessCode;
/**
* 用户的 unionid
*/
private String unionId;
/**
* 授予的权限部分平台可能没有
*/
private String scope;
/**
* 授权的第三方账号
*/
private String userName;
/**
* 授权的第三方昵称
*/
private String nickName;
/**
* 授权的第三方邮箱
*/
private String email;
/**
* 授权的第三方头像地址
*/
private String avatar;
/**
* 个别平台的授权信息部分平台可能没有
*/
private String tokenType;
/**
* id token部分平台可能没有
*/
private String idToken;
/**
* 小米平台用户的附带属性部分平台可能没有
*/
private String macAlgorithm;
/**
* 小米平台用户的附带属性部分平台可能没有
*/
private String macKey;
/**
* 用户的授权code部分平台可能没有
*/
private String code;
/**
* Twitter平台用户的附带属性部分平台可能没有
*/
private String oauthToken;
/**
* Twitter平台用户的附带属性部分平台可能没有
*/
private String oauthTokenSecret;
}

View File

@ -0,0 +1,162 @@
package org.dromara.system.domain.vo;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import io.github.linpeilie.annotations.AutoMapper;
import lombok.Data;
import org.dromara.common.tenant.core.TenantEntity;
import org.dromara.system.domain.SocialUser;
import java.io.Serial;
/**
* 社会化关系视图对象 social_user
*
* @author thiszhc
* @date 2023-06-12
*/
@Data
@ExcelIgnoreUnannotated
@AutoMapper(target = SocialUser.class)
public class SocialUserVo extends TenantEntity {
@Serial
private static final long serialVersionUID = 1L;
/**
* 主键
*/
@ExcelProperty(value = "主键")
private Long id;
/**
* 的唯一ID
*/
@ExcelProperty(value = "授权UUID")
private String authId;
/**
* 用户来源
*/
@ExcelProperty(value = "用户来源")
private String source;
/**
* 用户的授权令牌
*/
@ExcelProperty(value = "用户的授权令牌")
private String accessToken;
/**
* 用户的授权令牌的有效期部分平台可能没有
*/
@ExcelProperty(value = "用户的授权令牌的有效期,部分平台可能没有")
private int expireIn;
/**
* 刷新令牌部分平台可能没有
*/
@ExcelProperty(value = "刷新令牌,部分平台可能没有")
private String refreshToken;
/**
* 用户的 open id
*/
@ExcelProperty(value = "用户的 open id")
private String openId;
/**
* 用户的 ID
*/
@ExcelProperty(value = "用户的 ID")
private Long userId;
/**
* 授权的第三方账号
*/
@ExcelProperty(value = "授权的第三方账号")
private String userName;
/**
* 授权的第三方昵称
*/
@ExcelProperty(value = "授权的第三方昵称")
private String nickName;
/**
* 授权的第三方邮箱
*/
@ExcelProperty(value = "授权的第三方邮箱")
private String email;
/**
* 授权的第三方头像地址
*/
@ExcelProperty(value = "授权的第三方头像地址")
private String avatar;
/**
* 平台的授权信息部分平台可能没有
*/
@ExcelProperty(value = "平台的授权信息,部分平台可能没有")
private String accessCode;
/**
* 用户的 unionid
*/
@ExcelProperty(value = "用户的 unionid")
private String unionId;
/**
* 授予的权限部分平台可能没有
*/
@ExcelProperty(value = "授予的权限,部分平台可能没有")
private String scope;
/**
* 个别平台的授权信息部分平台可能没有
*/
@ExcelProperty(value = "个别平台的授权信息,部分平台可能没有")
private String tokenType;
/**
* id token部分平台可能没有
*/
@ExcelProperty(value = "id token部分平台可能没有")
private String idToken;
/**
* 小米平台用户的附带属性部分平台可能没有
*/
@ExcelProperty(value = "小米平台用户的附带属性,部分平台可能没有")
private String macAlgorithm;
/**
* 小米平台用户的附带属性部分平台可能没有
*/
@ExcelProperty(value = "小米平台用户的附带属性,部分平台可能没有")
private String macKey;
/**
* 用户的授权code部分平台可能没有
*/
@ExcelProperty(value = "用户的授权code部分平台可能没有")
private String code;
/**
* Twitter平台用户的附带属性部分平台可能没有
*/
@ExcelProperty(value = "Twitter平台用户的附带属性部分平台可能没有")
private String oauthToken;
/**
* Twitter平台用户的附带属性部分平台可能没有
*/
@ExcelProperty(value = "Twitter平台用户的附带属性部分平台可能没有")
private String oauthTokenSecret;
}

View File

@ -0,0 +1,23 @@
package org.dromara.system.mapper;
import org.dromara.system.domain.SocialUser;
import org.dromara.system.domain.bo.SysUserBo;
import org.dromara.system.domain.vo.SocialUserVo;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;
/**
* 社会化关系Mapper接口
*
* @author thiszhc
* @date 2023-06-12
*/
public interface SocialUserMapper extends BaseMapperPlus<SocialUser, SocialUserVo> {
/**
* 根据authId查询SocialUser表和SysUser表返回SocialUserAuthResult映射的对象
* @param authId 认证ID
* @return SocialUser
*/
SocialUserVo selectSocialUserByAuthId(String authId);
}

View File

@ -0,0 +1,71 @@
package org.dromara.system.service;
import org.dromara.system.domain.SocialUser;
import org.dromara.system.domain.bo.SysUserBo;
import org.dromara.system.domain.vo.SocialUserVo;
import org.dromara.system.domain.bo.SocialUserBo;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.system.domain.vo.SysUserVo;
import java.util.Collection;
import java.util.List;
/**
* 社会化关系Service接口
*
* @author thiszhc
* @date 2023-06-12
*/
public interface ISocialUserService {
/**
* 查询授权关系
*/
SocialUserVo queryById(Long id);
/**
* 查询授权列表
*/
TableDataInfo<SocialUserVo> queryPageList(SocialUserBo bo, PageQuery pageQuery);
/**
* 查询授权列表
*/
List<SocialUserVo> queryList(SocialUserBo bo);
/**
* 新增授权关系
*/
Boolean insertByBo(SocialUserBo bo);
/**
* 修改授权关系
*/
Boolean updateByBo(SocialUserBo bo);
/**
* 校验并批量删除社会化关系信息
*/
Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid);
/**
* 删除社会化关系信息
*/
Boolean deleteWithValidById(Long id);
/**
* 根据用户ID查询授权关系
*/
Boolean isExistByUserIdAndSource(Long userId, String source);
/**
* 根据authId查询SocialUser表和SysUser表返回SocialUserAuthResult映射的对象
* @param authId 认证ID
* @return SocialUser
*/
SocialUserVo selectSocialUserByAuthId(String authId);
}

View File

@ -0,0 +1,162 @@
package org.dromara.system.service.impl;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.bo.SysUserBo;
import org.dromara.system.domain.vo.SysUserVo;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.SocialUserBo;
import org.dromara.system.domain.vo.SocialUserVo;
import org.dromara.system.domain.SocialUser;
import org.dromara.system.mapper.SocialUserMapper;
import org.dromara.system.service.ISocialUserService;
import java.util.List;
import java.util.Map;
import java.util.Collection;
/**
* 社会化关系Service业务层处理
*
* @author thiszhc
* @date 2023-06-12
*/
@RequiredArgsConstructor
@Service
public class SocialUserServiceImpl implements ISocialUserService {
private final SocialUserMapper baseMapper;
/**
* 查询社会化关系
*/
@Override
public SocialUserVo queryById(Long id) {
return baseMapper.selectVoById(id);
}
/**
* 查询社会化关系列表
*/
@Override
public TableDataInfo<SocialUserVo> queryPageList(SocialUserBo bo, PageQuery pageQuery) {
LambdaQueryWrapper<SocialUser> lqw = buildQueryWrapper(bo);
Page<SocialUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
return TableDataInfo.build(result);
}
/**
* 查询社会化关系列表
*/
@Override
public List<SocialUserVo> queryList(SocialUserBo bo) {
LambdaQueryWrapper<SocialUser> lqw = buildQueryWrapper(bo);
return baseMapper.selectVoList(lqw);
}
private LambdaQueryWrapper<SocialUser> buildQueryWrapper(SocialUserBo bo) {
Map<String, Object> params = bo.getParams();
LambdaQueryWrapper<SocialUser> lqw = Wrappers.lambdaQuery();
lqw.eq(StringUtils.isNotBlank(bo.getAuthId()), SocialUser::getAuthId, bo.getAuthId());
lqw.eq(StringUtils.isNotBlank(bo.getSource()), SocialUser::getSource, bo.getSource());
lqw.eq(StringUtils.isNotBlank(bo.getAccessToken()), SocialUser::getAccessToken, bo.getAccessToken());
lqw.eq(bo.getExpireIn() != 0, SocialUser::getExpireIn, bo.getExpireIn());
lqw.eq(StringUtils.isNotBlank(bo.getRefreshToken()), SocialUser::getRefreshToken, bo.getRefreshToken());
lqw.eq(StringUtils.isNotBlank(bo.getOpenId()), SocialUser::getOpenId, bo.getOpenId());
lqw.eq(StringUtils.isNotBlank(bo.getScope()), SocialUser::getScope, bo.getScope());
lqw.eq(StringUtils.isNotBlank(bo.getAccessCode()), SocialUser::getAccessCode, bo.getAccessCode());
lqw.eq(StringUtils.isNotBlank(bo.getUnionId()), SocialUser::getUnionId, bo.getUnionId());
lqw.eq(StringUtils.isNotBlank(bo.getScope()), SocialUser::getScope, bo.getScope());
lqw.eq(StringUtils.isNotBlank(bo.getTokenType()), SocialUser::getTokenType, bo.getTokenType());
lqw.eq(StringUtils.isNotBlank(bo.getIdToken()), SocialUser::getIdToken, bo.getIdToken());
lqw.eq(StringUtils.isNotBlank(bo.getMacAlgorithm()), SocialUser::getMacAlgorithm, bo.getMacAlgorithm());
lqw.eq(StringUtils.isNotBlank(bo.getMacKey()), SocialUser::getMacKey, bo.getMacKey());
lqw.eq(StringUtils.isNotBlank(bo.getCode()), SocialUser::getCode, bo.getCode());
lqw.eq(StringUtils.isNotBlank(bo.getOauthToken()), SocialUser::getOauthToken, bo.getOauthToken());
lqw.eq(StringUtils.isNotBlank(bo.getOauthTokenSecret()), SocialUser::getOauthTokenSecret, bo.getOauthTokenSecret());
return lqw;
}
/**
* 新增社会化关系
*/
@Override
public Boolean insertByBo(SocialUserBo bo) {
SocialUser add = MapstructUtils.convert(bo, SocialUser.class);
validEntityBeforeSave(add);
boolean flag = baseMapper.insert(add) > 0;
if (flag) {
bo.setId(add.getId());
}
return flag;
}
/**
* 修改社会化关系
*/
@Override
public Boolean updateByBo(SocialUserBo bo) {
SocialUser update = MapstructUtils.convert(bo, SocialUser.class);
validEntityBeforeSave(update);
return baseMapper.updateById(update) > 0;
}
/**
* 保存前的数据校验
*/
private void validEntityBeforeSave(SocialUser entity) {
//TODO 做一些数据校验,如唯一约束
}
/**
* 批量删除社会化关系
*/
@Override
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
if (isValid) {
//TODO 做一些业务上的校验,判断是否需要校验
}
return baseMapper.deleteBatchIds(ids) > 0;
}
/**
* 删除社会化关系
*/
@Override
public Boolean deleteWithValidById(Long id) {
return baseMapper.deleteById(id) > 0;
}
/**
* 根据用户id和来源查询用户是否存在
*
* @param userId 用户id
* @param source 来源
* @return 是否存在
*/
@Override
public Boolean isExistByUserIdAndSource(Long userId, String source) {
LambdaQueryWrapper<SocialUser> lqw = Wrappers.lambdaQuery();
lqw.eq(SocialUser::getUserId, userId);
lqw.eq(SocialUser::getSource, source);
return baseMapper.selectCount(lqw) > 0;
}
/**
* 根据authId查询用户信息
*
* @param authId 用户id
* @return 用户信息
*/
@Override
public SocialUserVo selectSocialUserByAuthId(String authId) {
return baseMapper.selectSocialUserByAuthId(authId);
}
}

View File

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.dromara.system.mapper.SocialUserMapper">
<resultMap type="org.dromara.system.domain.vo.SocialUserVo" id="SocialUserAuthResult">
<id property="id" column="id"/>
</resultMap>
<sql id="selectSocialUser">
select id,
user_id,
tenant_id,
auth_id,
source,
open_id,
access_token,
expire_in,
refresh_token,
access_code,
union_id,
scope,
token_type,
id_token,
mac_algorithm,
mac_key,
code,
oauth_token,
oauth_token_secret,
create_dept,
create_by,
create_time,
update_by,
update_time
from social_user
</sql>
<!-- 根据userId查询SocialUser表对应userId的SysUser返回SysUserBo的对象 -->
<select id="selectSocialUserByUserId" parameterType="String" resultMap="SocialUserAuthResult">
select b.*
from social_user a
left join sys_user b on a.user_id = b.user_id
where a.user_id = #{userId}
</select>
<!-- 根据authId查询SocialUser表和SysUser表返回SocialUserAuthResult映射的对象 -->
<select id="selectSocialUserByAuthId" parameterType="String" resultMap="SocialUserAuthResult">
select b.user_id as userId,
b.tenant_id as tenantId,
b.user_name as userName,
b.password as password,
a.auth_id as authId,
a.source as source
from social_user a
left join sys_user b on a.user_id = b.user_id
where a.auth_id = #{authId}
</select>
<!-- 根据userId和source查询SocialUser表返回int类型的结果 -->
<select id="checkSocialUser" parameterType="org.dromara.system.domain.vo.SocialUserVo" resultType="int">
select count(*)
from social_user
where user_id = #{userId} and source = #{source} limit 1
</select>
</mapper>

File diff suppressed because it is too large Load Diff