From 005e7df1b8d7733b459c24104bc775c52e528454 Mon Sep 17 00:00:00 2001
From: thiszhc <2029364173@qq.com>
Date: 星期四, 15 六月 2023 19:30:17 +0800
Subject: [PATCH] 优化第三方授权登录

---
 ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java |   76 ++++++++++++++++++-------------------
 1 files changed, 37 insertions(+), 39 deletions(-)

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 6d0ef7b..8cb96cd 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
@@ -3,20 +3,14 @@
 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;
@@ -25,11 +19,14 @@
 import org.dromara.common.core.utils.MapstructUtils;
 import org.dromara.common.core.utils.StreamUtils;
 import org.dromara.common.core.utils.StringUtils;
+import org.dromara.common.social.config.properties.SocialLoginConfigProperties;
+import org.dromara.common.social.config.properties.SocialProperties;
+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.domain.vo.SysUserVo;
-import org.dromara.system.mapper.SysUserMapper;
+import org.dromara.system.service.ISocialUserService;
 import org.dromara.system.service.ISysConfigService;
 import org.dromara.system.service.ISysTenantService;
 import org.dromara.web.domain.vo.LoginTenantVo;
@@ -42,9 +39,7 @@
 
 import java.io.IOException;
 import java.net.URL;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 /**
  * 璁よ瘉
@@ -58,18 +53,14 @@
 @RequestMapping("/auth")
 public class AuthController {
 
-    private AuthStateCache authStateCache;
+    private final SocialProperties socialProperties;
     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;// 浣跨敤榛樿鐨勭紦瀛�
-    }
+    private final ISocialUserService socialUserService;
+
+
 
     /**
      * 鐧诲綍鏂规硶
@@ -141,12 +132,9 @@
     }
 
 
-
-
     /**
      * 璁よ瘉鎺堟潈
      * @param source
-     * @throws IOException
      */
     @GetMapping("/binding/{source}")
     @ResponseBody
@@ -155,25 +143,24 @@
         if (ObjectUtil.isNull(userLoding)) {
             return R.fail("鎺堟潈澶辫触锛岃鍏堢櫥褰曞啀缁戝畾");
         }
-        if (userMapper.checkAuthUser(userLoding.getUserId(),source) > 0)
+        if (socialUserService.isExistByUserIdAndSource(userLoding.getUserId(),source))
         {
-            return R.fail(source + "骞冲彴璐﹀彿宸茬粡缁戝畾");
+            return R.fail(source + "骞冲彴璐﹀彿宸茬粡琚处鍙风粦瀹�");
         }
-        String obj = auths.get(source);
-        if (StringUtils.isEmpty(obj))
-        {
+        SocialLoginConfigProperties obj = socialProperties.getType().get(source);
+        if (ObjectUtil.isNull(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);
+        AuthRequest authRequest = SocialUtils.getAuthRequest(source,
+            obj.getClientId(),
+            obj.getClientSecret(),
+            obj.getRedirectUri());
         String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
         return R.ok(authorizeUrl);
     }
 
     /**
+     * 绗笁鏂圭櫥褰曞洖璋冧笟鍔″鐞�
      * @param source
      * @param callback
      * @param request
@@ -182,20 +169,31 @@
     @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("绗笁鏂瑰钩鍙扮郴缁熶笉鏀寔鎴栨湭鎻愪緵鏉ユ簮");
+        SocialLoginConfigProperties obj = socialProperties.getType().get(source);
+        if (ObjectUtil.isNull(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);
+        AuthRequest authRequest = SocialUtils.getAuthRequest(source,
+            obj.getClientId(),
+            obj.getClientSecret(),
+            obj.getRedirectUri());
         AuthResponse<AuthUser> response = authRequest.login(callback);
         return loginService.socialLogin(source, response, request);
     }
 
+    /**
+     * 鍙栨秷鎺堟潈
+     * @param socialId
+     */
+    @DeleteMapping(value = "/unlock/{socialId}")
+    public R<Void> unlockSocial(@PathVariable Long socialId)
+    {
+        Boolean rows = socialUserService.deleteWithValidById(socialId);
+        return rows ? R.ok() : R.fail("鍙栨秷鎺堟潈澶辫触");
+    }
+
+
+
 
 
     /**

--
Gitblit v1.9.3