From 4cebe4af1424b11ec6c959c5562673ca4a60c4a8 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Wed, 30 Apr 2025 00:05:54 +0800 Subject: [PATCH] =?UTF-8?q?reactor=EF=BC=9A=E4=BD=BF=E7=94=A8=20justauth?= =?UTF-8?q?=20=E5=AE=98=E6=96=B9=201.16.7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yudao-dependencies/pom.xml | 18 +- .../BrokerageWithdrawServiceImpl.java | 2 +- .../service/auth/MemberAuthServiceImpl.java | 5 +- .../system/enums/social/SocialTypeEnum.java | 2 +- .../yudao-module-system-biz/pom.xml | 8 +- .../api/social/SocialClientApiImpl.java | 2 +- .../dal/dataobject/social/SocialClientDO.java | 2 +- .../config/YudaoJustAuthConfiguration.java | 39 +++ .../justauth/core/AuthRequestFactory.java | 321 ++++++++++++++++++ .../framework/justauth/package-info.java | 6 + .../service/social/SocialClientService.java | 2 +- .../social/SocialClientServiceImpl.java | 16 +- .../service/social/SocialUserServiceImpl.java | 7 +- .../social/SocialClientServiceImplTest.java | 25 +- .../social/SocialUserServiceImplTest.java | 4 +- .../src/main/resources/application-dev.yaml | 2 +- .../src/main/resources/application-local.yaml | 10 +- 17 files changed, 419 insertions(+), 52 deletions(-) create mode 100644 yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/justauth/config/YudaoJustAuthConfiguration.java create mode 100644 yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/justauth/core/AuthRequestFactory.java create mode 100644 yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/justauth/package-info.java diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml index c9ebf24e58..90ce8aef22 100644 --- a/yudao-dependencies/pom.xml +++ b/yudao-dependencies/pom.xml @@ -72,7 +72,8 @@ 2.17.0 1.27.1 2.30.14 - 2.0.5 + 1.16.7 + 1.4.0 1.8.1 4.7.2.B @@ -559,15 +560,14 @@ - com.xingyuv - spring-boot-starter-justauth + me.zhyd.oauth + JustAuth ${justauth.version} - - - cn.hutool - hutool-core - - + + + com.xkcoding.justauth + justauth-spring-boot-starter + ${justauth-starter.version} diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/brokerage/BrokerageWithdrawServiceImpl.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/brokerage/BrokerageWithdrawServiceImpl.java index 390a1fcde7..2eef855b6e 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/brokerage/BrokerageWithdrawServiceImpl.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/brokerage/BrokerageWithdrawServiceImpl.java @@ -135,7 +135,7 @@ public class BrokerageWithdrawServiceImpl implements BrokerageWithdrawService { private Long createPayTransfer(BrokerageWithdrawDO withdraw) { // 1.1 获取微信 openid SocialUserRespDTO socialUser = socialUserApi.getSocialUserByUserId( - UserTypeEnum.MEMBER.getValue(), withdraw.getUserId(), SocialTypeEnum.WECHAT_MINI_APP.getType()); + UserTypeEnum.MEMBER.getValue(), withdraw.getUserId(), SocialTypeEnum.WECHAT_MINI_PROGRAM.getType()); // TODO @luchi:这里,需要校验非空。如果空的话,要有业务异常哈; // 1.2 构建请求 PayTransferCreateReqDTO payTransferCreateReqDTO = new PayTransferCreateReqDTO() diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java index 69d9252b89..45aa150480 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java @@ -1,7 +1,6 @@ package cn.iocoder.yudao.module.member.service.auth; import cn.hutool.core.lang.Assert; -import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.enums.TerminalEnum; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; @@ -27,11 +26,11 @@ import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum; import cn.iocoder.yudao.module.system.enums.oauth2.OAuth2ClientConstants; import cn.iocoder.yudao.module.system.enums.sms.SmsSceneEnum; import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; +import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import jakarta.annotation.Resource; import java.util.Objects; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; @@ -147,7 +146,7 @@ public class MemberAuthServiceImpl implements MemberAuthService { // 绑定社交用户 String openid = socialUserApi.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(), - SocialTypeEnum.WECHAT_MINI_APP.getType(), reqVO.getLoginCode(), reqVO.getState())); + SocialTypeEnum.WECHAT_MINI_PROGRAM.getType(), reqVO.getLoginCode(), reqVO.getState())); // 创建 Token 令牌,记录登录日志 return createTokenAfterLoginSuccess(user, user.getMobile(), LoginLogTypeEnum.LOGIN_SOCIAL, openid); diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/social/SocialTypeEnum.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/social/SocialTypeEnum.java index e94b920b5a..e6c4dc32b5 100644 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/social/SocialTypeEnum.java +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/social/SocialTypeEnum.java @@ -52,7 +52,7 @@ public enum SocialTypeEnum implements ArrayValuable { * * @see 接入文档 */ - WECHAT_MINI_APP(34, "WECHAT_MINI_APP"), + WECHAT_MINI_PROGRAM(34, "WECHAT_MINI_PROGRAM"), ; public static final Integer[] ARRAYS = Arrays.stream(values()).map(SocialTypeEnum::getType).toArray(Integer[]::new); diff --git a/yudao-module-system/yudao-module-system-biz/pom.xml b/yudao-module-system/yudao-module-system-biz/pom.xml index f12ca62711..06a6ae4a06 100644 --- a/yudao-module-system/yudao-module-system-biz/pom.xml +++ b/yudao-module-system/yudao-module-system-biz/pom.xml @@ -97,8 +97,12 @@ - com.xingyuv - spring-boot-starter-justauth + me.zhyd.oauth + JustAuth + + + com.xkcoding.justauth + justauth-spring-boot-starter diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/social/SocialClientApiImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/social/SocialClientApiImpl.java index 08fa53b845..046fc3a0d6 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/social/SocialClientApiImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/social/SocialClientApiImpl.java @@ -84,7 +84,7 @@ public class SocialClientApiImpl implements SocialClientApi { // 2. 获得社交用户 SocialUserRespDTO socialUser = socialUserService.getSocialUserByUserId(reqDTO.getUserType(), reqDTO.getUserId(), - SocialTypeEnum.WECHAT_MINI_APP.getType()); + SocialTypeEnum.WECHAT_MINI_PROGRAM.getType()); if (StrUtil.isBlankIfStr(socialUser.getOpenid())) { log.warn("[sendWxaSubscribeMessage][reqDTO({}) 发送订阅消息失败,原因:会员 openid 缺失]", reqDTO); return; diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/social/SocialClientDO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/social/SocialClientDO.java index 71fc1bc4ae..5449106105 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/social/SocialClientDO.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/social/SocialClientDO.java @@ -7,8 +7,8 @@ import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; import com.baomidou.mybatisplus.annotation.KeySequence; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; -import com.xingyuv.jushauth.config.AuthConfig; import lombok.*; +import me.zhyd.oauth.config.AuthConfig; /** * 社交客户端 DO diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/justauth/config/YudaoJustAuthConfiguration.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/justauth/config/YudaoJustAuthConfiguration.java new file mode 100644 index 0000000000..56a24eed29 --- /dev/null +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/justauth/config/YudaoJustAuthConfiguration.java @@ -0,0 +1,39 @@ +package cn.iocoder.yudao.module.system.framework.justauth.config; + +import cn.iocoder.yudao.module.system.framework.justauth.core.AuthRequestFactory; +import com.xkcoding.justauth.autoconfigure.JustAuthProperties; +import com.xkcoding.justauth.support.cache.RedisStateCache; +import me.zhyd.oauth.cache.AuthStateCache; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.core.RedisTemplate; + +/** + * JustAuth 配置类 TODO 芋艿:等 justauth 1.4.1 版本发布!!! + * + * @author 芋道源码 + */ +@Configuration(proxyBeanMethods = false) +@EnableConfigurationProperties({JustAuthProperties.class}) +public class YudaoJustAuthConfiguration { + + @Bean + @ConditionalOnProperty( + prefix = "justauth", + value = {"enabled"}, + havingValue = "true", + matchIfMissing = true + ) + public AuthRequestFactory authRequestFactory(JustAuthProperties properties, AuthStateCache authStateCache) { + return new AuthRequestFactory(properties, authStateCache); + } + + @Bean + public AuthStateCache authStateCache(RedisTemplate justAuthRedisCacheTemplate, + JustAuthProperties justAuthProperties) { + return new RedisStateCache(justAuthRedisCacheTemplate, justAuthProperties.getCache()); + } + +} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/justauth/core/AuthRequestFactory.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/justauth/core/AuthRequestFactory.java new file mode 100644 index 0000000000..4ae8b78c6c --- /dev/null +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/justauth/core/AuthRequestFactory.java @@ -0,0 +1,321 @@ +/* + * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package cn.iocoder.yudao.module.system.framework.justauth.core; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.EnumUtil; +import cn.hutool.core.util.ReflectUtil; +import cn.hutool.core.util.StrUtil; +import com.xkcoding.http.config.HttpConfig; +import com.xkcoding.justauth.autoconfigure.ExtendProperties; +import com.xkcoding.justauth.autoconfigure.JustAuthProperties; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import me.zhyd.oauth.cache.AuthStateCache; +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.config.AuthDefaultSource; +import me.zhyd.oauth.config.AuthSource; +import me.zhyd.oauth.enums.AuthResponseStatus; +import me.zhyd.oauth.exception.AuthException; +import me.zhyd.oauth.request.*; +import org.springframework.util.CollectionUtils; + +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +// TODO @芋艿:等官方发布 1.4.1!!! +/** + *

+ * AuthRequest工厂类 + *

+ * + * @author yangkai.shen + * @date Created in 2019-07-22 14:21 + */ +@Slf4j +@RequiredArgsConstructor +public class AuthRequestFactory { + private final JustAuthProperties properties; + private final AuthStateCache authStateCache; + + /** + * 返回当前Oauth列表 + * + * @return Oauth列表 + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + public List oauthList() { + // 默认列表 + List defaultList = new ArrayList<>(properties.getType().keySet()); + // 扩展列表 + List extendList = new ArrayList<>(); + ExtendProperties extend = properties.getExtend(); + if (null != extend) { + Class enumClass = extend.getEnumClass(); + List names = EnumUtil.getNames(enumClass); + // 扩展列表 + extendList = extend.getConfig() + .keySet() + .stream() + .filter(x -> names.contains(x.toUpperCase())) + .map(String::toUpperCase) + .collect(Collectors.toList()); + } + + // 合并 + return (List) CollUtil.addAll(defaultList, extendList); + } + + /** + * 返回AuthRequest对象 + * + * @param source {@link AuthSource} + * @return {@link AuthRequest} + */ + public AuthRequest get(String source) { + if (StrUtil.isBlank(source)) { + throw new AuthException(AuthResponseStatus.NO_AUTH_SOURCE); + } + + // 获取 JustAuth 中已存在的 + AuthRequest authRequest = getDefaultRequest(source); + + // 如果获取不到则尝试取自定义的 + if (authRequest == null) { + authRequest = getExtendRequest(properties.getExtend().getEnumClass(), source); + } + + if (authRequest == null) { + throw new AuthException(AuthResponseStatus.UNSUPPORTED); + } + + return authRequest; + } + + /** + * 获取自定义的 request + * + * @param clazz 枚举类 {@link AuthSource} + * @param source {@link AuthSource} + * @return {@link AuthRequest} + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + private AuthRequest getExtendRequest(Class clazz, String source) { + String upperSource = source.toUpperCase(); + try { + EnumUtil.fromString(clazz, upperSource); + } catch (IllegalArgumentException e) { + // 无自定义匹配 + return null; + } + + Map extendConfig = properties.getExtend().getConfig(); + + // key 转大写 + Map upperConfig = new HashMap<>(6); + extendConfig.forEach((k, v) -> upperConfig.put(k.toUpperCase(), v)); + + ExtendProperties.ExtendRequestConfig extendRequestConfig = upperConfig.get(upperSource); + if (extendRequestConfig != null) { + + // 配置 http config + configureHttpConfig(upperSource, extendRequestConfig, properties.getHttpConfig()); + + Class requestClass = extendRequestConfig.getRequestClass(); + + if (requestClass != null) { + // 反射获取 Request 对象,所以必须实现 2 个参数的构造方法 + return ReflectUtil.newInstance(requestClass, (AuthConfig) extendRequestConfig, authStateCache); + } + } + + return null; + } + + + /** + * 获取默认的 Request + * + * @param source {@link AuthSource} + * @return {@link AuthRequest} + */ + private AuthRequest getDefaultRequest(String source) { + AuthDefaultSource authDefaultSource; + + try { + authDefaultSource = EnumUtil.fromString(AuthDefaultSource.class, source.toUpperCase()); + } catch (IllegalArgumentException e) { + // 无自定义匹配 + return null; + } + + AuthConfig config = properties.getType().get(authDefaultSource.name()); + // 找不到对应关系,直接返回空 + if (config == null) { + return null; + } + + // 配置 http config + configureHttpConfig(authDefaultSource.name(), config, properties.getHttpConfig()); + + switch (authDefaultSource) { + case GITHUB: + return new AuthGithubRequest(config, authStateCache); + case WEIBO: + return new AuthWeiboRequest(config, authStateCache); + case GITEE: + return new AuthGiteeRequest(config, authStateCache); + case DINGTALK: + return new AuthDingTalkRequest(config, authStateCache); + case DINGTALK_V2: + return new AuthDingTalkV2Request(config, authStateCache); + case DINGTALK_ACCOUNT: + return new AuthDingTalkAccountRequest(config, authStateCache); + case BAIDU: + return new AuthBaiduRequest(config, authStateCache); + case CSDN: + return new AuthCsdnRequest(config, authStateCache); + case CODING: + return new AuthCodingRequest(config, authStateCache); + case OSCHINA: + return new AuthOschinaRequest(config, authStateCache); + case ALIPAY: + return new AuthAlipayRequest(config, authStateCache); + case QQ: + return new AuthQqRequest(config, authStateCache); + case WECHAT_OPEN: + return new AuthWeChatOpenRequest(config, authStateCache); + case WECHAT_MP: + return new AuthWeChatMpRequest(config, authStateCache); + case TAOBAO: + return new AuthTaobaoRequest(config, authStateCache); + case GOOGLE: + return new AuthGoogleRequest(config, authStateCache); + case FACEBOOK: + return new AuthFacebookRequest(config, authStateCache); + case DOUYIN: + return new AuthDouyinRequest(config, authStateCache); + case LINKEDIN: + return new AuthLinkedinRequest(config, authStateCache); + case MICROSOFT: + return new AuthMicrosoftRequest(config, authStateCache); + case MICROSOFT_CN: + return new AuthMicrosoftCnRequest(config, authStateCache); + + case MI: + return new AuthMiRequest(config, authStateCache); + case TOUTIAO: + return new AuthToutiaoRequest(config, authStateCache); + case TEAMBITION: + return new AuthTeambitionRequest(config, authStateCache); + case RENREN: + return new AuthRenrenRequest(config, authStateCache); + case PINTEREST: + return new AuthPinterestRequest(config, authStateCache); + case STACK_OVERFLOW: + return new AuthStackOverflowRequest(config, authStateCache); + case HUAWEI: + return new AuthHuaweiRequest(config, authStateCache); + case HUAWEI_V3: + return new AuthHuaweiV3Request(config, authStateCache); + case WECHAT_ENTERPRISE: + return new AuthWeChatEnterpriseQrcodeRequest(config, authStateCache); + case WECHAT_ENTERPRISE_V2: + return new AuthWeChatEnterpriseQrcodeV2Request(config, authStateCache); + case WECHAT_ENTERPRISE_QRCODE_THIRD: + return new AuthWeChatEnterpriseThirdQrcodeRequest(config, authStateCache); + case WECHAT_ENTERPRISE_WEB: + return new AuthWeChatEnterpriseWebRequest(config, authStateCache); + case KUJIALE: + return new AuthKujialeRequest(config, authStateCache); + case GITLAB: + return new AuthGitlabRequest(config, authStateCache); + case MEITUAN: + return new AuthMeituanRequest(config, authStateCache); + case ELEME: + return new AuthElemeRequest(config, authStateCache); + case TWITTER: + return new AuthTwitterRequest(config, authStateCache); + case FEISHU: + return new AuthFeishuRequest(config, authStateCache); + case JD: + return new AuthJdRequest(config, authStateCache); + case ALIYUN: + return new AuthAliyunRequest(config, authStateCache); + case XMLY: + return new AuthXmlyRequest(config, authStateCache); + case AMAZON: + return new AuthAmazonRequest(config, authStateCache); + case SLACK: + return new AuthSlackRequest(config, authStateCache); + case LINE: + return new AuthLineRequest(config, authStateCache); + case OKTA: + return new AuthOktaRequest(config, authStateCache); + case PROGINN: + return new AuthProginnRequest(config,authStateCache); + case AFDIAN: + return new AuthAfDianRequest(config,authStateCache); + case APPLE: + return new AuthAppleRequest(config,authStateCache); + case FIGMA: + return new AuthFigmaRequest(config,authStateCache); + case WECHAT_MINI_PROGRAM: + config.setIgnoreCheckRedirectUri(true); + config.setIgnoreCheckState(true); + return new AuthWechatMiniProgramRequest(config, authStateCache); + case QQ_MINI_PROGRAM: + config.setIgnoreCheckRedirectUri(true); + config.setIgnoreCheckState(true); + return new AuthQQMiniProgramRequest(config, authStateCache); + default: + return null; + } + } + + /** + * 配置 http 相关的配置 + * + * @param authSource {@link AuthSource} + * @param authConfig {@link AuthConfig} + */ + private void configureHttpConfig(String authSource, AuthConfig authConfig, JustAuthProperties.JustAuthHttpConfig httpConfig) { + if (null == httpConfig) { + return; + } + Map proxyConfigMap = httpConfig.getProxy(); + if (CollectionUtils.isEmpty(proxyConfigMap)) { + return; + } + JustAuthProperties.JustAuthProxyConfig proxyConfig = proxyConfigMap.get(authSource); + + if (null == proxyConfig) { + return; + } + + authConfig.setHttpConfig(HttpConfig.builder() + .timeout(httpConfig.getTimeout()) + .proxy(new Proxy(Proxy.Type.valueOf(proxyConfig.getType()), new InetSocketAddress(proxyConfig.getHostname(), proxyConfig.getPort()))) + .build()); + } +} \ No newline at end of file diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/justauth/package-info.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/justauth/package-info.java new file mode 100644 index 0000000000..e9af3ab18a --- /dev/null +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/justauth/package-info.java @@ -0,0 +1,6 @@ +/** + * justauth 三方登录的拓展 + * + * @author 芋道源码 + */ +package cn.iocoder.yudao.module.system.framework.justauth; \ No newline at end of file diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientService.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientService.java index 5b293c41c9..40578a6cd0 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientService.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientService.java @@ -8,10 +8,10 @@ import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialCl import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientSaveReqVO; import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialClientDO; import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; -import com.xingyuv.jushauth.model.AuthUser; import jakarta.validation.Valid; import me.chanjar.weixin.common.bean.WxJsapiSignature; import me.chanjar.weixin.common.bean.subscribemsg.TemplateInfo; +import me.zhyd.oauth.model.AuthUser; import java.util.List; diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java index 7095193efb..4d5d13ca27 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java @@ -26,18 +26,12 @@ import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialClientDO; import cn.iocoder.yudao.module.system.dal.mysql.social.SocialClientMapper; import cn.iocoder.yudao.module.system.dal.redis.RedisKeyConstants; import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; +import cn.iocoder.yudao.module.system.framework.justauth.core.AuthRequestFactory; import com.binarywang.spring.starter.wxjava.miniapp.properties.WxMaProperties; import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties; import com.google.common.annotations.VisibleForTesting; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; -import com.xingyuv.jushauth.config.AuthConfig; -import com.xingyuv.jushauth.model.AuthCallback; -import com.xingyuv.jushauth.model.AuthResponse; -import com.xingyuv.jushauth.model.AuthUser; -import com.xingyuv.jushauth.request.AuthRequest; -import com.xingyuv.jushauth.utils.AuthStateUtils; -import com.xingyuv.justauth.AuthRequestFactory; import jakarta.annotation.Resource; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @@ -48,6 +42,12 @@ import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl; +import me.zhyd.oauth.config.AuthConfig; +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.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.Cacheable; import org.springframework.data.redis.core.StringRedisTemplate; @@ -337,7 +337,7 @@ public class SocialClientServiceImpl implements SocialClientService { WxMaService getWxMaService(Integer userType) { // 第一步,查询 DB 的配置项,获得对应的 WxMaService 对象 SocialClientDO client = socialClientMapper.selectBySocialTypeAndUserType( - SocialTypeEnum.WECHAT_MINI_APP.getType(), userType); + SocialTypeEnum.WECHAT_MINI_PROGRAM.getType(), userType); if (client != null && Objects.equals(client.getStatus(), CommonStatusEnum.ENABLE.getStatus())) { return wxMaServiceCache.getUnchecked(client.getClientId() + ":" + client.getClientSecret()); } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImpl.java index dedab0db3a..f3cad58ebd 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImpl.java @@ -6,21 +6,20 @@ import cn.iocoder.yudao.framework.common.exception.ServiceException; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO; -import cn.iocoder.yudao.module.system.api.social.dto.SocialWxQrcodeReqDTO; import cn.iocoder.yudao.module.system.controller.admin.socail.vo.user.SocialUserPageReqVO; import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserBindDO; import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO; import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserBindMapper; import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserMapper; import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; -import com.xingyuv.jushauth.model.AuthUser; +import jakarta.annotation.Resource; +import jakarta.validation.constraints.NotNull; import lombok.extern.slf4j.Slf4j; +import me.zhyd.oauth.model.AuthUser; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; -import jakarta.annotation.Resource; -import jakarta.validation.constraints.NotNull; import java.util.Collections; import java.util.List; diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImplTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImplTest.java index fba8ff07e6..02f7a6155f 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImplTest.java +++ b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImplTest.java @@ -13,26 +13,25 @@ import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialCl import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialClientDO; import cn.iocoder.yudao.module.system.dal.mysql.social.SocialClientMapper; import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; +import cn.iocoder.yudao.module.system.framework.justauth.core.AuthRequestFactory; import com.binarywang.spring.starter.wxjava.miniapp.properties.WxMaProperties; import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties; -import com.xingyuv.jushauth.config.AuthConfig; -import com.xingyuv.jushauth.model.AuthResponse; -import com.xingyuv.jushauth.model.AuthUser; -import com.xingyuv.jushauth.request.AuthDefaultRequest; -import com.xingyuv.jushauth.request.AuthRequest; -import com.xingyuv.jushauth.utils.AuthStateUtils; -import com.xingyuv.justauth.AuthRequestFactory; +import jakarta.annotation.Resource; import me.chanjar.weixin.common.bean.WxJsapiSignature; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.mp.api.WxMpService; +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.model.AuthResponse; +import me.zhyd.oauth.model.AuthUser; +import me.zhyd.oauth.request.AuthDefaultRequest; +import me.zhyd.oauth.request.AuthRequest; +import me.zhyd.oauth.utils.AuthStateUtils; import org.junit.jupiter.api.Test; import org.mockito.MockedStatic; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import org.springframework.data.redis.core.StringRedisTemplate; -import jakarta.annotation.Resource; - import static cn.hutool.core.util.RandomUtil.randomEle; import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; @@ -103,7 +102,7 @@ public class SocialClientServiceImplTest extends BaseDbUnitTest { when(authRequestFactory.get(eq("WECHAT_MP"))).thenReturn(authRequest); // mock 方法(AuthResponse) AuthUser authUser = randomPojo(AuthUser.class); - AuthResponse authResponse = new AuthResponse<>(2000, null, authUser); + AuthResponse authResponse = new AuthResponse<>(2000, null, authUser); when(authRequest.login(argThat(authCallback -> { assertEquals(code, authCallback.getCode()); assertEquals(state, authCallback.getState()); @@ -127,7 +126,7 @@ public class SocialClientServiceImplTest extends BaseDbUnitTest { AuthRequest authRequest = mock(AuthRequest.class); when(authRequestFactory.get(eq("WECHAT_MP"))).thenReturn(authRequest); // mock 方法(AuthResponse) - AuthResponse authResponse = new AuthResponse<>(0, "模拟失败", null); + AuthResponse authResponse = new AuthResponse<>(0, "模拟失败", null); when(authRequest.login(argThat(authCallback -> { assertEquals(code, authCallback.getCode()); assertEquals(state, authCallback.getState()); @@ -317,7 +316,7 @@ public class SocialClientServiceImplTest extends BaseDbUnitTest { Integer userType = randomPojo(UserTypeEnum.class).getValue(); // mock 数据 SocialClientDO client = randomPojo(SocialClientDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()) - .setUserType(userType).setSocialType(SocialTypeEnum.WECHAT_MINI_APP.getType())); + .setUserType(userType).setSocialType(SocialTypeEnum.WECHAT_MINI_PROGRAM.getType())); socialClientMapper.insert(client); // 调用 @@ -332,7 +331,7 @@ public class SocialClientServiceImplTest extends BaseDbUnitTest { Integer userType = randomPojo(UserTypeEnum.class).getValue(); // mock 数据 SocialClientDO client = randomPojo(SocialClientDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()) - .setUserType(userType).setSocialType(SocialTypeEnum.WECHAT_MINI_APP.getType())); + .setUserType(userType).setSocialType(SocialTypeEnum.WECHAT_MINI_PROGRAM.getType())); socialClientMapper.insert(client); // mock 方法 WxMaProperties.ConfigStorage configStorage = mock(WxMaProperties.ConfigStorage.class); diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImplTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImplTest.java index 32393dd51c..84164155e5 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImplTest.java +++ b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImplTest.java @@ -11,12 +11,12 @@ import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO; import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserBindMapper; import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserMapper; import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; -import com.xingyuv.jushauth.model.AuthUser; +import jakarta.annotation.Resource; +import me.zhyd.oauth.model.AuthUser; import org.junit.jupiter.api.Test; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; -import jakarta.annotation.Resource; import java.util.List; import static cn.hutool.core.util.RandomUtil.randomEle; diff --git a/yudao-server/src/main/resources/application-dev.yaml b/yudao-server/src/main/resources/application-dev.yaml index 7ca9ec6cc1..db07245c63 100644 --- a/yudao-server/src/main/resources/application-dev.yaml +++ b/yudao-server/src/main/resources/application-dev.yaml @@ -186,7 +186,7 @@ justauth: agent-id: 1000004 ignore-check-redirect-uri: true # noinspection SpringBootApplicationYaml - WECHAT_MINI_APP: # 微信小程序 + WECHAT_MINI_PROGRAM: # 微信小程序 client-id: ${wx.miniapp.appid} client-secret: ${wx.miniapp.secret} ignore-check-redirect-uri: true diff --git a/yudao-server/src/main/resources/application-local.yaml b/yudao-server/src/main/resources/application-local.yaml index 77438ef2af..bcb2236261 100644 --- a/yudao-server/src/main/resources/application-local.yaml +++ b/yudao-server/src/main/resources/application-local.yaml @@ -209,10 +209,10 @@ wx: # secret: 333ae72f41552af1e998fe1f54e1584a # appid: wx63c280fe3248a3e7 # wenhualian的接口测试号 # secret: 6f270509224a7ae1296bbf1c8cb97aed -# appid: wxc4598c446f8a9cb3 # 测试号(Kongdy 提供的) -# secret: 4a1a04e07f6a4a0751b39c3064a92c8b - appid: wx66186af0759f47c9 # 测试号(puhui 提供的) - secret: 3218bcbd112cbc614c7264ceb20144ac + appid: wxc4598c446f8a9cb3 # 测试号(Kongdy 提供的) + secret: 4a1a04e07f6a4a0751b39c3064a92c8b +# appid: wx66186af0759f47c9 # 测试号(puhui 提供的) +# secret: 3218bcbd112cbc614c7264ceb20144ac config-storage: type: RedisTemplate # 采用 RedisTemplate 操作 Redis,会自动从 Spring 中获取 key-prefix: wa # Redis Key 的前缀 @@ -252,7 +252,7 @@ justauth: agent-id: 1000004 ignore-check-redirect-uri: true # noinspection SpringBootApplicationYaml - WECHAT_MINI_APP: # 微信小程序 + WECHAT_MINI_PROGRAM: # 微信小程序 client-id: ${wx.miniapp.appid} client-secret: ${wx.miniapp.secret} ignore-check-redirect-uri: true