From 9448782f451790beb05b2e6717683dc0a59d6a24 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子Li <15040126243@163.com> Date: 星期二, 20 六月 2023 15:00:16 +0800 Subject: [PATCH] update 优化 !pr370 完成三方登录授权功能 --- ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java | 97 ++++++++++++++++++++---------------------------- 1 files changed, 41 insertions(+), 56 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..41a5362 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,13 @@ 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.ISysSocialService; import org.dromara.system.service.ISysConfigService; import org.dromara.system.service.ISysTenantService; import org.dromara.web.domain.vo.LoginTenantVo; @@ -40,11 +36,8 @@ 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; /** * 璁よ瘉 @@ -58,18 +51,13 @@ @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 ISysSocialService socialUserService; + /** * 鐧诲綍鏂规硶 @@ -141,61 +129,58 @@ } - - /** * 璁よ瘉鎺堟潈 - * @param source - * @throws IOException + * + * @param source 鐧诲綍鏉ユ簮 + * @return 缁撴灉 */ @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)) - { + public R<String> authBinding(@PathVariable("source") String source) { + 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 - * @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 { - String obj = auths.get(source); - if (StringUtils.isEmpty(obj)) - { - return R.fail("绗笁鏂瑰钩鍙扮郴缁熶笉鏀寔鎴栨湭鎻愪緵鏉ユ簮"); + public R<String> socialLogin(@PathVariable("source") String source, AuthCallback callback) { + 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); + return loginService.socialLogin(source, response); } + /** + * 鍙栨秷鎺堟潈 + * + * @param socialId 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