fix bugs
This commit is contained in:
40
blade-starter-social/pom.xml
Normal file
40
blade-starter-social/pom.xml
Normal file
@@ -0,0 +1,40 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<artifactId>BladeX-Tool</artifactId>
|
||||
<groupId>org.springblade</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>blade-starter-social</artifactId>
|
||||
<name>${project.artifactId}</name>
|
||||
<version>${project.parent.version}</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<!--Blade-->
|
||||
<dependency>
|
||||
<groupId>org.springblade</groupId>
|
||||
<artifactId>blade-starter-redis</artifactId>
|
||||
</dependency>
|
||||
<!-- 第三方登陆 -->
|
||||
<dependency>
|
||||
<groupId>me.zhyd.oauth</groupId>
|
||||
<artifactId>JustAuth</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
</dependency>
|
||||
<!-- Auto -->
|
||||
<dependency>
|
||||
<groupId>org.springblade</groupId>
|
||||
<artifactId>blade-core-auto</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
69
blade-starter-social/src/main/java/org/springblade/core/social/cache/AuthStateRedisCache.java
vendored
Normal file
69
blade-starter-social/src/main/java/org/springblade/core/social/cache/AuthStateRedisCache.java
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
package org.springblade.core.social.cache;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import me.zhyd.oauth.cache.AuthCacheConfig;
|
||||
import me.zhyd.oauth.cache.AuthStateCache;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 扩展Redis版的state缓存
|
||||
*
|
||||
* @author yadong.zhang, Chill
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
public class AuthStateRedisCache implements AuthStateCache {
|
||||
|
||||
private final RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
private final ValueOperations<String, Object> valueOperations;
|
||||
|
||||
|
||||
/**
|
||||
* 存入缓存,默认3分钟
|
||||
*
|
||||
* @param key 缓存key
|
||||
* @param value 缓存内容
|
||||
*/
|
||||
@Override
|
||||
public void cache(String key, String value) {
|
||||
valueOperations.set(key, value, AuthCacheConfig.timeout, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 存入缓存
|
||||
*
|
||||
* @param key 缓存key
|
||||
* @param value 缓存内容
|
||||
* @param timeout 指定缓存过期时间(毫秒)
|
||||
*/
|
||||
@Override
|
||||
public void cache(String key, String value, long timeout) {
|
||||
valueOperations.set(key, value, timeout, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存内容
|
||||
*
|
||||
* @param key 缓存key
|
||||
* @return 缓存内容
|
||||
*/
|
||||
@Override
|
||||
public String get(String key) {
|
||||
return String.valueOf(valueOperations.get(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否存在key,如果对应key的value值已过期,也返回false
|
||||
*
|
||||
* @param key 缓存key
|
||||
* @return true:存在key,并且value没过期;false:key不存在或者已过期
|
||||
*/
|
||||
@Override
|
||||
public boolean containsKey(String key) {
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* BladeX Commercial License Agreement
|
||||
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
|
||||
* <p>
|
||||
* Use of this software is governed by the Commercial License Agreement
|
||||
* obtained after purchasing a license from BladeX.
|
||||
* <p>
|
||||
* 1. This software is for development use only under a valid license
|
||||
* from BladeX.
|
||||
* <p>
|
||||
* 2. Redistribution of this software's source code to any third party
|
||||
* without a commercial license is strictly prohibited.
|
||||
* <p>
|
||||
* 3. Licensees may copyright their own code but cannot use segments
|
||||
* from this software for such purposes. Copyright of this software
|
||||
* remains with BladeX.
|
||||
* <p>
|
||||
* Using this software signifies agreement to this License, and the software
|
||||
* must not be used for illegal purposes.
|
||||
* <p>
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
|
||||
* not liable for any claims arising from secondary or illegal development.
|
||||
* <p>
|
||||
* Author: Chill Zhuang (bladejava@qq.com)
|
||||
*/
|
||||
package org.springblade.core.social.config;
|
||||
|
||||
import com.xkcoding.http.HttpUtil;
|
||||
import com.xkcoding.http.support.Http;
|
||||
import com.xkcoding.http.support.httpclient.HttpClientImpl;
|
||||
import me.zhyd.oauth.cache.AuthStateCache;
|
||||
import org.springblade.core.launch.props.BladePropertySource;
|
||||
import org.springblade.core.redis.config.RedisTemplateConfiguration;
|
||||
import org.springblade.core.social.cache.AuthStateRedisCache;
|
||||
import org.springblade.core.social.props.SocialProperties;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
/**
|
||||
* SocialConfiguration
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
@EnableConfigurationProperties(SocialProperties.class)
|
||||
@AutoConfiguration(after = RedisTemplateConfiguration.class)
|
||||
@BladePropertySource(value = "classpath:/blade-social.yml")
|
||||
public class SocialConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(Http.class)
|
||||
public Http simpleHttp() {
|
||||
HttpClientImpl httpClient = new HttpClientImpl();
|
||||
HttpUtil.setHttp(httpClient);
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(AuthStateCache.class)
|
||||
public AuthStateCache authStateCache(RedisTemplate<String, Object> redisTemplate) {
|
||||
return new AuthStateRedisCache(redisTemplate, redisTemplate.opsForValue());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* BladeX Commercial License Agreement
|
||||
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
|
||||
* <p>
|
||||
* Use of this software is governed by the Commercial License Agreement
|
||||
* obtained after purchasing a license from BladeX.
|
||||
* <p>
|
||||
* 1. This software is for development use only under a valid license
|
||||
* from BladeX.
|
||||
* <p>
|
||||
* 2. Redistribution of this software's source code to any third party
|
||||
* without a commercial license is strictly prohibited.
|
||||
* <p>
|
||||
* 3. Licensees may copyright their own code but cannot use segments
|
||||
* from this software for such purposes. Copyright of this software
|
||||
* remains with BladeX.
|
||||
* <p>
|
||||
* Using this software signifies agreement to this License, and the software
|
||||
* must not be used for illegal purposes.
|
||||
* <p>
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
|
||||
* not liable for any claims arising from secondary or illegal development.
|
||||
* <p>
|
||||
* Author: Chill Zhuang (bladejava@qq.com)
|
||||
*/
|
||||
package org.springblade.core.social.props;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.config.AuthDefaultSource;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* SocialProperties
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@ConfigurationProperties(prefix = "social")
|
||||
public class SocialProperties {
|
||||
|
||||
/**
|
||||
* 启用
|
||||
*/
|
||||
private Boolean enabled = false;
|
||||
|
||||
/**
|
||||
* 域名地址
|
||||
*/
|
||||
private String domain;
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
private Map<AuthDefaultSource, AuthConfig> oauth = Maps.newHashMap();
|
||||
|
||||
/**
|
||||
* 别名
|
||||
*/
|
||||
private Map<String, String> alias = Maps.newHashMap();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
/**
|
||||
* BladeX Commercial License Agreement
|
||||
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
|
||||
* <p>
|
||||
* Use of this software is governed by the Commercial License Agreement
|
||||
* obtained after purchasing a license from BladeX.
|
||||
* <p>
|
||||
* 1. This software is for development use only under a valid license
|
||||
* from BladeX.
|
||||
* <p>
|
||||
* 2. Redistribution of this software's source code to any third party
|
||||
* without a commercial license is strictly prohibited.
|
||||
* <p>
|
||||
* 3. Licensees may copyright their own code but cannot use segments
|
||||
* from this software for such purposes. Copyright of this software
|
||||
* remains with BladeX.
|
||||
* <p>
|
||||
* Using this software signifies agreement to this License, and the software
|
||||
* must not be used for illegal purposes.
|
||||
* <p>
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
|
||||
* not liable for any claims arising from secondary or illegal development.
|
||||
* <p>
|
||||
* Author: Chill Zhuang (bladejava@qq.com)
|
||||
*/
|
||||
package org.springblade.core.social.utils;
|
||||
|
||||
import me.zhyd.oauth.cache.AuthStateCache;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.config.AuthDefaultSource;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.request.*;
|
||||
import org.springblade.core.social.props.SocialProperties;
|
||||
import org.springblade.core.tool.utils.SpringUtil;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* SocialUtil
|
||||
*
|
||||
* @author Chill
|
||||
*/
|
||||
public class SocialUtil {
|
||||
|
||||
/**
|
||||
* 自定义state缓存
|
||||
*/
|
||||
private static AuthStateCache authStateCache;
|
||||
|
||||
public static AuthStateCache getAuthStateCache() {
|
||||
if (authStateCache == null) {
|
||||
authStateCache = SpringUtil.getBean(AuthStateCache.class);
|
||||
}
|
||||
return authStateCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据具体的授权来源,获取授权请求工具类
|
||||
*
|
||||
* @param source 授权来源
|
||||
* @return AuthRequest
|
||||
*/
|
||||
public static AuthRequest getAuthRequest(String source, SocialProperties socialProperties) {
|
||||
AuthDefaultSource authSource = Objects.requireNonNull(AuthDefaultSource.valueOf(source.toUpperCase()));
|
||||
AuthConfig authConfig = socialProperties.getOauth().get(authSource);
|
||||
if (authConfig == null) {
|
||||
throw new AuthException("未获取到有效的Auth配置");
|
||||
}
|
||||
AuthRequest authRequest = null;
|
||||
switch (authSource) {
|
||||
case GITHUB:
|
||||
authRequest = new AuthGithubRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case GITEE:
|
||||
authRequest = new AuthGiteeRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case OSCHINA:
|
||||
authRequest = new AuthOschinaRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case QQ:
|
||||
authRequest = new AuthQqRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case WECHAT_OPEN:
|
||||
authRequest = new AuthWeChatOpenRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case WECHAT_ENTERPRISE:
|
||||
authRequest = new AuthWeChatEnterpriseQrcodeRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case WECHAT_ENTERPRISE_WEB:
|
||||
authRequest = new AuthWeChatEnterpriseWebRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case WECHAT_MP:
|
||||
authRequest = new AuthWeChatMpRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case DINGTALK:
|
||||
authRequest = new AuthDingTalkRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case ALIPAY:
|
||||
// 支付宝在创建回调地址时,不允许使用localhost或者127.0.0.1,所以这儿的回调地址使用的局域网内的ip
|
||||
authRequest = new AuthAlipayRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case BAIDU:
|
||||
authRequest = new AuthBaiduRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case WEIBO:
|
||||
authRequest = new AuthWeiboRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case CODING:
|
||||
authRequest = new AuthCodingRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case CSDN:
|
||||
authRequest = new AuthCsdnRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case TAOBAO:
|
||||
authRequest = new AuthTaobaoRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case GOOGLE:
|
||||
authRequest = new AuthGoogleRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case FACEBOOK:
|
||||
authRequest = new AuthFacebookRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case DOUYIN:
|
||||
authRequest = new AuthDouyinRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case LINKEDIN:
|
||||
authRequest = new AuthLinkedinRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case MICROSOFT:
|
||||
authRequest = new AuthMicrosoftRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case MI:
|
||||
authRequest = new AuthMiRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case TOUTIAO:
|
||||
authRequest = new AuthToutiaoRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case TEAMBITION:
|
||||
authRequest = new AuthTeambitionRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case PINTEREST:
|
||||
authRequest = new AuthPinterestRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case RENREN:
|
||||
authRequest = new AuthRenrenRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case STACK_OVERFLOW:
|
||||
authRequest = new AuthStackOverflowRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case HUAWEI:
|
||||
authRequest = new AuthHuaweiRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case KUJIALE:
|
||||
authRequest = new AuthKujialeRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case GITLAB:
|
||||
authRequest = new AuthGitlabRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case MEITUAN:
|
||||
authRequest = new AuthMeituanRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case ELEME:
|
||||
authRequest = new AuthElemeRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
case TWITTER:
|
||||
authRequest = new AuthTwitterRequest(authConfig, getAuthStateCache());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (null == authRequest) {
|
||||
throw new AuthException("未获取到有效的Auth配置");
|
||||
}
|
||||
return authRequest;
|
||||
}
|
||||
|
||||
}
|
||||
3
blade-starter-social/src/main/resources/blade-social.yml
Normal file
3
blade-starter-social/src/main/resources/blade-social.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
blade:
|
||||
social:
|
||||
enabled: false
|
||||
Reference in New Issue
Block a user