From e4b405491fa2ba0ede349b8f92b5b86172e2da3a Mon Sep 17 00:00:00 2001 From: thiszhc <2029364173@qq.com> Date: 星期六, 10 六月 2023 21:39:31 +0800 Subject: [PATCH] 新增加第三方登录授权功能 --- ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysAuthUserMapper.xml | 60 +++++++ ruoyi-admin/pom.xml | 15 + ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java | 83 ++++++++++ ruoyi-common/ruoyi-common-auth/src/main/java/org.dromara.common.auth/utils/AuthUtils.java | 112 ++++++++++++++ ruoyi-admin/src/main/java/org/dromara/web/service/SysLoginService.java | 65 ++++++++ ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/enums/DeviceType.java | 4 ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/mapper/SysUserMapper.java | 45 +++++ script/AuthSQL/sys_auth_user.sql | 14 + pom.xml | 8 + ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/SysAuthUser.java | 40 +++++ ruoyi-common/pom.xml | 1 ruoyi-common/ruoyi-common-auth/pom.xml | 22 ++ 12 files changed, 466 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index b20e7f0..a1e7524 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,7 @@ <bouncycastle.version>1.72</bouncycastle.version> <!-- 绂荤嚎IP鍦板潃瀹氫綅搴� --> <ip2region.version>2.7.0</ip2region.version> + <justauth.version>1.15.6</justauth.version> <!-- 涓存椂淇 snakeyaml 婕忔礊 --> <snakeyaml.version>1.33</snakeyaml.version> @@ -291,6 +292,13 @@ <version>${snakeyaml.version}</version> </dependency> + <!-- 绗笁鏂规巿鏉冪櫥褰� --> + <dependency> + <groupId>me.zhyd.oauth</groupId> + <artifactId>JustAuth</artifactId> + <version>${justauth.version}</version> + </dependency> + <!-- 鍔犲瘑鍖呭紩鍏� --> <dependency> <groupId>org.bouncycastle</groupId> diff --git a/ruoyi-admin/pom.xml b/ruoyi-admin/pom.xml index af5f239..dd4fc58 100644 --- a/ruoyi-admin/pom.xml +++ b/ruoyi-admin/pom.xml @@ -75,6 +75,21 @@ <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-auth</artifactId> + <version>5.1.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>com.aliyun</groupId> + <artifactId>credentials-java</artifactId> + <version>0.2.4</version> + <scope>compile</scope> + </dependency> <!-- skywalking 鏁村悎 logback --> <!-- <dependency>--> diff --git a/ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java b/ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java index bba6113..6d0ef7b 100644 --- a/ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java +++ b/ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java @@ -2,9 +2,21 @@ import cn.dev33.satoken.annotation.SaIgnore; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import com.alibaba.fastjson.JSON; import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.constraints.NotBlank; import lombok.RequiredArgsConstructor; +import me.zhyd.oauth.cache.AuthDefaultStateCache; +import me.zhyd.oauth.cache.AuthStateCache; +import me.zhyd.oauth.model.AuthCallback; +import me.zhyd.oauth.model.AuthResponse; +import me.zhyd.oauth.model.AuthUser; +import me.zhyd.oauth.request.AuthRequest; +import me.zhyd.oauth.utils.AuthStateUtils; +import org.dromara.common.auth.utils.AuthUtils; import org.dromara.common.core.domain.R; import org.dromara.common.core.domain.model.EmailLoginBody; import org.dromara.common.core.domain.model.LoginBody; @@ -16,6 +28,8 @@ 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.domain.vo.SysUserVo; +import org.dromara.system.mapper.SysUserMapper; import org.dromara.system.service.ISysConfigService; import org.dromara.system.service.ISysTenantService; import org.dromara.web.domain.vo.LoginTenantVo; @@ -26,8 +40,11 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import java.io.IOException; import java.net.URL; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * 璁よ瘉 @@ -41,10 +58,18 @@ @RequestMapping("/auth") public class AuthController { + private AuthStateCache authStateCache; private final SysLoginService loginService; private final SysRegisterService registerService; private final ISysConfigService configService; private final ISysTenantService tenantService; + private final SysUserMapper userMapper; + private final Map<String, String> auths = new HashMap<>(); + { + auths.put("gitee", "{\"clientId\":\"38eaaa1b77b5e064313057a2f5745ce3a9f3e7686d9bd302c7df2f308ef6db81\",\"clientSecret\":\"2e633af8780cb9fe002c4c7291b722db944402e271efb99b062811f52d7da1ff\",\"redirectUri\":\"http://127.0.0.1:8888/social-login?source=gitee\"}"); + auths.put("github", "{\"clientId\":\"Iv1.1be0cdcd71aca63b\",\"clientSecret\":\"0d59d28b43152bc8906011624db37b0fed88d154\",\"redirectUri\":\"http://127.0.0.1:80/social-login?source=github\"}"); + authStateCache = AuthDefaultStateCache.INSTANCE;// 浣跨敤榛樿鐨勭紦瀛� + } /** * 鐧诲綍鏂规硶 @@ -115,6 +140,64 @@ return R.ok(loginVo); } + + + + /** + * 璁よ瘉鎺堟潈 + * @param source + * @throws IOException + */ + @GetMapping("/binding/{source}") + @ResponseBody + public R<LoginVo> authBinding(@PathVariable("source") String source, HttpServletRequest request){ + SysUserVo userLoding = new SysUserVo(); + if (ObjectUtil.isNull(userLoding)) { + return R.fail("鎺堟潈澶辫触锛岃鍏堢櫥褰曞啀缁戝畾"); + } + if (userMapper.checkAuthUser(userLoding.getUserId(),source) > 0) + { + return R.fail(source + "骞冲彴璐﹀彿宸茬粡缁戝畾"); + } + String obj = auths.get(source); + if (StringUtils.isEmpty(obj)) + { + return R.fail(source + "骞冲彴璐﹀彿鏆備笉鏀寔"); + } + JSONObject json = JSONUtil.parseObj(obj); + AuthRequest authRequest = AuthUtils.getAuthRequest(source, + json.getStr("clientId"), + json.getStr("clientSecret"), + json.getStr("redirectUri"), authStateCache); + String authorizeUrl = authRequest.authorize(AuthStateUtils.createState()); + return R.ok(authorizeUrl); + } + + /** + * @param source + * @param callback + * @param request + * @return + */ + @SuppressWarnings("unchecked") + @GetMapping("/social-login/{source}") + public R<String> socialLogin(@PathVariable("source") String source, AuthCallback callback, HttpServletRequest request) throws IOException { + String obj = auths.get(source); + if (StringUtils.isEmpty(obj)) + { + return R.fail("绗笁鏂瑰钩鍙扮郴缁熶笉鏀寔鎴栨湭鎻愪緵鏉ユ簮"); + } + JSONObject json = JSONUtil.parseObj(obj); + AuthRequest authRequest = AuthUtils.getAuthRequest(source, + json.getStr("clientId"), + json.getStr("clientSecret"), + json.getStr("redirectUri"), authStateCache); + AuthResponse<AuthUser> response = authRequest.login(callback); + return loginService.socialLogin(source, response, request); + } + + + /** * 閫�鍑虹櫥褰� */ diff --git a/ruoyi-admin/src/main/java/org/dromara/web/service/SysLoginService.java b/ruoyi-admin/src/main/java/org/dromara/web/service/SysLoginService.java index 689a55d..b152710 100644 --- a/ruoyi-admin/src/main/java/org/dromara/web/service/SysLoginService.java +++ b/ruoyi-admin/src/main/java/org/dromara/web/service/SysLoginService.java @@ -6,11 +6,15 @@ 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; @@ -28,6 +32,7 @@ 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.SysAuthUser; import org.dromara.system.domain.SysUser; import org.dromara.system.domain.vo.SysTenantVo; import org.dromara.system.domain.vo.SysUserVo; @@ -37,6 +42,7 @@ 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; @@ -154,6 +160,65 @@ return StpUtil.getTokenValue(); } + + /** + * 璁よ瘉鎺堟潈鐧诲綍 + * @param source + * @throws IOException + */ + /** + * 绀句氦鐧诲綍 + * @param source 鐧诲綍鏉ユ簮 + * @param authUser 鎺堟潈鍝嶅簲瀹炰綋 + * @param request Http璇锋眰瀵硅薄 + * @return 缁熶竴鍝嶅簲瀹炰綋 + * @throws IOException + */ + public R<String> socialLogin(String source, AuthResponse<AuthUser> authUser, HttpServletRequest request) throws IOException { + // 鍒ゆ柇鎺堟潈鍝嶅簲鏄惁鎴愬姛 + if (!authUser.ok()) { + return R.fail("瀵逛笉璧凤紝鎺堟潈淇℃伅楠岃瘉涓嶉�氳繃锛岃鑱旂郴绠$悊鍛�"); + } + AuthUser authUserData = authUser.getData(); + // 鍒ゆ柇鏁版嵁搴撲腑鏄惁宸插瓨鍦ㄨ鐢ㄦ埛 + SysUserVo user = userMapper.selectAuthUserByUuid(source + authUserData.getUuid()); + if (ObjectUtil.isNotNull(user)) { + checkTenant(user.getTenantId()); + SysUserVo dbUser = loadUserByUsername(user.getTenantId(), user.getUserName()); + // 鐧诲綍 + LoginHelper.loginByDevice(buildLoginUser(dbUser), DeviceType.auth); + recordLogininfor(dbUser.getTenantId(), user.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")); + recordLoginInfo(user.getUserId()); + return R.ok(StpUtil.getTokenValue()); + } else { + if (LoginHelper.getUserId() == null) { + return R.fail("鎺堟潈澶辫触锛岃鍏堢櫥褰曞啀缁戝畾"); + } + // 缁勮鎺堟潈鐢ㄦ埛淇℃伅 + SysAuthUser sysAuthUser = new SysAuthUser(); + sysAuthUser.setAvatar(authUserData.getAvatar()); + sysAuthUser.setUuid(source + authUserData.getUuid()); + sysAuthUser.setUserId(LoginHelper.getUserId()); + sysAuthUser.setUserName(authUserData.getUsername()); + sysAuthUser.setNickName(authUserData.getNickname()); + sysAuthUser.setEmail(authUserData.getEmail()); + sysAuthUser.setSource(source); + sysAuthUser.setCreateTime(new Date().toString()); + // 鏂扮敤鎴凤紝缁戝畾绗笁鏂硅处鍙� + userMapper.insertAuthUser(sysAuthUser); + SysUserVo lodingData = loadUserByUsername(LoginHelper.getTenantId(), LoginHelper.getUsername()); + checkTenant(lodingData.getTenantId()); + LoginHelper.loginByDevice(buildLoginUser(lodingData), DeviceType.auth); + recordLogininfor(lodingData.getTenantId(), sysAuthUser.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")); + recordLoginInfo(sysAuthUser.getUserId()); + return R.ok(StpUtil.getTokenValue()); + } + } + + + + + /** * 閫�鍑虹櫥褰� */ diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 0428aea..4813f79 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -11,6 +11,7 @@ <modules> <module>ruoyi-common-bom</module> + <module>ruoyi-common-auth</module> <module>ruoyi-common-core</module> <module>ruoyi-common-doc</module> <module>ruoyi-common-excel</module> diff --git a/ruoyi-common/ruoyi-common-auth/pom.xml b/ruoyi-common/ruoyi-common-auth/pom.xml new file mode 100644 index 0000000..9af89e6 --- /dev/null +++ b/ruoyi-common/ruoyi-common-auth/pom.xml @@ -0,0 +1,22 @@ +<?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-auth</artifactId> + <description> + ruoyi-common-auth 璁よ瘉妯″潡 + </description> + <dependencies> + <dependency> + <groupId>me.zhyd.oauth</groupId> + <artifactId>JustAuth</artifactId> + </dependency> + </dependencies> +</project> diff --git a/ruoyi-common/ruoyi-common-auth/src/main/java/org.dromara.common.auth/utils/AuthUtils.java b/ruoyi-common/ruoyi-common-auth/src/main/java/org.dromara.common.auth/utils/AuthUtils.java new file mode 100644 index 0000000..981b3be --- /dev/null +++ b/ruoyi-common/ruoyi-common-auth/src/main/java/org.dromara.common.auth/utils/AuthUtils.java @@ -0,0 +1,112 @@ +package org.dromara.common.auth.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 ruoyi + */ +public class AuthUtils { + public static AuthRequest getAuthRequest(String source, String clientId, String clientSecret, String redirectUri, + AuthStateCache authStateCache) + { + AuthRequest authRequest = null; + switch (source.toLowerCase()) { + case "dingtalk" -> + authRequest = new AuthDingTalkRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "baidu" -> + authRequest = new AuthBaiduRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "github" -> + authRequest = new AuthGithubRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "gitee" -> + authRequest = new AuthGiteeRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "weibo" -> + authRequest = new AuthWeiboRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "coding" -> + authRequest = new AuthCodingRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).codingGroupName("").build(), authStateCache); + case "oschina" -> + authRequest = new AuthOschinaRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "alipay" -> + // 鏀粯瀹濆湪鍒涘缓鍥炶皟鍦板潃鏃讹紝涓嶅厑璁镐娇鐢╨ocalhost鎴栬��127.0.0.1锛屾墍浠ヨ繖鍎跨殑鍥炶皟鍦板潃浣跨敤鐨勫眬鍩熺綉鍐呯殑ip + authRequest = new AuthAlipayRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .alipayPublicKey("").redirectUri(redirectUri).build(), authStateCache); + case "qq" -> + authRequest = new AuthQqRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "wechat_open" -> authRequest = new AuthWeChatOpenRequest(AuthConfig.builder().clientId(clientId) + .clientSecret(clientSecret).redirectUri(redirectUri).build(), authStateCache); + case "csdn" -> + authRequest = new AuthCsdnRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "taobao" -> + authRequest = new AuthTaobaoRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "douyin" -> + authRequest = new AuthDouyinRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "linkedin" -> + authRequest = new AuthLinkedinRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "microsoft" -> authRequest = new AuthMicrosoftRequest(AuthConfig.builder().clientId(clientId) + .clientSecret(clientSecret).redirectUri(redirectUri).build(), authStateCache); + case "mi" -> + authRequest = new AuthMiRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "toutiao" -> + authRequest = new AuthToutiaoRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "teambition" -> authRequest = new AuthTeambitionRequest(AuthConfig.builder().clientId(clientId) + .clientSecret(clientSecret).redirectUri(redirectUri).build(), authStateCache); + case "pinterest" -> authRequest = new AuthPinterestRequest(AuthConfig.builder().clientId(clientId) + .clientSecret(clientSecret).redirectUri(redirectUri).build(), authStateCache); + case "renren" -> + authRequest = new AuthRenrenRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "stack_overflow" -> authRequest = new AuthStackOverflowRequest(AuthConfig.builder().clientId(clientId) + .clientSecret(clientSecret).redirectUri(redirectUri).stackOverflowKey("").build(), + authStateCache); + case "huawei" -> + authRequest = new AuthHuaweiRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "wechat_enterprise" -> + authRequest = new AuthWeChatEnterpriseRequest(AuthConfig.builder().clientId(clientId) + .clientSecret(clientSecret).redirectUri(redirectUri).agentId("").build(), authStateCache); + case "kujiale" -> + authRequest = new AuthKujialeRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "gitlab" -> + authRequest = new AuthGitlabRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + case "meituan" -> + authRequest = new AuthMeituanRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + 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(), authStateCache); + case "aliyun" -> + authRequest = new AuthAliyunRequest(AuthConfig.builder().clientId(clientId).clientSecret(clientSecret) + .redirectUri(redirectUri).build(), authStateCache); + default -> { + } + } + if (null == authRequest) + { + throw new AuthException("鏈幏鍙栧埌鏈夋晥鐨凙uth閰嶇疆"); + } + return authRequest; + } +} + diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/enums/DeviceType.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/enums/DeviceType.java index 09bf44b..7127bbb 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/enums/DeviceType.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/enums/DeviceType.java @@ -26,7 +26,9 @@ /** * 灏忕▼搴忕 */ - XCX("xcx"); + XCX("xcx"), + + auth("auth"); private final String device; } diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/SysAuthUser.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/SysAuthUser.java new file mode 100644 index 0000000..5008786 --- /dev/null +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/SysAuthUser.java @@ -0,0 +1,40 @@ +package org.dromara.system.domain; + +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@TableName("sys_auth_user") +public class SysAuthUser { + + private static final long serialVersionUID = 1L; + + /** 鎺堟潈ID */ + private Long authId; + + /** 绗笁鏂瑰钩鍙扮敤鎴峰敮涓�ID */ + private String uuid; + + /** 绯荤粺鐢ㄦ埛ID */ + private Long userId; + + /** 鐧诲綍璐﹀彿 */ + private String userName; + + /** 鐢ㄦ埛鏄电О */ + private String nickName; + + /** 澶村儚鍦板潃 */ + private String avatar; + + /** 鐢ㄦ埛閭 */ + private String email; + + /** 鐢ㄦ埛鏉ユ簮 */ + private String source; + + private String createTime; + +} diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/mapper/SysUserMapper.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/mapper/SysUserMapper.java index 81bd1ff..94c9c1e 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/mapper/SysUserMapper.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/mapper/SysUserMapper.java @@ -7,6 +7,7 @@ import org.dromara.common.mybatis.annotation.DataColumn; import org.dromara.common.mybatis.annotation.DataPermission; import org.dromara.common.mybatis.core.mapper.BaseMapperPlus; +import org.dromara.system.domain.SysAuthUser; import org.dromara.system.domain.SysUser; import org.dromara.system.domain.vo.SysUserVo; import org.apache.ibatis.annotations.Param; @@ -123,8 +124,8 @@ * @return 鐢ㄦ埛瀵硅薄淇℃伅 */ @DataPermission({ - @DataColumn(key = "deptName", value = "d.dept_id"), - @DataColumn(key = "userName", value = "u.user_id") + @DataColumn(key = "deptName", value = "d.dept_id"),// 閮ㄩ棬鏉冮檺 + @DataColumn(key = "userName", value = "u.user_id")// 鐢ㄦ埛鏉冮檺 }) SysUserVo selectUserById(Long userId); @@ -142,4 +143,44 @@ }) int updateById(@Param(Constants.ENTITY) SysUser user); + /** + * 鏍规嵁鐢ㄦ埛缂栧彿鏌ヨ鎺堟潈鍒楄〃 + * + * @param userId 鐢ㄦ埛缂栧彿 + * @return 鎺堟潈鍒楄〃 + */ + public List<SysAuthUser> selectAuthUserListByUserId(Long userId); + + /** + * 鏍规嵁uuid鏌ヨ鐢ㄦ埛淇℃伅 + * + * @param uuid 鍞竴淇℃伅 + * @return 缁撴灉 + */ + public SysUserVo selectAuthUserByUuid(String uuid); + + /** + * 鏍¢獙source骞冲彴鏄惁缁戝畾 + * + * @param userId 鐢ㄦ埛缂栧彿 + * @param source 缁戝畾骞冲彴 + * @return 缁撴灉 + */ + public int checkAuthUser(@Param("userId") Long userId, @Param("source") String source); + + /** + * 鏂板绗笁鏂规巿鏉冧俊鎭� + * + * @param authUser 鐢ㄦ埛淇℃伅 + * @return 缁撴灉 + */ + public int insertAuthUser(SysAuthUser authUser); + + /** + * 鏍规嵁缂栧彿鍒犻櫎绗笁鏂规巿鏉冧俊鎭� + * + * @param authId 鎺堟潈缂栧彿 + * @return 缁撴灉 + */ + public int deleteAuthUser(Long authId); } diff --git a/ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysAuthUserMapper.xml b/ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysAuthUserMapper.xml new file mode 100644 index 0000000..864b3ba --- /dev/null +++ b/ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysAuthUserMapper.xml @@ -0,0 +1,60 @@ +<?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.SysUserMapper"> + + <resultMap id="SysAuthUserResult" type="org.dromara.system.domain.SysAuthUser"> + <id property="authId" column="auth_id" /> + <result property="uuid" column="uuid" /> + <result property="userId" column="user_id" /> + <result property="userName" column="user_name" /> + <result property="nickName" column="nick_name" /> + <result property="avatar" column="avatar" /> + <result property="email" column="email" /> + <result property="source" column="source" /> + <result property="createTime" column="create_time" /> + </resultMap> + + + <select id="selectAuthUserByUuid" parameterType="String" resultMap="SysUserResult"> + select b.user_id as user_id, b.user_name as user_name, b.password as password , a.tenant_id as tenant_id + from sys_auth_user a left join sys_user b on a.user_id = b.user_id + where a.uuid = #{uuid} and b.del_flag = '0' + </select> + + <select id="selectAuthUserListByUserId" parameterType="Long" resultMap="SysAuthUserResult"> + select auth_id, uuid, user_id, user_name, nick_name, avatar, email, source, create_time, tenant_id from sys_auth_user where user_id = #{userId} + </select> + + <select id="checkAuthUser" parameterType="org.dromara.system.domain.SysAuthUser" resultType="int"> + select count(1) from sys_auth_user where user_id=#{userId} and source=#{source} limit 1 + </select> + + <insert id="insertAuthUser" parameterType="org.dromara.system.domain.SysAuthUser"> + insert into sys_auth_user( + <if test="uuid != null and uuid != ''">uuid,</if> + <if test="userId != null and userId != 0">user_id,</if> + <if test="userName != null and userName != ''">user_name,</if> + <if test="nickName != null and nickName != ''">nick_name,</if> + <if test="avatar != null and avatar != ''">avatar,</if> + <if test="email != null and email != ''">email,</if> + <if test="source != null and source != ''">source,</if> + create_time + )values( + <if test="uuid != null and uuid != ''">#{uuid},</if> + <if test="userId != null and userId != 0">#{userId},</if> + <if test="userName != null and userName != ''">#{userName},</if> + <if test="nickName != null and nickName != ''">#{nickName},</if> + <if test="avatar != null and avatar != ''">#{avatar},</if> + <if test="email != null and email != ''">#{email},</if> + <if test="source != null and source != ''">#{source},</if> + now() + ) + </insert> + + <delete id="deleteAuthUser" parameterType="Long"> + delete from sys_auth_user where auth_id = #{authId} + </delete> + +</mapper> diff --git a/script/AuthSQL/sys_auth_user.sql b/script/AuthSQL/sys_auth_user.sql new file mode 100644 index 0000000..4a41675 --- /dev/null +++ b/script/AuthSQL/sys_auth_user.sql @@ -0,0 +1,14 @@ +CREATE TABLE `sys_auth_user` ( + `auth_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '鎺堟潈ID', + `uuid` varchar(500) NOT NULL COMMENT '绗笁鏂瑰钩鍙扮敤鎴峰敮涓�ID', + `user_id` bigint(20) unsigned NOT NULL COMMENT '绯荤粺鐢ㄦ埛ID', + `user_name` varchar(30) NOT NULL COMMENT '鐧诲綍璐﹀彿', + `nick_name` varchar(30) DEFAULT '' COMMENT '鐢ㄦ埛鏄电О', + `avatar` varchar(500) DEFAULT '' COMMENT '澶村儚鍦板潃', + `email` varchar(255) DEFAULT '' COMMENT '鐢ㄦ埛閭', + `source` varchar(255) DEFAULT '' COMMENT '鐢ㄦ埛鏉ユ簮', + ` tenant_id` varchar(20) DEFAULT '000000' COMMENT '绉熸埛id', + `create_time` datetime DEFAULT NULL COMMENT '鍒涘缓鏃堕棿', + `tenant_id` varchar(25) NOT NULL DEFAULT '000000', + PRIMARY KEY (`auth_id`) USING BTREE +) ENGINE=InnoDB AUTO_INCREMENT=102 DEFAULT CHARSET=utf8mb4 COMMENT='绗笁鏂瑰钩鍙版巿鏉冪敤鎴蜂俊鎭〃'; -- Gitblit v1.9.3