mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-22 23:31:52 +08:00
!752 使用 justauth 替代现有 yudao-spring-boot-starter-biz-social 依赖,减少 starter
Merge pull request !752 from 芋道源码/feature/justauth-op
This commit is contained in:
commit
ee25b8aedb
@ -70,7 +70,7 @@
|
|||||||
<aliyun-java-sdk-core.version>4.6.4</aliyun-java-sdk-core.version>
|
<aliyun-java-sdk-core.version>4.6.4</aliyun-java-sdk-core.version>
|
||||||
<aliyun-java-sdk-dysmsapi.version>2.2.1</aliyun-java-sdk-dysmsapi.version>
|
<aliyun-java-sdk-dysmsapi.version>2.2.1</aliyun-java-sdk-dysmsapi.version>
|
||||||
<tencentcloud-sdk-java.version>3.1.880</tencentcloud-sdk-java.version>
|
<tencentcloud-sdk-java.version>3.1.880</tencentcloud-sdk-java.version>
|
||||||
<justauth.version>1.0.7</justauth.version>
|
<justauth.version>1.0.8</justauth.version>
|
||||||
<jimureport.version>1.6.1</jimureport.version>
|
<jimureport.version>1.6.1</jimureport.version>
|
||||||
<xercesImpl.version>2.12.2</xercesImpl.version>
|
<xercesImpl.version>2.12.2</xercesImpl.version>
|
||||||
<weixin-java.version>4.5.0</weixin-java.version>
|
<weixin-java.version>4.5.0</weixin-java.version>
|
||||||
@ -611,6 +611,12 @@
|
|||||||
<groupId>com.xingyuv</groupId>
|
<groupId>com.xingyuv</groupId>
|
||||||
<artifactId>spring-boot-starter-justauth</artifactId> <!-- 社交登陆(例如说,个人微信、企业微信等等) -->
|
<artifactId>spring-boot-starter-justauth</artifactId> <!-- 社交登陆(例如说,个人微信、企业微信等等) -->
|
||||||
<version>${justauth.version}</version>
|
<version>${justauth.version}</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-core</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -32,7 +32,6 @@
|
|||||||
|
|
||||||
<module>yudao-spring-boot-starter-biz-pay</module>
|
<module>yudao-spring-boot-starter-biz-pay</module>
|
||||||
<module>yudao-spring-boot-starter-biz-weixin</module>
|
<module>yudao-spring-boot-starter-biz-weixin</module>
|
||||||
<module>yudao-spring-boot-starter-biz-social</module>
|
|
||||||
<module>yudao-spring-boot-starter-biz-tenant</module>
|
<module>yudao-spring-boot-starter-biz-tenant</module>
|
||||||
<module>yudao-spring-boot-starter-biz-data-permission</module>
|
<module>yudao-spring-boot-starter-biz-data-permission</module>
|
||||||
<module>yudao-spring-boot-starter-biz-error-code</module>
|
<module>yudao-spring-boot-starter-biz-error-code</module>
|
||||||
|
@ -1,56 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<parent>
|
|
||||||
<groupId>cn.iocoder.boot</groupId>
|
|
||||||
<artifactId>yudao-framework</artifactId>
|
|
||||||
<version>${revision}</version>
|
|
||||||
</parent>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<artifactId>yudao-spring-boot-starter-biz-social</artifactId>
|
|
||||||
<name>${project.artifactId}</name>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>cn.iocoder.boot</groupId>
|
|
||||||
<artifactId>yudao-common</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<!-- Spring 核心 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-aop</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<!-- Web 相关 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>cn.iocoder.boot</groupId>
|
|
||||||
<artifactId>yudao-spring-boot-starter-web</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<!-- spring boot 配置所需依赖 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<!-- 三方云服务相关 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.xingyuv</groupId>
|
|
||||||
<artifactId>spring-boot-starter-justauth</artifactId>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>cn.hutool</groupId>
|
|
||||||
<artifactId>hutool-core</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>cn.iocoder.boot</groupId>
|
|
||||||
<artifactId>yudao-spring-boot-starter-redis</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
|
|
||||||
</project>
|
|
@ -1,36 +0,0 @@
|
|||||||
package cn.iocoder.yudao.framework.social.config;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.social.core.YudaoAuthRequestFactory;
|
|
||||||
import com.xingyuv.http.HttpUtil;
|
|
||||||
import com.xingyuv.http.support.hutool.HutoolImpl;
|
|
||||||
import com.xingyuv.jushauth.cache.AuthStateCache;
|
|
||||||
import com.xingyuv.justauth.autoconfigure.JustAuthProperties;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
|
||||||
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.Primary;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 社交自动装配类
|
|
||||||
*
|
|
||||||
* @author timfruit
|
|
||||||
* @date 2021-10-30
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
@AutoConfiguration
|
|
||||||
@EnableConfigurationProperties(JustAuthProperties.class)
|
|
||||||
public class YudaoSocialAutoConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Primary
|
|
||||||
@ConditionalOnProperty(prefix = "justauth", value = "enabled", havingValue = "true", matchIfMissing = true)
|
|
||||||
public YudaoAuthRequestFactory yudaoAuthRequestFactory(JustAuthProperties properties, AuthStateCache authStateCache) {
|
|
||||||
// 需要修改 HttpUtil 使用的实现,避免类报错
|
|
||||||
HttpUtil.setHttp(new HutoolImpl());
|
|
||||||
// 创建 YudaoAuthRequestFactory
|
|
||||||
return new YudaoAuthRequestFactory(properties, authStateCache);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,94 +0,0 @@
|
|||||||
package cn.iocoder.yudao.framework.social.core;
|
|
||||||
|
|
||||||
import cn.hutool.core.util.EnumUtil;
|
|
||||||
import cn.hutool.core.util.ReflectUtil;
|
|
||||||
import cn.iocoder.yudao.framework.social.core.enums.AuthExtendSource;
|
|
||||||
import cn.iocoder.yudao.framework.social.core.request.AuthWeChatMiniAppRequest;
|
|
||||||
import cn.iocoder.yudao.framework.social.core.request.AuthWeChatMpRequest;
|
|
||||||
import com.xingyuv.jushauth.cache.AuthStateCache;
|
|
||||||
import com.xingyuv.jushauth.config.AuthConfig;
|
|
||||||
import com.xingyuv.jushauth.config.AuthSource;
|
|
||||||
import com.xingyuv.jushauth.request.AuthRequest;
|
|
||||||
import com.xingyuv.justauth.AuthRequestFactory;
|
|
||||||
import com.xingyuv.justauth.autoconfigure.JustAuthProperties;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
|
|
||||||
import static com.xingyuv.jushauth.config.AuthDefaultSource.WECHAT_MP;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 第三方授权拓展 request 工厂类
|
|
||||||
* 为使得拓展配置 {@link AuthConfig} 和默认配置齐平,所以自定义本工厂类
|
|
||||||
*
|
|
||||||
* @author timfruit
|
|
||||||
* @date 2021-10-31
|
|
||||||
*/
|
|
||||||
public class YudaoAuthRequestFactory extends AuthRequestFactory {
|
|
||||||
|
|
||||||
protected JustAuthProperties properties;
|
|
||||||
protected AuthStateCache authStateCache;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 由于父类 configureHttpConfig 方法是 private 修饰,所以获取后,进行反射调用
|
|
||||||
*/
|
|
||||||
private final Method configureHttpConfigMethod = ReflectUtil.getMethod(AuthRequestFactory.class,
|
|
||||||
"configureHttpConfig", String.class, AuthConfig.class, JustAuthProperties.JustAuthHttpConfig.class);
|
|
||||||
|
|
||||||
public YudaoAuthRequestFactory(JustAuthProperties properties, AuthStateCache authStateCache) {
|
|
||||||
super(properties, authStateCache);
|
|
||||||
this.properties = properties;
|
|
||||||
this.authStateCache = authStateCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回 AuthRequest 对象
|
|
||||||
*
|
|
||||||
* @param source {@link AuthSource}
|
|
||||||
* @return {@link AuthRequest}
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public AuthRequest get(String source) {
|
|
||||||
// 先尝试获取自定义扩展的
|
|
||||||
AuthRequest authRequest = getExtendRequest(source);
|
|
||||||
// 找不到,使用默认拓展
|
|
||||||
if (authRequest == null) {
|
|
||||||
authRequest = super.get(source);
|
|
||||||
}
|
|
||||||
return authRequest;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected AuthRequest getExtendRequest(String source) {
|
|
||||||
// TODO 芋艿:临时兼容 justauth 迁移的类型不对问题;
|
|
||||||
if (WECHAT_MP.name().equalsIgnoreCase(source)) {
|
|
||||||
AuthConfig config = properties.getType().get(WECHAT_MP.name());
|
|
||||||
return new AuthWeChatMpRequest(config, authStateCache);
|
|
||||||
}
|
|
||||||
|
|
||||||
AuthExtendSource authExtendSource;
|
|
||||||
try {
|
|
||||||
authExtendSource = EnumUtil.fromString(AuthExtendSource.class, source.toUpperCase());
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
// 无自定义匹配
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 拓展配置和默认配置齐平,properties 放在一起
|
|
||||||
AuthConfig config = properties.getType().get(authExtendSource.name());
|
|
||||||
// 找不到对应关系,直接返回空
|
|
||||||
if (config == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
// 反射调用,配置 http config
|
|
||||||
ReflectUtil.invoke(this, configureHttpConfigMethod, authExtendSource.name(), config, properties.getHttpConfig());
|
|
||||||
|
|
||||||
// 获得拓展的 Request
|
|
||||||
// noinspection SwitchStatementWithTooFewBranches
|
|
||||||
switch (authExtendSource) {
|
|
||||||
case WECHAT_MINI_APP:
|
|
||||||
return new AuthWeChatMiniAppRequest(config, authStateCache);
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
package cn.iocoder.yudao.framework.social.core.enums;
|
|
||||||
|
|
||||||
import com.xingyuv.jushauth.config.AuthSource;
|
|
||||||
import com.xingyuv.jushauth.request.AuthDefaultRequest;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 拓展 JustAuth 各 api 需要的 url, 用枚举类分平台类型管理
|
|
||||||
*
|
|
||||||
* 默认配置 {@link com.xingyuv.jushauth.config.AuthDefaultSource}
|
|
||||||
*
|
|
||||||
* @author timfruit
|
|
||||||
*/
|
|
||||||
public enum AuthExtendSource implements AuthSource {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 微信小程序授权登录
|
|
||||||
*/
|
|
||||||
WECHAT_MINI_APP {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String authorize() {
|
|
||||||
// 参见 https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html 文档
|
|
||||||
throw new UnsupportedOperationException("不支持获取授权 url,请使用小程序内置函数 wx.login() 登录获取 code");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String accessToken() {
|
|
||||||
// 参见 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html 文档
|
|
||||||
// 获取 openid, unionId , session_key 等字段
|
|
||||||
return "https://api.weixin.qq.com/sns/jscode2session";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String userInfo() {
|
|
||||||
// 参见 https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html 文档
|
|
||||||
throw new UnsupportedOperationException("不支持获取用户信息 url,请使用小程序内置函数 wx.getUserProfile() 获取用户信息");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Class<? extends AuthDefaultRequest> getTargetClass() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,97 +0,0 @@
|
|||||||
package cn.iocoder.yudao.framework.social.core.request;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
|
||||||
import cn.iocoder.yudao.framework.social.core.enums.AuthExtendSource;
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
import com.xingyuv.jushauth.cache.AuthStateCache;
|
|
||||||
import com.xingyuv.jushauth.config.AuthConfig;
|
|
||||||
import com.xingyuv.jushauth.exception.AuthException;
|
|
||||||
import com.xingyuv.jushauth.model.AuthCallback;
|
|
||||||
import com.xingyuv.jushauth.model.AuthToken;
|
|
||||||
import com.xingyuv.jushauth.model.AuthUser;
|
|
||||||
import com.xingyuv.jushauth.request.AuthDefaultRequest;
|
|
||||||
import com.xingyuv.jushauth.utils.HttpUtils;
|
|
||||||
import com.xingyuv.jushauth.utils.UrlBuilder;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 微信小程序登陆 Request 请求
|
|
||||||
*
|
|
||||||
* 由于 JustAuth 定位是面向 Web 为主的三方登录,所以微信小程序只能自己封装
|
|
||||||
*
|
|
||||||
* @author timfruit
|
|
||||||
* @date 2021-10-29
|
|
||||||
*/
|
|
||||||
public class AuthWeChatMiniAppRequest extends AuthDefaultRequest {
|
|
||||||
|
|
||||||
public AuthWeChatMiniAppRequest(AuthConfig config, AuthStateCache authStateCache) {
|
|
||||||
super(config, AuthExtendSource.WECHAT_MINI_APP, authStateCache);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected AuthToken getAccessToken(AuthCallback authCallback) {
|
|
||||||
// 参见 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html 文档
|
|
||||||
// 使用 code 获取对应的 openId、unionId 等字段
|
|
||||||
String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl(authCallback.getCode())).getBody();
|
|
||||||
JSCode2SessionResponse accessTokenObject = JsonUtils.parseObject(response, JSCode2SessionResponse.class);
|
|
||||||
assert accessTokenObject != null;
|
|
||||||
checkResponse(accessTokenObject);
|
|
||||||
// 拼装结果
|
|
||||||
return AuthToken.builder()
|
|
||||||
.openId(accessTokenObject.getOpenid())
|
|
||||||
.unionId(accessTokenObject.getUnionId())
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected AuthUser getUserInfo(AuthToken authToken) {
|
|
||||||
// 参见 https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html 文档
|
|
||||||
// 如果需要用户信息,需要在小程序调用函数后传给后端
|
|
||||||
return AuthUser.builder()
|
|
||||||
.username("")
|
|
||||||
.nickname("")
|
|
||||||
.avatar("")
|
|
||||||
.uuid(authToken.getOpenId())
|
|
||||||
.token(authToken)
|
|
||||||
.source(source.toString())
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查响应内容是否正确
|
|
||||||
*
|
|
||||||
* @param response 请求响应内容
|
|
||||||
*/
|
|
||||||
private void checkResponse(JSCode2SessionResponse response) {
|
|
||||||
if (response.getErrorCode() != 0) {
|
|
||||||
throw new AuthException(response.getErrorCode(), response.getErrorMsg());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected String accessTokenUrl(String code) {
|
|
||||||
return UrlBuilder.fromBaseUrl(source.accessToken())
|
|
||||||
.queryParam("appid", config.getClientId())
|
|
||||||
.queryParam("secret", config.getClientSecret())
|
|
||||||
.queryParam("js_code", code)
|
|
||||||
.queryParam("grant_type", "authorization_code")
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@SuppressWarnings("SpellCheckingInspection")
|
|
||||||
private static class JSCode2SessionResponse {
|
|
||||||
|
|
||||||
@JsonProperty("errcode")
|
|
||||||
private int errorCode;
|
|
||||||
@JsonProperty("errmsg")
|
|
||||||
private String errorMsg;
|
|
||||||
@JsonProperty("session_key")
|
|
||||||
private String sessionKey;
|
|
||||||
private String openid;
|
|
||||||
@JsonProperty("unionid")
|
|
||||||
private String unionId;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,178 +0,0 @@
|
|||||||
package cn.iocoder.yudao.framework.social.core.request;
|
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import com.xingyuv.jushauth.cache.AuthStateCache;
|
|
||||||
import com.xingyuv.jushauth.config.AuthConfig;
|
|
||||||
import com.xingyuv.jushauth.config.AuthDefaultSource;
|
|
||||||
import com.xingyuv.jushauth.enums.AuthResponseStatus;
|
|
||||||
import com.xingyuv.jushauth.enums.AuthUserGender;
|
|
||||||
import com.xingyuv.jushauth.enums.scope.AuthWechatMpScope;
|
|
||||||
import com.xingyuv.jushauth.exception.AuthException;
|
|
||||||
import com.xingyuv.jushauth.model.AuthCallback;
|
|
||||||
import com.xingyuv.jushauth.model.AuthResponse;
|
|
||||||
import com.xingyuv.jushauth.model.AuthToken;
|
|
||||||
import com.xingyuv.jushauth.model.AuthUser;
|
|
||||||
import com.xingyuv.jushauth.request.AuthDefaultRequest;
|
|
||||||
import com.xingyuv.jushauth.utils.AuthScopeUtils;
|
|
||||||
import com.xingyuv.jushauth.utils.GlobalAuthUtils;
|
|
||||||
import com.xingyuv.jushauth.utils.HttpUtils;
|
|
||||||
import com.xingyuv.jushauth.utils.UrlBuilder;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 微信公众平台登录
|
|
||||||
*
|
|
||||||
* @author yangkai.shen (https://xkcoding.com)
|
|
||||||
* @since 1.1.0
|
|
||||||
*/
|
|
||||||
public class AuthWeChatMpRequest extends AuthDefaultRequest {
|
|
||||||
public AuthWeChatMpRequest(AuthConfig config) {
|
|
||||||
super(config, AuthDefaultSource.WECHAT_MP);
|
|
||||||
}
|
|
||||||
|
|
||||||
public AuthWeChatMpRequest(AuthConfig config, AuthStateCache authStateCache) {
|
|
||||||
super(config, AuthDefaultSource.WECHAT_MP, authStateCache);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 微信的特殊性,此时返回的信息同时包含 openid 和 access_token
|
|
||||||
*
|
|
||||||
* @param authCallback 回调返回的参数
|
|
||||||
* @return 所有信息
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected AuthToken getAccessToken(AuthCallback authCallback) {
|
|
||||||
return this.getToken(accessTokenUrl(authCallback.getCode()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected AuthUser getUserInfo(AuthToken authToken) {
|
|
||||||
String openId = authToken.getOpenId();
|
|
||||||
|
|
||||||
String response = doGetUserInfo(authToken);
|
|
||||||
JSONObject object = JSONObject.parseObject(response);
|
|
||||||
|
|
||||||
this.checkResponse(object);
|
|
||||||
|
|
||||||
String location = String.format("%s-%s-%s", object.getString("country"), object.getString("province"), object.getString("city"));
|
|
||||||
|
|
||||||
if (object.containsKey("unionid")) {
|
|
||||||
authToken.setUnionId(object.getString("unionid"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return AuthUser.builder()
|
|
||||||
.rawUserInfo(object)
|
|
||||||
.username(object.getString("nickname"))
|
|
||||||
.nickname(object.getString("nickname"))
|
|
||||||
.avatar(object.getString("headimgurl"))
|
|
||||||
.location(location)
|
|
||||||
.uuid(openId)
|
|
||||||
.gender(AuthUserGender.getWechatRealGender(object.getString("sex")))
|
|
||||||
.token(authToken)
|
|
||||||
.source(source.toString())
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public AuthResponse refresh(AuthToken oldToken) {
|
|
||||||
return AuthResponse.builder()
|
|
||||||
.code(AuthResponseStatus.SUCCESS.getCode())
|
|
||||||
.data(this.getToken(refreshTokenUrl(oldToken.getRefreshToken())))
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查响应内容是否正确
|
|
||||||
*
|
|
||||||
* @param object 请求响应内容
|
|
||||||
*/
|
|
||||||
private void checkResponse(JSONObject object) {
|
|
||||||
if (object.containsKey("errcode")) {
|
|
||||||
throw new AuthException(object.getIntValue("errcode"), object.getString("errmsg"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取token,适用于获取access_token和刷新token
|
|
||||||
*
|
|
||||||
* @param accessTokenUrl 实际请求token的地址
|
|
||||||
* @return token对象
|
|
||||||
*/
|
|
||||||
private AuthToken getToken(String accessTokenUrl) {
|
|
||||||
String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl).getBody();
|
|
||||||
JSONObject accessTokenObject = JSONObject.parseObject(response);
|
|
||||||
|
|
||||||
this.checkResponse(accessTokenObject);
|
|
||||||
|
|
||||||
return AuthToken.builder()
|
|
||||||
.accessToken(accessTokenObject.getString("access_token"))
|
|
||||||
.refreshToken(accessTokenObject.getString("refresh_token"))
|
|
||||||
.expireIn(accessTokenObject.getIntValue("expires_in"))
|
|
||||||
.openId(accessTokenObject.getString("openid"))
|
|
||||||
.scope(accessTokenObject.getString("scope"))
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回带{@code state}参数的授权url,授权回调时会带上这个{@code state}
|
|
||||||
*
|
|
||||||
* @param state state 验证授权流程的参数,可以防止csrf
|
|
||||||
* @return 返回授权地址
|
|
||||||
* @since 1.9.3
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public String authorize(String state) {
|
|
||||||
return UrlBuilder.fromBaseUrl(source.authorize())
|
|
||||||
.queryParam("appid", config.getClientId())
|
|
||||||
.queryParam("redirect_uri", GlobalAuthUtils.urlEncode(config.getRedirectUri()))
|
|
||||||
.queryParam("response_type", "code")
|
|
||||||
.queryParam("scope", this.getScopes(",", false, AuthScopeUtils.getDefaultScopes(AuthWechatMpScope.values())))
|
|
||||||
.queryParam("state", getRealState(state).concat("#wechat_redirect"))
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回获取accessToken的url
|
|
||||||
*
|
|
||||||
* @param code 授权码
|
|
||||||
* @return 返回获取accessToken的url
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected String accessTokenUrl(String code) {
|
|
||||||
return UrlBuilder.fromBaseUrl(source.accessToken())
|
|
||||||
.queryParam("appid", config.getClientId())
|
|
||||||
.queryParam("secret", config.getClientSecret())
|
|
||||||
.queryParam("code", code)
|
|
||||||
.queryParam("grant_type", "authorization_code")
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回获取userInfo的url
|
|
||||||
*
|
|
||||||
* @param authToken 用户授权后的token
|
|
||||||
* @return 返回获取userInfo的url
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected String userInfoUrl(AuthToken authToken) {
|
|
||||||
return UrlBuilder.fromBaseUrl(source.userInfo())
|
|
||||||
.queryParam("access_token", authToken.getAccessToken())
|
|
||||||
.queryParam("openid", authToken.getOpenId())
|
|
||||||
.queryParam("lang", "zh_CN")
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回获取userInfo的url
|
|
||||||
*
|
|
||||||
* @param refreshToken getAccessToken方法返回的refreshToken
|
|
||||||
* @return 返回获取userInfo的url
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected String refreshTokenUrl(String refreshToken) {
|
|
||||||
return UrlBuilder.fromBaseUrl(source.refresh())
|
|
||||||
.queryParam("appid", config.getClientId())
|
|
||||||
.queryParam("grant_type", "refresh_token")
|
|
||||||
.queryParam("refresh_token", refreshToken)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
cn.iocoder.yudao.framework.social.config.YudaoSocialAutoConfiguration
|
|
@ -46,10 +46,6 @@
|
|||||||
<groupId>cn.iocoder.boot</groupId>
|
<groupId>cn.iocoder.boot</groupId>
|
||||||
<artifactId>yudao-spring-boot-starter-biz-data-permission</artifactId>
|
<artifactId>yudao-spring-boot-starter-biz-data-permission</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>cn.iocoder.boot</groupId>
|
|
||||||
<artifactId>yudao-spring-boot-starter-biz-social</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.iocoder.boot</groupId>
|
<groupId>cn.iocoder.boot</groupId>
|
||||||
<artifactId>yudao-spring-boot-starter-biz-tenant</artifactId>
|
<artifactId>yudao-spring-boot-starter-biz-tenant</artifactId>
|
||||||
@ -119,6 +115,13 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-mail</artifactId>
|
<artifactId>spring-boot-starter-mail</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 三方云服务相关 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.xingyuv</groupId>
|
||||||
|
<artifactId>spring-boot-starter-justauth</artifactId> <!-- 社交登陆(例如说,个人微信、企业微信等等) -->
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -12,7 +12,6 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
|||||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||||
import cn.iocoder.yudao.framework.common.util.cache.CacheUtils;
|
import cn.iocoder.yudao.framework.common.util.cache.CacheUtils;
|
||||||
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
|
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
|
||||||
import cn.iocoder.yudao.framework.social.core.YudaoAuthRequestFactory;
|
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientCreateReqVO;
|
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientCreateReqVO;
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientPageReqVO;
|
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientPageReqVO;
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientUpdateReqVO;
|
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientUpdateReqVO;
|
||||||
@ -31,6 +30,7 @@ import com.xingyuv.jushauth.model.AuthResponse;
|
|||||||
import com.xingyuv.jushauth.model.AuthUser;
|
import com.xingyuv.jushauth.model.AuthUser;
|
||||||
import com.xingyuv.jushauth.request.AuthRequest;
|
import com.xingyuv.jushauth.request.AuthRequest;
|
||||||
import com.xingyuv.jushauth.utils.AuthStateUtils;
|
import com.xingyuv.jushauth.utils.AuthStateUtils;
|
||||||
|
import com.xingyuv.justauth.AuthRequestFactory;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import me.chanjar.weixin.common.bean.WxJsapiSignature;
|
import me.chanjar.weixin.common.bean.WxJsapiSignature;
|
||||||
@ -59,8 +59,8 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class SocialClientServiceImpl implements SocialClientService {
|
public class SocialClientServiceImpl implements SocialClientService {
|
||||||
|
|
||||||
@Resource // 由于自定义了 YudaoAuthRequestFactory 无法覆盖默认的 AuthRequestFactory,所以只能注入它
|
@Resource
|
||||||
private YudaoAuthRequestFactory yudaoAuthRequestFactory;
|
private AuthRequestFactory authRequestFactory;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private WxMpService wxMpService;
|
private WxMpService wxMpService;
|
||||||
@ -145,7 +145,7 @@ public class SocialClientServiceImpl implements SocialClientService {
|
|||||||
*/
|
*/
|
||||||
private AuthRequest buildAuthRequest(Integer socialType, Integer userType) {
|
private AuthRequest buildAuthRequest(Integer socialType, Integer userType) {
|
||||||
// 1. 先查找默认的配置项,从 application-*.yaml 中读取
|
// 1. 先查找默认的配置项,从 application-*.yaml 中读取
|
||||||
AuthRequest request = yudaoAuthRequestFactory.get(SocialTypeEnum.valueOfType(socialType).getSource());
|
AuthRequest request = authRequestFactory.get(SocialTypeEnum.valueOfType(socialType).getSource());
|
||||||
Assert.notNull(request, String.format("社交平台(%d) 不存在", socialType));
|
Assert.notNull(request, String.format("社交平台(%d) 不存在", socialType));
|
||||||
// 2. 查询 DB 的配置项,如果存在则进行覆盖
|
// 2. 查询 DB 的配置项,如果存在则进行覆盖
|
||||||
SocialClientDO client = socialClientMapper.selectBySocialTypeAndUserType(socialType, userType);
|
SocialClientDO client = socialClientMapper.selectBySocialTypeAndUserType(socialType, userType);
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package cn.iocoder.yudao.module.system.service.social;
|
package cn.iocoder.yudao.module.system.service.social;
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||||
import cn.iocoder.yudao.framework.social.core.YudaoAuthRequestFactory;
|
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||||
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
|
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.SocialUserRespDTO;
|
||||||
@ -15,6 +14,7 @@ import com.xingyuv.jushauth.model.AuthCallback;
|
|||||||
import com.xingyuv.jushauth.model.AuthResponse;
|
import com.xingyuv.jushauth.model.AuthResponse;
|
||||||
import com.xingyuv.jushauth.model.AuthUser;
|
import com.xingyuv.jushauth.model.AuthUser;
|
||||||
import com.xingyuv.jushauth.request.AuthRequest;
|
import com.xingyuv.jushauth.request.AuthRequest;
|
||||||
|
import com.xingyuv.justauth.AuthRequestFactory;
|
||||||
import org.junit.jupiter.api.Disabled;
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
@ -46,7 +46,7 @@ public class SocialUserServiceImplTest extends BaseDbUnitTest {
|
|||||||
private SocialUserBindMapper socialUserBindMapper;
|
private SocialUserBindMapper socialUserBindMapper;
|
||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private YudaoAuthRequestFactory authRequestFactory;
|
private AuthRequestFactory authRequestFactory;
|
||||||
|
|
||||||
// TODO 芋艿:后续统一修复
|
// TODO 芋艿:后续统一修复
|
||||||
// @Test
|
// @Test
|
||||||
|
Loading…
Reference in New Issue
Block a user