From 5110961eb98d820881a7c4a218e9e93fea07870e Mon Sep 17 00:00:00 2001 From: thiszhc <2029364173@qq.com> Date: 星期四, 15 六月 2023 01:23:07 +0800 Subject: [PATCH] 第三方授权登录,加上配置 --- ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java | 87 ++++++++++++ ruoyi-common/ruoyi-common-social/src/main/java/org/dromara/common/social/utils/AuthRedisStateCache.java | 79 +++++++++++ ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/enums/DeviceType.java | 7 ruoyi-common/ruoyi-common-social/src/main/java/org/dromara/common/social/config/properties/ConfigProperties.java | 58 ++++++++ ruoyi-admin/src/main/resources/application-dev.yml | 148 +++++++++++++++++++++ 5 files changed, 378 insertions(+), 1 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 bba6113..f46cbdc 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,18 @@ import cn.dev33.satoken.annotation.SaIgnore; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.json.JSON; +import cn.hutool.json.JSONUtil; import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.constraints.NotBlank; import lombok.RequiredArgsConstructor; +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.core.domain.R; import org.dromara.common.core.domain.model.EmailLoginBody; import org.dromara.common.core.domain.model.LoginBody; @@ -13,9 +22,16 @@ 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.SocialConfig; +import org.dromara.common.social.config.properties.ConfigProperties; +import org.dromara.common.social.config.properties.SocialProperties; +import org.dromara.common.social.utils.AuthRedisStateCache; +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.service.ISocialUserService; import org.dromara.system.service.ISysConfigService; import org.dromara.system.service.ISysTenantService; import org.dromara.web.domain.vo.LoginTenantVo; @@ -26,8 +42,10 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import java.io.IOException; import java.net.URL; import java.util.List; +import java.util.Map; /** * 璁よ瘉 @@ -41,10 +59,14 @@ @RequestMapping("/auth") public class AuthController { + private final SocialProperties socialProperties; private final SysLoginService loginService; private final SysRegisterService registerService; private final ISysConfigService configService; private final ISysTenantService tenantService; + private final ISocialUserService socialUserService; + + /** * 鐧诲綍鏂规硶 @@ -115,6 +137,71 @@ return R.ok(loginVo); } + + /** + * 璁よ瘉鎺堟潈 + * @param source + */ + @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 (socialUserService.isExistByUserIdAndSource(userLoding.getUserId(),source)) + { + return R.fail(source + "骞冲彴璐﹀彿宸茬粡琚处鍙风粦瀹�"); + } + ConfigProperties obj = socialProperties.getType().get(source); + if (ObjectUtil.isNull(obj)){ + return R.fail(source + "骞冲彴璐﹀彿鏆備笉鏀寔"); + } + 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 + */ + @SuppressWarnings("unchecked") + @GetMapping("/social-login/{source}") + public R<String> socialLogin(@PathVariable("source") String source, AuthCallback callback, HttpServletRequest request) throws IOException { + ConfigProperties obj = socialProperties.getType().get(source); + if (ObjectUtil.isNull(obj)){ + return R.fail(source + "骞冲彴璐﹀彿鏆備笉鏀寔"); + } + 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("鍙栨秷鎺堟潈澶辫触"); + } + + + + + /** * 閫�鍑虹櫥褰� */ diff --git a/ruoyi-admin/src/main/resources/application-dev.yml b/ruoyi-admin/src/main/resources/application-dev.yml index 92a3640..78cad6e 100644 --- a/ruoyi-admin/src/main/resources/application-dev.yml +++ b/ruoyi-admin/src/main/resources/application-dev.yml @@ -169,3 +169,151 @@ signName: 娴嬭瘯 # 鑵捐涓撶敤 sdkAppId: + +justauth: + enabled: true + type: + QQ: + client-id: 10**********6 + client-secret: 1f7d08**********5b7**********29e + redirect-uri: http://oauth.xkcoding.com/demo/oauth/qq/callback + union-id: false + WEIBO: + client-id: 10**********6 + client-secret: 1f7d08**********5b7**********29e + redirect-uri: http://oauth.xkcoding.com/demo/oauth/weibo/callback + gitee: + client-id: 38eaaa1b77b5e064313057a2f5745ce3a9f3e7686d9bd302c7df2f308ef6db81 + client-secret: 2e633af8780cb9fe002c4c7291b722db944402e271efb99b062811f52d7da1ff + redirect-uri: http://localhost:8888/social-login?source=gitee + DINGTALK: + client-id: 10**********6 + client-secret: 1f7d08**********5b7**********29e + redirect-uri: http://oauth.xkcoding.com/demo/oauth/dingtalk/callback + BAIDU: + client-id: 10**********6 + client-secret: 1f7d08**********5b7**********29e + redirect-uri: http://oauth.xkcoding.com/demo/oauth/baidu/callback + CSDN: + client-id: 10**********6 + client-secret: 1f7d08**********5b7**********29e + redirect-uri: http://oauth.xkcoding.com/demo/oauth/csdn/callback + CODING: + client-id: 10**********6 + client-secret: 1f7d08**********5b7**********29e + redirect-uri: http://oauth.xkcoding.com/demo/oauth/coding/callback + coding-group-name: xx + OSCHINA: + client-id: 10**********6 + client-secret: 1f7d08**********5b7**********29e + redirect-uri: http://oauth.xkcoding.com/demo/oauth/oschina/callback + ALIPAY: + client-id: 10**********6 + client-secret: 1f7d08**********5b7**********29e + redirect-uri: http://oauth.xkcoding.com/demo/oauth/alipay/callback + alipay-public-key: MIIB**************DAQAB + WECHAT_OPEN: + client-id: 10**********6 + client-secret: 1f7d08**********5b7**********29e + redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat_open/callback + WECHAT_MP: + client-id: 10**********6 + client-secret: 1f7d08**********5b7**********29e + redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat_mp/callback + WECHAT_ENTERPRISE: + client-id: 10**********6 + client-secret: 1f7d08**********5b7**********29e + redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat_enterprise/callback + agent-id: 1000002 + TAOBAO: + 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 + 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..dbadfc2 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,12 @@ /** * 灏忕▼搴忕 */ - XCX("xcx"); + XCX("xcx"), + + /** + * social绗笁鏂圭 + */ + SOCIAL("social"); private final String device; } diff --git a/ruoyi-common/ruoyi-common-social/src/main/java/org/dromara/common/social/config/properties/ConfigProperties.java b/ruoyi-common/ruoyi-common-social/src/main/java/org/dromara/common/social/config/properties/ConfigProperties.java new file mode 100644 index 0000000..813b03a --- /dev/null +++ b/ruoyi-common/ruoyi-common-social/src/main/java/org/dromara/common/social/config/properties/ConfigProperties.java @@ -0,0 +1,58 @@ +package org.dromara.common.social.config.properties; + +import lombok.Data; + +@Data +public class ConfigProperties { + + /** + * 搴旂敤 ID + */ + private String clientId; + + /** + * 搴旂敤瀵嗛挜 + */ + private String clientSecret; + + /** + * 鍥炶皟鍦板潃 + */ + private String redirectUri; + + /** + * 鏄惁鑾峰彇unionId + */ + private boolean unionId; + + /** + * Coding 浼佷笟鍚嶇О + */ + private String codingGroupName; + + /** + * 鏀粯瀹濆叕閽� + */ + private String alipayPublicKey; + + /** + * 浼佷笟寰俊搴旂敤ID + */ + private String agentId; + + /** + * stackoverflow api key + */ + private String stackOverflowKey; + + /** + * 璁惧ID + */ + private String deviceId; + + /** + * 瀹㈡埛绔郴缁熺被鍨� + */ + private String clientOsType; + +} diff --git a/ruoyi-common/ruoyi-common-social/src/main/java/org/dromara/common/social/utils/AuthRedisStateCache.java b/ruoyi-common/ruoyi-common-social/src/main/java/org/dromara/common/social/utils/AuthRedisStateCache.java new file mode 100644 index 0000000..f438a1c --- /dev/null +++ b/ruoyi-common/ruoyi-common-social/src/main/java/org/dromara/common/social/utils/AuthRedisStateCache.java @@ -0,0 +1,79 @@ +package org.dromara.common.social.utils; + +import jakarta.annotation.PostConstruct; +import me.zhyd.oauth.cache.AuthStateCache; +import org.dromara.common.redis.utils.RedisUtils; +import org.dromara.common.social.config.properties.SocialProperties; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; + +import java.time.Duration; + +public class AuthRedisStateCache implements AuthStateCache { + + private final SocialProperties socialProperties; + private final RedisTemplate<String, String> redisTemplate; + + private ValueOperations<String, String> valueOperations; + + @PostConstruct + public void init() { + valueOperations = redisTemplate.opsForValue(); + } + + + public AuthRedisStateCache() { + this.socialProperties = new SocialProperties(); + redisTemplate = new RedisTemplate<>(); + } + + /** + * 瀛樺叆缂撳瓨 + * + * @param key 缂撳瓨key + * @param value 缂撳瓨鍐呭 + */ + @Override + public void cache(String key, String value) { + // TODO: 鑷畾涔夊瓨鍏ョ紦瀛� + RedisUtils.setCacheObject(key, value, Duration.ofMillis(socialProperties.getTimeout())); + } + + /** + * 瀛樺叆缂撳瓨 + * + * @param key 缂撳瓨key + * @param value 缂撳瓨鍐呭 + * @param timeout 鎸囧畾缂撳瓨杩囨湡鏃堕棿(姣) + */ + @Override + public void cache(String key, String value, long timeout) { + // TODO: 鑷畾涔夊瓨鍏ョ紦瀛� + RedisUtils.setCacheObject(key, value, Duration.ofMillis(timeout)); + } + + /** + * 鑾峰彇缂撳瓨鍐呭 + * + * @param key 缂撳瓨key + * @return 缂撳瓨鍐呭 + */ + @Override + public String get(String key) { + // TODO: 鑷畾涔夎幏鍙栫紦瀛樺唴瀹� + return RedisUtils.getCacheObject(key); + } + + /** + * 鏄惁瀛樺湪key锛屽鏋滃搴攌ey鐨剉alue鍊煎凡杩囨湡锛屼篃杩斿洖false + * + * @param key 缂撳瓨key + * @return true锛氬瓨鍦╧ey锛屽苟涓攙alue娌¤繃鏈燂紱false锛歬ey涓嶅瓨鍦ㄦ垨鑰呭凡杩囨湡 + */ + @Override + public boolean containsKey(String key) { + // TODO: 鑷畾涔夊垽鏂璳ey鏄惁瀛樺湪 + return RedisUtils.hasKey(key); + } +} -- Gitblit v1.9.3