Commit ee25b8aedbe25289a1dad53763fd33e954eef06a
Committed by
Gitee
!752 使用 justauth 替代现有 yudao-spring-boot-starter-biz-social 依赖,减少 starter
Merge pull request !752 from 芋道源码/feature/justauth-op
Showing
12 changed files
with
20 additions
and
519 deletions
yudao-dependencies/pom.xml
| ... | ... | @@ -70,7 +70,7 @@ |
| 70 | 70 | <aliyun-java-sdk-core.version>4.6.4</aliyun-java-sdk-core.version> |
| 71 | 71 | <aliyun-java-sdk-dysmsapi.version>2.2.1</aliyun-java-sdk-dysmsapi.version> |
| 72 | 72 | <tencentcloud-sdk-java.version>3.1.880</tencentcloud-sdk-java.version> |
| 73 | - <justauth.version>1.0.7</justauth.version> | |
| 73 | + <justauth.version>1.0.8</justauth.version> | |
| 74 | 74 | <jimureport.version>1.6.1</jimureport.version> |
| 75 | 75 | <xercesImpl.version>2.12.2</xercesImpl.version> |
| 76 | 76 | <weixin-java.version>4.5.0</weixin-java.version> |
| ... | ... | @@ -611,6 +611,12 @@ |
| 611 | 611 | <groupId>com.xingyuv</groupId> |
| 612 | 612 | <artifactId>spring-boot-starter-justauth</artifactId> <!-- 社交登陆(例如说,个人微信、企业微信等等) --> |
| 613 | 613 | <version>${justauth.version}</version> |
| 614 | + <exclusions> | |
| 615 | + <exclusion> | |
| 616 | + <groupId>cn.hutool</groupId> | |
| 617 | + <artifactId>hutool-core</artifactId> | |
| 618 | + </exclusion> | |
| 619 | + </exclusions> | |
| 614 | 620 | </dependency> |
| 615 | 621 | |
| 616 | 622 | <dependency> | ... | ... |
yudao-framework/pom.xml
| ... | ... | @@ -32,7 +32,6 @@ |
| 32 | 32 | |
| 33 | 33 | <module>yudao-spring-boot-starter-biz-pay</module> |
| 34 | 34 | <module>yudao-spring-boot-starter-biz-weixin</module> |
| 35 | - <module>yudao-spring-boot-starter-biz-social</module> | |
| 36 | 35 | <module>yudao-spring-boot-starter-biz-tenant</module> |
| 37 | 36 | <module>yudao-spring-boot-starter-biz-data-permission</module> |
| 38 | 37 | <module>yudao-spring-boot-starter-biz-error-code</module> | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-social/pom.xml deleted
| 1 | -<?xml version="1.0" encoding="UTF-8"?> | |
| 2 | -<project xmlns="http://maven.apache.org/POM/4.0.0" | |
| 3 | - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| 4 | - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| 5 | - <parent> | |
| 6 | - <groupId>cn.iocoder.boot</groupId> | |
| 7 | - <artifactId>yudao-framework</artifactId> | |
| 8 | - <version>${revision}</version> | |
| 9 | - </parent> | |
| 10 | - <packaging>jar</packaging> | |
| 11 | - <modelVersion>4.0.0</modelVersion> | |
| 12 | - | |
| 13 | - <artifactId>yudao-spring-boot-starter-biz-social</artifactId> | |
| 14 | - <name>${project.artifactId}</name> | |
| 15 | - | |
| 16 | - <dependencies> | |
| 17 | - <dependency> | |
| 18 | - <groupId>cn.iocoder.boot</groupId> | |
| 19 | - <artifactId>yudao-common</artifactId> | |
| 20 | - </dependency> | |
| 21 | - <!-- Spring 核心 --> | |
| 22 | - <dependency> | |
| 23 | - <groupId>org.springframework.boot</groupId> | |
| 24 | - <artifactId>spring-boot-starter-aop</artifactId> | |
| 25 | - </dependency> | |
| 26 | - <!-- Web 相关 --> | |
| 27 | - <dependency> | |
| 28 | - <groupId>cn.iocoder.boot</groupId> | |
| 29 | - <artifactId>yudao-spring-boot-starter-web</artifactId> | |
| 30 | - </dependency> | |
| 31 | - <!-- spring boot 配置所需依赖 --> | |
| 32 | - <dependency> | |
| 33 | - <groupId>org.springframework.boot</groupId> | |
| 34 | - <artifactId>spring-boot-configuration-processor</artifactId> | |
| 35 | - <optional>true</optional> | |
| 36 | - </dependency> | |
| 37 | - <!-- 三方云服务相关 --> | |
| 38 | - <dependency> | |
| 39 | - <groupId>com.xingyuv</groupId> | |
| 40 | - <artifactId>spring-boot-starter-justauth</artifactId> | |
| 41 | - <exclusions> | |
| 42 | - <exclusion> | |
| 43 | - <groupId>cn.hutool</groupId> | |
| 44 | - <artifactId>hutool-core</artifactId> | |
| 45 | - </exclusion> | |
| 46 | - </exclusions> | |
| 47 | - </dependency> | |
| 48 | - <dependency> | |
| 49 | - <groupId>cn.iocoder.boot</groupId> | |
| 50 | - <artifactId>yudao-spring-boot-starter-redis</artifactId> | |
| 51 | - </dependency> | |
| 52 | - | |
| 53 | - </dependencies> | |
| 54 | - | |
| 55 | - | |
| 56 | -</project> | |
| 57 | 0 | \ No newline at end of file |
yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/config/YudaoSocialAutoConfiguration.java deleted
| 1 | -package cn.iocoder.yudao.framework.social.config; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.social.core.YudaoAuthRequestFactory; | |
| 4 | -import com.xingyuv.http.HttpUtil; | |
| 5 | -import com.xingyuv.http.support.hutool.HutoolImpl; | |
| 6 | -import com.xingyuv.jushauth.cache.AuthStateCache; | |
| 7 | -import com.xingyuv.justauth.autoconfigure.JustAuthProperties; | |
| 8 | -import lombok.extern.slf4j.Slf4j; | |
| 9 | -import org.springframework.boot.autoconfigure.AutoConfiguration; | |
| 10 | -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | |
| 11 | -import org.springframework.boot.context.properties.EnableConfigurationProperties; | |
| 12 | -import org.springframework.context.annotation.Bean; | |
| 13 | -import org.springframework.context.annotation.Primary; | |
| 14 | - | |
| 15 | -/** | |
| 16 | - * 社交自动装配类 | |
| 17 | - * | |
| 18 | - * @author timfruit | |
| 19 | - * @date 2021-10-30 | |
| 20 | - */ | |
| 21 | -@Slf4j | |
| 22 | -@AutoConfiguration | |
| 23 | -@EnableConfigurationProperties(JustAuthProperties.class) | |
| 24 | -public class YudaoSocialAutoConfiguration { | |
| 25 | - | |
| 26 | - @Bean | |
| 27 | - @Primary | |
| 28 | - @ConditionalOnProperty(prefix = "justauth", value = "enabled", havingValue = "true", matchIfMissing = true) | |
| 29 | - public YudaoAuthRequestFactory yudaoAuthRequestFactory(JustAuthProperties properties, AuthStateCache authStateCache) { | |
| 30 | - // 需要修改 HttpUtil 使用的实现,避免类报错 | |
| 31 | - HttpUtil.setHttp(new HutoolImpl()); | |
| 32 | - // 创建 YudaoAuthRequestFactory | |
| 33 | - return new YudaoAuthRequestFactory(properties, authStateCache); | |
| 34 | - } | |
| 35 | - | |
| 36 | -} |
yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/YudaoAuthRequestFactory.java deleted
| 1 | -package cn.iocoder.yudao.framework.social.core; | |
| 2 | - | |
| 3 | -import cn.hutool.core.util.EnumUtil; | |
| 4 | -import cn.hutool.core.util.ReflectUtil; | |
| 5 | -import cn.iocoder.yudao.framework.social.core.enums.AuthExtendSource; | |
| 6 | -import cn.iocoder.yudao.framework.social.core.request.AuthWeChatMiniAppRequest; | |
| 7 | -import cn.iocoder.yudao.framework.social.core.request.AuthWeChatMpRequest; | |
| 8 | -import com.xingyuv.jushauth.cache.AuthStateCache; | |
| 9 | -import com.xingyuv.jushauth.config.AuthConfig; | |
| 10 | -import com.xingyuv.jushauth.config.AuthSource; | |
| 11 | -import com.xingyuv.jushauth.request.AuthRequest; | |
| 12 | -import com.xingyuv.justauth.AuthRequestFactory; | |
| 13 | -import com.xingyuv.justauth.autoconfigure.JustAuthProperties; | |
| 14 | - | |
| 15 | -import java.lang.reflect.Method; | |
| 16 | - | |
| 17 | -import static com.xingyuv.jushauth.config.AuthDefaultSource.WECHAT_MP; | |
| 18 | - | |
| 19 | -/** | |
| 20 | - * 第三方授权拓展 request 工厂类 | |
| 21 | - * 为使得拓展配置 {@link AuthConfig} 和默认配置齐平,所以自定义本工厂类 | |
| 22 | - * | |
| 23 | - * @author timfruit | |
| 24 | - * @date 2021-10-31 | |
| 25 | - */ | |
| 26 | -public class YudaoAuthRequestFactory extends AuthRequestFactory { | |
| 27 | - | |
| 28 | - protected JustAuthProperties properties; | |
| 29 | - protected AuthStateCache authStateCache; | |
| 30 | - | |
| 31 | - /** | |
| 32 | - * 由于父类 configureHttpConfig 方法是 private 修饰,所以获取后,进行反射调用 | |
| 33 | - */ | |
| 34 | - private final Method configureHttpConfigMethod = ReflectUtil.getMethod(AuthRequestFactory.class, | |
| 35 | - "configureHttpConfig", String.class, AuthConfig.class, JustAuthProperties.JustAuthHttpConfig.class); | |
| 36 | - | |
| 37 | - public YudaoAuthRequestFactory(JustAuthProperties properties, AuthStateCache authStateCache) { | |
| 38 | - super(properties, authStateCache); | |
| 39 | - this.properties = properties; | |
| 40 | - this.authStateCache = authStateCache; | |
| 41 | - } | |
| 42 | - | |
| 43 | - /** | |
| 44 | - * 返回 AuthRequest 对象 | |
| 45 | - * | |
| 46 | - * @param source {@link AuthSource} | |
| 47 | - * @return {@link AuthRequest} | |
| 48 | - */ | |
| 49 | - @Override | |
| 50 | - public AuthRequest get(String source) { | |
| 51 | - // 先尝试获取自定义扩展的 | |
| 52 | - AuthRequest authRequest = getExtendRequest(source); | |
| 53 | - // 找不到,使用默认拓展 | |
| 54 | - if (authRequest == null) { | |
| 55 | - authRequest = super.get(source); | |
| 56 | - } | |
| 57 | - return authRequest; | |
| 58 | - } | |
| 59 | - | |
| 60 | - protected AuthRequest getExtendRequest(String source) { | |
| 61 | - // TODO 芋艿:临时兼容 justauth 迁移的类型不对问题; | |
| 62 | - if (WECHAT_MP.name().equalsIgnoreCase(source)) { | |
| 63 | - AuthConfig config = properties.getType().get(WECHAT_MP.name()); | |
| 64 | - return new AuthWeChatMpRequest(config, authStateCache); | |
| 65 | - } | |
| 66 | - | |
| 67 | - AuthExtendSource authExtendSource; | |
| 68 | - try { | |
| 69 | - authExtendSource = EnumUtil.fromString(AuthExtendSource.class, source.toUpperCase()); | |
| 70 | - } catch (IllegalArgumentException e) { | |
| 71 | - // 无自定义匹配 | |
| 72 | - return null; | |
| 73 | - } | |
| 74 | - | |
| 75 | - // 拓展配置和默认配置齐平,properties 放在一起 | |
| 76 | - AuthConfig config = properties.getType().get(authExtendSource.name()); | |
| 77 | - // 找不到对应关系,直接返回空 | |
| 78 | - if (config == null) { | |
| 79 | - return null; | |
| 80 | - } | |
| 81 | - // 反射调用,配置 http config | |
| 82 | - ReflectUtil.invoke(this, configureHttpConfigMethod, authExtendSource.name(), config, properties.getHttpConfig()); | |
| 83 | - | |
| 84 | - // 获得拓展的 Request | |
| 85 | - // noinspection SwitchStatementWithTooFewBranches | |
| 86 | - switch (authExtendSource) { | |
| 87 | - case WECHAT_MINI_APP: | |
| 88 | - return new AuthWeChatMiniAppRequest(config, authStateCache); | |
| 89 | - default: | |
| 90 | - return null; | |
| 91 | - } | |
| 92 | - } | |
| 93 | - | |
| 94 | -} |
yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/enums/AuthExtendSource.java deleted
| 1 | -package cn.iocoder.yudao.framework.social.core.enums; | |
| 2 | - | |
| 3 | -import com.xingyuv.jushauth.config.AuthSource; | |
| 4 | -import com.xingyuv.jushauth.request.AuthDefaultRequest; | |
| 5 | - | |
| 6 | -/** | |
| 7 | - * 拓展 JustAuth 各 api 需要的 url, 用枚举类分平台类型管理 | |
| 8 | - * | |
| 9 | - * 默认配置 {@link com.xingyuv.jushauth.config.AuthDefaultSource} | |
| 10 | - * | |
| 11 | - * @author timfruit | |
| 12 | - */ | |
| 13 | -public enum AuthExtendSource implements AuthSource { | |
| 14 | - | |
| 15 | - /** | |
| 16 | - * 微信小程序授权登录 | |
| 17 | - */ | |
| 18 | - WECHAT_MINI_APP { | |
| 19 | - | |
| 20 | - @Override | |
| 21 | - public String authorize() { | |
| 22 | - // 参见 https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html 文档 | |
| 23 | - throw new UnsupportedOperationException("不支持获取授权 url,请使用小程序内置函数 wx.login() 登录获取 code"); | |
| 24 | - } | |
| 25 | - | |
| 26 | - @Override | |
| 27 | - public String accessToken() { | |
| 28 | - // 参见 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html 文档 | |
| 29 | - // 获取 openid, unionId , session_key 等字段 | |
| 30 | - return "https://api.weixin.qq.com/sns/jscode2session"; | |
| 31 | - } | |
| 32 | - | |
| 33 | - @Override | |
| 34 | - public String userInfo() { | |
| 35 | - // 参见 https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html 文档 | |
| 36 | - throw new UnsupportedOperationException("不支持获取用户信息 url,请使用小程序内置函数 wx.getUserProfile() 获取用户信息"); | |
| 37 | - } | |
| 38 | - | |
| 39 | - @Override | |
| 40 | - public Class<? extends AuthDefaultRequest> getTargetClass() { | |
| 41 | - return null; | |
| 42 | - } | |
| 43 | - } | |
| 44 | - | |
| 45 | -} |
yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/request/AuthWeChatMiniAppRequest.java deleted
| 1 | -package cn.iocoder.yudao.framework.social.core.request; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | |
| 4 | -import cn.iocoder.yudao.framework.social.core.enums.AuthExtendSource; | |
| 5 | -import com.fasterxml.jackson.annotation.JsonProperty; | |
| 6 | -import com.xingyuv.jushauth.cache.AuthStateCache; | |
| 7 | -import com.xingyuv.jushauth.config.AuthConfig; | |
| 8 | -import com.xingyuv.jushauth.exception.AuthException; | |
| 9 | -import com.xingyuv.jushauth.model.AuthCallback; | |
| 10 | -import com.xingyuv.jushauth.model.AuthToken; | |
| 11 | -import com.xingyuv.jushauth.model.AuthUser; | |
| 12 | -import com.xingyuv.jushauth.request.AuthDefaultRequest; | |
| 13 | -import com.xingyuv.jushauth.utils.HttpUtils; | |
| 14 | -import com.xingyuv.jushauth.utils.UrlBuilder; | |
| 15 | -import lombok.Data; | |
| 16 | - | |
| 17 | -/** | |
| 18 | - * 微信小程序登陆 Request 请求 | |
| 19 | - * | |
| 20 | - * 由于 JustAuth 定位是面向 Web 为主的三方登录,所以微信小程序只能自己封装 | |
| 21 | - * | |
| 22 | - * @author timfruit | |
| 23 | - * @date 2021-10-29 | |
| 24 | - */ | |
| 25 | -public class AuthWeChatMiniAppRequest extends AuthDefaultRequest { | |
| 26 | - | |
| 27 | - public AuthWeChatMiniAppRequest(AuthConfig config, AuthStateCache authStateCache) { | |
| 28 | - super(config, AuthExtendSource.WECHAT_MINI_APP, authStateCache); | |
| 29 | - } | |
| 30 | - | |
| 31 | - @Override | |
| 32 | - protected AuthToken getAccessToken(AuthCallback authCallback) { | |
| 33 | - // 参见 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html 文档 | |
| 34 | - // 使用 code 获取对应的 openId、unionId 等字段 | |
| 35 | - String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl(authCallback.getCode())).getBody(); | |
| 36 | - JSCode2SessionResponse accessTokenObject = JsonUtils.parseObject(response, JSCode2SessionResponse.class); | |
| 37 | - assert accessTokenObject != null; | |
| 38 | - checkResponse(accessTokenObject); | |
| 39 | - // 拼装结果 | |
| 40 | - return AuthToken.builder() | |
| 41 | - .openId(accessTokenObject.getOpenid()) | |
| 42 | - .unionId(accessTokenObject.getUnionId()) | |
| 43 | - .build(); | |
| 44 | - } | |
| 45 | - | |
| 46 | - @Override | |
| 47 | - protected AuthUser getUserInfo(AuthToken authToken) { | |
| 48 | - // 参见 https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html 文档 | |
| 49 | - // 如果需要用户信息,需要在小程序调用函数后传给后端 | |
| 50 | - return AuthUser.builder() | |
| 51 | - .username("") | |
| 52 | - .nickname("") | |
| 53 | - .avatar("") | |
| 54 | - .uuid(authToken.getOpenId()) | |
| 55 | - .token(authToken) | |
| 56 | - .source(source.toString()) | |
| 57 | - .build(); | |
| 58 | - } | |
| 59 | - | |
| 60 | - /** | |
| 61 | - * 检查响应内容是否正确 | |
| 62 | - * | |
| 63 | - * @param response 请求响应内容 | |
| 64 | - */ | |
| 65 | - private void checkResponse(JSCode2SessionResponse response) { | |
| 66 | - if (response.getErrorCode() != 0) { | |
| 67 | - throw new AuthException(response.getErrorCode(), response.getErrorMsg()); | |
| 68 | - } | |
| 69 | - } | |
| 70 | - | |
| 71 | - @Override | |
| 72 | - protected String accessTokenUrl(String code) { | |
| 73 | - return UrlBuilder.fromBaseUrl(source.accessToken()) | |
| 74 | - .queryParam("appid", config.getClientId()) | |
| 75 | - .queryParam("secret", config.getClientSecret()) | |
| 76 | - .queryParam("js_code", code) | |
| 77 | - .queryParam("grant_type", "authorization_code") | |
| 78 | - .build(); | |
| 79 | - } | |
| 80 | - | |
| 81 | - @Data | |
| 82 | - @SuppressWarnings("SpellCheckingInspection") | |
| 83 | - private static class JSCode2SessionResponse { | |
| 84 | - | |
| 85 | - @JsonProperty("errcode") | |
| 86 | - private int errorCode; | |
| 87 | - @JsonProperty("errmsg") | |
| 88 | - private String errorMsg; | |
| 89 | - @JsonProperty("session_key") | |
| 90 | - private String sessionKey; | |
| 91 | - private String openid; | |
| 92 | - @JsonProperty("unionid") | |
| 93 | - private String unionId; | |
| 94 | - | |
| 95 | - } | |
| 96 | - | |
| 97 | -} |
yudao-framework/yudao-spring-boot-starter-biz-social/src/main/java/cn/iocoder/yudao/framework/social/core/request/AuthWeChatMpRequest.java deleted
| 1 | -package cn.iocoder.yudao.framework.social.core.request; | |
| 2 | - | |
| 3 | -import com.alibaba.fastjson.JSONObject; | |
| 4 | -import com.xingyuv.jushauth.cache.AuthStateCache; | |
| 5 | -import com.xingyuv.jushauth.config.AuthConfig; | |
| 6 | -import com.xingyuv.jushauth.config.AuthDefaultSource; | |
| 7 | -import com.xingyuv.jushauth.enums.AuthResponseStatus; | |
| 8 | -import com.xingyuv.jushauth.enums.AuthUserGender; | |
| 9 | -import com.xingyuv.jushauth.enums.scope.AuthWechatMpScope; | |
| 10 | -import com.xingyuv.jushauth.exception.AuthException; | |
| 11 | -import com.xingyuv.jushauth.model.AuthCallback; | |
| 12 | -import com.xingyuv.jushauth.model.AuthResponse; | |
| 13 | -import com.xingyuv.jushauth.model.AuthToken; | |
| 14 | -import com.xingyuv.jushauth.model.AuthUser; | |
| 15 | -import com.xingyuv.jushauth.request.AuthDefaultRequest; | |
| 16 | -import com.xingyuv.jushauth.utils.AuthScopeUtils; | |
| 17 | -import com.xingyuv.jushauth.utils.GlobalAuthUtils; | |
| 18 | -import com.xingyuv.jushauth.utils.HttpUtils; | |
| 19 | -import com.xingyuv.jushauth.utils.UrlBuilder; | |
| 20 | - | |
| 21 | -/** | |
| 22 | - * 微信公众平台登录 | |
| 23 | - * | |
| 24 | - * @author yangkai.shen (https://xkcoding.com) | |
| 25 | - * @since 1.1.0 | |
| 26 | - */ | |
| 27 | -public class AuthWeChatMpRequest extends AuthDefaultRequest { | |
| 28 | - public AuthWeChatMpRequest(AuthConfig config) { | |
| 29 | - super(config, AuthDefaultSource.WECHAT_MP); | |
| 30 | - } | |
| 31 | - | |
| 32 | - public AuthWeChatMpRequest(AuthConfig config, AuthStateCache authStateCache) { | |
| 33 | - super(config, AuthDefaultSource.WECHAT_MP, authStateCache); | |
| 34 | - } | |
| 35 | - | |
| 36 | - /** | |
| 37 | - * 微信的特殊性,此时返回的信息同时包含 openid 和 access_token | |
| 38 | - * | |
| 39 | - * @param authCallback 回调返回的参数 | |
| 40 | - * @return 所有信息 | |
| 41 | - */ | |
| 42 | - @Override | |
| 43 | - protected AuthToken getAccessToken(AuthCallback authCallback) { | |
| 44 | - return this.getToken(accessTokenUrl(authCallback.getCode())); | |
| 45 | - } | |
| 46 | - | |
| 47 | - @Override | |
| 48 | - protected AuthUser getUserInfo(AuthToken authToken) { | |
| 49 | - String openId = authToken.getOpenId(); | |
| 50 | - | |
| 51 | - String response = doGetUserInfo(authToken); | |
| 52 | - JSONObject object = JSONObject.parseObject(response); | |
| 53 | - | |
| 54 | - this.checkResponse(object); | |
| 55 | - | |
| 56 | - String location = String.format("%s-%s-%s", object.getString("country"), object.getString("province"), object.getString("city")); | |
| 57 | - | |
| 58 | - if (object.containsKey("unionid")) { | |
| 59 | - authToken.setUnionId(object.getString("unionid")); | |
| 60 | - } | |
| 61 | - | |
| 62 | - return AuthUser.builder() | |
| 63 | - .rawUserInfo(object) | |
| 64 | - .username(object.getString("nickname")) | |
| 65 | - .nickname(object.getString("nickname")) | |
| 66 | - .avatar(object.getString("headimgurl")) | |
| 67 | - .location(location) | |
| 68 | - .uuid(openId) | |
| 69 | - .gender(AuthUserGender.getWechatRealGender(object.getString("sex"))) | |
| 70 | - .token(authToken) | |
| 71 | - .source(source.toString()) | |
| 72 | - .build(); | |
| 73 | - } | |
| 74 | - | |
| 75 | - @Override | |
| 76 | - public AuthResponse refresh(AuthToken oldToken) { | |
| 77 | - return AuthResponse.builder() | |
| 78 | - .code(AuthResponseStatus.SUCCESS.getCode()) | |
| 79 | - .data(this.getToken(refreshTokenUrl(oldToken.getRefreshToken()))) | |
| 80 | - .build(); | |
| 81 | - } | |
| 82 | - | |
| 83 | - /** | |
| 84 | - * 检查响应内容是否正确 | |
| 85 | - * | |
| 86 | - * @param object 请求响应内容 | |
| 87 | - */ | |
| 88 | - private void checkResponse(JSONObject object) { | |
| 89 | - if (object.containsKey("errcode")) { | |
| 90 | - throw new AuthException(object.getIntValue("errcode"), object.getString("errmsg")); | |
| 91 | - } | |
| 92 | - } | |
| 93 | - | |
| 94 | - /** | |
| 95 | - * 获取token,适用于获取access_token和刷新token | |
| 96 | - * | |
| 97 | - * @param accessTokenUrl 实际请求token的地址 | |
| 98 | - * @return token对象 | |
| 99 | - */ | |
| 100 | - private AuthToken getToken(String accessTokenUrl) { | |
| 101 | - String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl).getBody(); | |
| 102 | - JSONObject accessTokenObject = JSONObject.parseObject(response); | |
| 103 | - | |
| 104 | - this.checkResponse(accessTokenObject); | |
| 105 | - | |
| 106 | - return AuthToken.builder() | |
| 107 | - .accessToken(accessTokenObject.getString("access_token")) | |
| 108 | - .refreshToken(accessTokenObject.getString("refresh_token")) | |
| 109 | - .expireIn(accessTokenObject.getIntValue("expires_in")) | |
| 110 | - .openId(accessTokenObject.getString("openid")) | |
| 111 | - .scope(accessTokenObject.getString("scope")) | |
| 112 | - .build(); | |
| 113 | - } | |
| 114 | - | |
| 115 | - /** | |
| 116 | - * 返回带{@code state}参数的授权url,授权回调时会带上这个{@code state} | |
| 117 | - * | |
| 118 | - * @param state state 验证授权流程的参数,可以防止csrf | |
| 119 | - * @return 返回授权地址 | |
| 120 | - * @since 1.9.3 | |
| 121 | - */ | |
| 122 | - @Override | |
| 123 | - public String authorize(String state) { | |
| 124 | - return UrlBuilder.fromBaseUrl(source.authorize()) | |
| 125 | - .queryParam("appid", config.getClientId()) | |
| 126 | - .queryParam("redirect_uri", GlobalAuthUtils.urlEncode(config.getRedirectUri())) | |
| 127 | - .queryParam("response_type", "code") | |
| 128 | - .queryParam("scope", this.getScopes(",", false, AuthScopeUtils.getDefaultScopes(AuthWechatMpScope.values()))) | |
| 129 | - .queryParam("state", getRealState(state).concat("#wechat_redirect")) | |
| 130 | - .build(); | |
| 131 | - } | |
| 132 | - | |
| 133 | - /** | |
| 134 | - * 返回获取accessToken的url | |
| 135 | - * | |
| 136 | - * @param code 授权码 | |
| 137 | - * @return 返回获取accessToken的url | |
| 138 | - */ | |
| 139 | - @Override | |
| 140 | - protected String accessTokenUrl(String code) { | |
| 141 | - return UrlBuilder.fromBaseUrl(source.accessToken()) | |
| 142 | - .queryParam("appid", config.getClientId()) | |
| 143 | - .queryParam("secret", config.getClientSecret()) | |
| 144 | - .queryParam("code", code) | |
| 145 | - .queryParam("grant_type", "authorization_code") | |
| 146 | - .build(); | |
| 147 | - } | |
| 148 | - | |
| 149 | - /** | |
| 150 | - * 返回获取userInfo的url | |
| 151 | - * | |
| 152 | - * @param authToken 用户授权后的token | |
| 153 | - * @return 返回获取userInfo的url | |
| 154 | - */ | |
| 155 | - @Override | |
| 156 | - protected String userInfoUrl(AuthToken authToken) { | |
| 157 | - return UrlBuilder.fromBaseUrl(source.userInfo()) | |
| 158 | - .queryParam("access_token", authToken.getAccessToken()) | |
| 159 | - .queryParam("openid", authToken.getOpenId()) | |
| 160 | - .queryParam("lang", "zh_CN") | |
| 161 | - .build(); | |
| 162 | - } | |
| 163 | - | |
| 164 | - /** | |
| 165 | - * 返回获取userInfo的url | |
| 166 | - * | |
| 167 | - * @param refreshToken getAccessToken方法返回的refreshToken | |
| 168 | - * @return 返回获取userInfo的url | |
| 169 | - */ | |
| 170 | - @Override | |
| 171 | - protected String refreshTokenUrl(String refreshToken) { | |
| 172 | - return UrlBuilder.fromBaseUrl(source.refresh()) | |
| 173 | - .queryParam("appid", config.getClientId()) | |
| 174 | - .queryParam("grant_type", "refresh_token") | |
| 175 | - .queryParam("refresh_token", refreshToken) | |
| 176 | - .build(); | |
| 177 | - } | |
| 178 | -} |
yudao-framework/yudao-spring-boot-starter-biz-social/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports deleted
yudao-module-system/yudao-module-system-biz/pom.xml
| ... | ... | @@ -48,10 +48,6 @@ |
| 48 | 48 | </dependency> |
| 49 | 49 | <dependency> |
| 50 | 50 | <groupId>cn.iocoder.boot</groupId> |
| 51 | - <artifactId>yudao-spring-boot-starter-biz-social</artifactId> | |
| 52 | - </dependency> | |
| 53 | - <dependency> | |
| 54 | - <groupId>cn.iocoder.boot</groupId> | |
| 55 | 51 | <artifactId>yudao-spring-boot-starter-biz-tenant</artifactId> |
| 56 | 52 | </dependency> |
| 57 | 53 | <dependency> |
| ... | ... | @@ -119,6 +115,13 @@ |
| 119 | 115 | <groupId>org.springframework.boot</groupId> |
| 120 | 116 | <artifactId>spring-boot-starter-mail</artifactId> |
| 121 | 117 | </dependency> |
| 118 | + | |
| 119 | + <!-- 三方云服务相关 --> | |
| 120 | + <dependency> | |
| 121 | + <groupId>com.xingyuv</groupId> | |
| 122 | + <artifactId>spring-boot-starter-justauth</artifactId> <!-- 社交登陆(例如说,个人微信、企业微信等等) --> | |
| 123 | + </dependency> | |
| 124 | + | |
| 122 | 125 | </dependencies> |
| 123 | 126 | |
| 124 | 127 | </project> | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java
| ... | ... | @@ -12,7 +12,6 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 12 | 12 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 13 | 13 | import cn.iocoder.yudao.framework.common.util.cache.CacheUtils; |
| 14 | 14 | import cn.iocoder.yudao.framework.common.util.http.HttpUtils; |
| 15 | -import cn.iocoder.yudao.framework.social.core.YudaoAuthRequestFactory; | |
| 16 | 15 | import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientCreateReqVO; |
| 17 | 16 | import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientPageReqVO; |
| 18 | 17 | import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientUpdateReqVO; |
| ... | ... | @@ -31,6 +30,7 @@ import com.xingyuv.jushauth.model.AuthResponse; |
| 31 | 30 | import com.xingyuv.jushauth.model.AuthUser; |
| 32 | 31 | import com.xingyuv.jushauth.request.AuthRequest; |
| 33 | 32 | import com.xingyuv.jushauth.utils.AuthStateUtils; |
| 33 | +import com.xingyuv.justauth.AuthRequestFactory; | |
| 34 | 34 | import lombok.SneakyThrows; |
| 35 | 35 | import lombok.extern.slf4j.Slf4j; |
| 36 | 36 | import me.chanjar.weixin.common.bean.WxJsapiSignature; |
| ... | ... | @@ -59,8 +59,8 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 59 | 59 | @Slf4j |
| 60 | 60 | public class SocialClientServiceImpl implements SocialClientService { |
| 61 | 61 | |
| 62 | - @Resource // 由于自定义了 YudaoAuthRequestFactory 无法覆盖默认的 AuthRequestFactory,所以只能注入它 | |
| 63 | - private YudaoAuthRequestFactory yudaoAuthRequestFactory; | |
| 62 | + @Resource | |
| 63 | + private AuthRequestFactory authRequestFactory; | |
| 64 | 64 | |
| 65 | 65 | @Resource |
| 66 | 66 | private WxMpService wxMpService; |
| ... | ... | @@ -145,7 +145,7 @@ public class SocialClientServiceImpl implements SocialClientService { |
| 145 | 145 | */ |
| 146 | 146 | private AuthRequest buildAuthRequest(Integer socialType, Integer userType) { |
| 147 | 147 | // 1. 先查找默认的配置项,从 application-*.yaml 中读取 |
| 148 | - AuthRequest request = yudaoAuthRequestFactory.get(SocialTypeEnum.valueOfType(socialType).getSource()); | |
| 148 | + AuthRequest request = authRequestFactory.get(SocialTypeEnum.valueOfType(socialType).getSource()); | |
| 149 | 149 | Assert.notNull(request, String.format("社交平台(%d) 不存在", socialType)); |
| 150 | 150 | // 2. 查询 DB 的配置项,如果存在则进行覆盖 |
| 151 | 151 | SocialClientDO client = socialClientMapper.selectBySocialTypeAndUserType(socialType, userType); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImplTest.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.social; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 4 | -import cn.iocoder.yudao.framework.social.core.YudaoAuthRequestFactory; | |
| 5 | 4 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 6 | 5 | import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; |
| 7 | 6 | import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO; |
| ... | ... | @@ -15,6 +14,7 @@ import com.xingyuv.jushauth.model.AuthCallback; |
| 15 | 14 | import com.xingyuv.jushauth.model.AuthResponse; |
| 16 | 15 | import com.xingyuv.jushauth.model.AuthUser; |
| 17 | 16 | import com.xingyuv.jushauth.request.AuthRequest; |
| 17 | +import com.xingyuv.justauth.AuthRequestFactory; | |
| 18 | 18 | import org.junit.jupiter.api.Disabled; |
| 19 | 19 | import org.junit.jupiter.api.Test; |
| 20 | 20 | import org.springframework.boot.test.mock.mockito.MockBean; |
| ... | ... | @@ -46,7 +46,7 @@ public class SocialUserServiceImplTest extends BaseDbUnitTest { |
| 46 | 46 | private SocialUserBindMapper socialUserBindMapper; |
| 47 | 47 | |
| 48 | 48 | @MockBean |
| 49 | - private YudaoAuthRequestFactory authRequestFactory; | |
| 49 | + private AuthRequestFactory authRequestFactory; | |
| 50 | 50 | |
| 51 | 51 | // TODO 芋艿:后续统一修复 |
| 52 | 52 | // @Test | ... | ... |