mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-04 20:28:44 +08:00 
			
		
		
		
	拓展授权登录抽取成单独的starter,拓展配置和默认配置齐平
This commit is contained in:
		@@ -0,0 +1,29 @@
 | 
			
		||||
package cn.iocoder.yudao.framework.social.config;
 | 
			
		||||
 | 
			
		||||
import cn.iocoder.yudao.framework.social.core.YudaoAuthRequestFactory;
 | 
			
		||||
import com.xkcoding.justauth.autoconfigure.JustAuthProperties;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 社交自动装配类
 | 
			
		||||
 *
 | 
			
		||||
 * @author timfruit
 | 
			
		||||
 * @date 2021-10-30
 | 
			
		||||
 */
 | 
			
		||||
@Slf4j
 | 
			
		||||
@Configuration
 | 
			
		||||
@EnableConfigurationProperties(JustAuthProperties.class)
 | 
			
		||||
public class YudaoSocialAutoConfiguration {
 | 
			
		||||
 | 
			
		||||
    @Bean
 | 
			
		||||
    @ConditionalOnProperty(prefix = "justauth", value = "enabled", havingValue = "true", matchIfMissing = true)
 | 
			
		||||
    public YudaoAuthRequestFactory yudaoAuthRequestFactory(JustAuthProperties properties, AuthStateCache authStateCache) {
 | 
			
		||||
        return new YudaoAuthRequestFactory(properties, authStateCache);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,108 @@
 | 
			
		||||
package cn.iocoder.yudao.framework.social.core;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.util.EnumUtil;
 | 
			
		||||
import cn.hutool.core.util.StrUtil;
 | 
			
		||||
import cn.iocoder.yudao.framework.social.core.enums.AuthExtendSource;
 | 
			
		||||
import cn.iocoder.yudao.framework.social.core.request.AuthWeChatMiniProgramRequest;
 | 
			
		||||
import com.xkcoding.http.config.HttpConfig;
 | 
			
		||||
import com.xkcoding.justauth.AuthRequestFactory;
 | 
			
		||||
import com.xkcoding.justauth.autoconfigure.JustAuthProperties;
 | 
			
		||||
import me.zhyd.oauth.cache.AuthStateCache;
 | 
			
		||||
import me.zhyd.oauth.config.AuthConfig;
 | 
			
		||||
import me.zhyd.oauth.config.AuthSource;
 | 
			
		||||
import me.zhyd.oauth.request.AuthRequest;
 | 
			
		||||
import org.springframework.util.CollectionUtils;
 | 
			
		||||
 | 
			
		||||
import java.net.InetSocketAddress;
 | 
			
		||||
import java.net.Proxy;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 第三方授权拓展request工厂类
 | 
			
		||||
 *
 | 
			
		||||
 * @author timfruit
 | 
			
		||||
 * @date 2021-10-31
 | 
			
		||||
 */
 | 
			
		||||
public class YudaoAuthRequestFactory extends AuthRequestFactory {
 | 
			
		||||
    protected JustAuthProperties properties;
 | 
			
		||||
    protected AuthStateCache authStateCache;
 | 
			
		||||
 | 
			
		||||
    public YudaoAuthRequestFactory(JustAuthProperties properties, AuthStateCache authStateCache) {
 | 
			
		||||
        super(properties, authStateCache);
 | 
			
		||||
        this.properties = properties;
 | 
			
		||||
        this.authStateCache = authStateCache;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 返回AuthRequest对象
 | 
			
		||||
     *
 | 
			
		||||
     * @param source {@link AuthSource}
 | 
			
		||||
     * @return {@link AuthRequest}
 | 
			
		||||
     */
 | 
			
		||||
    public AuthRequest get(String source) {
 | 
			
		||||
        //先尝试获取自定义扩展的
 | 
			
		||||
        AuthRequest authRequest = getExtendRequest(source);
 | 
			
		||||
 | 
			
		||||
        if (authRequest == null) {
 | 
			
		||||
            authRequest = super.get(source);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return authRequest;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    protected AuthRequest getExtendRequest(String source) {
 | 
			
		||||
        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
 | 
			
		||||
        configureHttpConfig(authExtendSource.name(), config, properties.getHttpConfig());
 | 
			
		||||
 | 
			
		||||
        switch (authExtendSource) {
 | 
			
		||||
            case WECHAT_MINI_PROGRAM:
 | 
			
		||||
                return new AuthWeChatMiniProgramRequest(config, authStateCache);
 | 
			
		||||
            default:
 | 
			
		||||
                return null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 配置 http 相关的配置
 | 
			
		||||
     *
 | 
			
		||||
     * @param authSource {@link AuthSource}
 | 
			
		||||
     * @param authConfig {@link AuthConfig}
 | 
			
		||||
     */
 | 
			
		||||
    protected void configureHttpConfig(String authSource, AuthConfig authConfig, JustAuthProperties.JustAuthHttpConfig httpConfig) {
 | 
			
		||||
        if (null == httpConfig) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        Map<String, JustAuthProperties.JustAuthProxyConfig> 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());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,33 @@
 | 
			
		||||
package cn.iocoder.yudao.framework.social.core.enums;
 | 
			
		||||
 | 
			
		||||
import me.zhyd.oauth.config.AuthSource;
 | 
			
		||||
 | 
			
		||||
public enum AuthExtendSource implements AuthSource {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 微信小程序授权登录
 | 
			
		||||
     */
 | 
			
		||||
    WECHAT_MINI_PROGRAM{
 | 
			
		||||
        @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()获取用户信息");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,23 @@
 | 
			
		||||
package cn.iocoder.yudao.framework.social.core.model;
 | 
			
		||||
 | 
			
		||||
import lombok.*;
 | 
			
		||||
import me.zhyd.oauth.model.AuthToken;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 授权所需的token 拓展类
 | 
			
		||||
 *
 | 
			
		||||
 * @author timfruit
 | 
			
		||||
 * @date 2021-10-29
 | 
			
		||||
 */
 | 
			
		||||
@Getter
 | 
			
		||||
@Setter
 | 
			
		||||
@NoArgsConstructor
 | 
			
		||||
@AllArgsConstructor
 | 
			
		||||
public class AuthExtendToken extends AuthToken {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 微信小程序 会话密钥
 | 
			
		||||
     */
 | 
			
		||||
    private String miniSessionKey;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,98 @@
 | 
			
		||||
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 cn.iocoder.yudao.framework.social.core.model.AuthExtendToken;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import me.zhyd.oauth.cache.AuthStateCache;
 | 
			
		||||
import me.zhyd.oauth.config.AuthConfig;
 | 
			
		||||
import me.zhyd.oauth.exception.AuthException;
 | 
			
		||||
import me.zhyd.oauth.model.AuthCallback;
 | 
			
		||||
import me.zhyd.oauth.model.AuthToken;
 | 
			
		||||
import me.zhyd.oauth.model.AuthUser;
 | 
			
		||||
import me.zhyd.oauth.request.AuthDefaultRequest;
 | 
			
		||||
import me.zhyd.oauth.utils.HttpUtils;
 | 
			
		||||
import me.zhyd.oauth.utils.UrlBuilder;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 微信小程序登陆
 | 
			
		||||
 *
 | 
			
		||||
 * @author timfruit
 | 
			
		||||
 * @date 2021-10-29
 | 
			
		||||
 */
 | 
			
		||||
public class AuthWeChatMiniProgramRequest extends AuthDefaultRequest {
 | 
			
		||||
 | 
			
		||||
    public AuthWeChatMiniProgramRequest(AuthConfig config) {
 | 
			
		||||
        super(config, AuthExtendSource.WECHAT_MINI_PROGRAM);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public AuthWeChatMiniProgramRequest(AuthConfig config, AuthStateCache authStateCache) {
 | 
			
		||||
        super(config, AuthExtendSource.WECHAT_MINI_PROGRAM, authStateCache);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected AuthToken getAccessToken(AuthCallback authCallback) {
 | 
			
		||||
        // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
 | 
			
		||||
        String response = new HttpUtils(config.getHttpConfig()).get(accessTokenUrl(authCallback.getCode()));
 | 
			
		||||
        CodeSessionResponse accessTokenObject = JsonUtils.parseObject(response, CodeSessionResponse.class);
 | 
			
		||||
 | 
			
		||||
        this.checkResponse(accessTokenObject);
 | 
			
		||||
 | 
			
		||||
        AuthExtendToken token = new AuthExtendToken();
 | 
			
		||||
        token.setMiniSessionKey(accessTokenObject.session_key);
 | 
			
		||||
        token.setOpenId(accessTokenObject.openid);
 | 
			
		||||
        token.setUnionId(accessTokenObject.unionid);
 | 
			
		||||
        return token;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected AuthUser getUserInfo(AuthToken authToken) {
 | 
			
		||||
        // https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html
 | 
			
		||||
        // 如果需要用户信息,需要在小程序调用函数后传给后端
 | 
			
		||||
        return AuthUser.builder()
 | 
			
		||||
                .uuid(authToken.getOpenId())
 | 
			
		||||
                //TODO 是使用默认值,还是有小程序获取用户信息 和 code 一起传过来
 | 
			
		||||
                .nickname("")
 | 
			
		||||
                .avatar("")
 | 
			
		||||
                .token(authToken)
 | 
			
		||||
                .source(source.toString())
 | 
			
		||||
                .build();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 检查响应内容是否正确
 | 
			
		||||
     *
 | 
			
		||||
     * @param object 请求响应内容
 | 
			
		||||
     */
 | 
			
		||||
    private void checkResponse(CodeSessionResponse object) {
 | 
			
		||||
        if (object.errcode != 0) {
 | 
			
		||||
            throw new AuthException(object.errcode, object.errmsg);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 返回获取 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("js_code", code)
 | 
			
		||||
                .queryParam("grant_type", "authorization_code")
 | 
			
		||||
                .build();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Data
 | 
			
		||||
    private static class CodeSessionResponse {
 | 
			
		||||
        private int errcode;
 | 
			
		||||
        private String errmsg;
 | 
			
		||||
        private String session_key;
 | 
			
		||||
        private String openid;
 | 
			
		||||
        private String unionid;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,2 @@
 | 
			
		||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
 | 
			
		||||
  cn.iocoder.yudao.framework.social.config.YudaoSocialAutoConfiguration
 | 
			
		||||
		Reference in New Issue
	
	Block a user