1
0
mirror of synced 2025-12-13 09:47:55 +08:00

37 Commits

Author SHA1 Message Date
Yangkai.Shen
2de415c7ba 📝 更新 README.md 2019-11-04 16:26:13 +08:00
Yangkai.Shen
c670d9d10a 🔖 发布 1.3.0 2019-11-04 16:20:31 +08:00
Yangkai.Shen
dea73c1c6e 📝 添加 CHANGELOG 2019-11-04 16:20:14 +08:00
Yangkai.Shen
64a389aac4 支持 推特 2019-11-04 16:19:47 +08:00
Yangkai.Shen
e07cb8970b ⬆️ 升级 JustAuth 版本:1.13.0,支持 推特 2019-11-04 16:19:10 +08:00
Yangkai.Shen
1a690f5dd1 📝 添加 CHANGELOG 2019-11-04 16:12:35 +08:00
Yangkai.Shen
2029083f26 📝 添加 CHANGELOG 2019-11-04 16:11:54 +08:00
Yangkai.Shen
c00a127011 📝 更新 README.md 2019-11-04 16:08:34 +08:00
Yangkai.Shen
6b99039b98 🔧 修改 group id 2019-10-31 17:14:14 +08:00
Yangkai.Shen
0b00363336 📝 更新 README.md 2019-10-12 17:37:23 +08:00
Yangkai.Shen
70e9fc0af9 🔖 发布 1.2.1 2019-10-12 17:30:04 +08:00
Yangkai.Shen
2e723b2c51 📝 添加 CHANGELOG 2019-10-12 17:29:57 +08:00
Yangkai.Shen
011ad64fea 🚑 紧急修复Oauth列表空指针BUG 2019-10-12 17:28:37 +08:00
Yangkai.Shen
585edda9fa 🔖 发布 1.2.0 2019-10-09 16:17:38 +08:00
Yangkai.Shen
ddf066002b 📝 更新 README.md 2019-10-09 16:17:23 +08:00
Yangkai.Shen
479ce1abb0 📝 添加 CHANGELOG 2019-10-09 16:12:46 +08:00
Yangkai.Shen
a2ce42c0f3 🚚 移动文件 2019-10-09 15:46:26 +08:00
Yangkai.Shen
8eceb0f1cd 自定义扩展第三方平台 2019-10-09 15:29:18 +08:00
Yangkai.Shen
71e3a09565 ⬆️ 升级 Spring Boot 版本:2.1.8.RELEASE 2019-10-09 10:29:11 +08:00
Yangkai.Shen
4dbb9b2bb5 ⬆️ 升级 JustAuth 版本:1.12.0,支持 美团饿了么自定义扩展第三方平台 2019-10-09 10:28:04 +08:00
Yangkai.Shen
794c316859 🔧 更新私服地址 2019-09-18 10:38:12 +08:00
Yangkai.Shen
abd87213c1 📝 更新 README.md 2019-09-04 19:40:06 +08:00
Yangkai.Shen
e3d7054171 📝 更新 README.md 2019-09-04 19:34:47 +08:00
Yangkai.Shen
4a9187af34 📝 添加 CHANGELOG 2019-09-04 19:34:38 +08:00
Yangkai.Shen
30de3ea9bc 📝 更新 README.md 2019-09-04 18:58:21 +08:00
Yangkai.Shen
a6cefde0e5 📝 添加 CHANGELOG 2019-09-04 18:58:10 +08:00
Yangkai.Shen
4de27beb76 🔖 发布 1.1.0 2019-09-04 18:57:59 +08:00
Yangkai.Shen
741a322798 ⬆️ 升级 JustAuth 版本:1.11.0,支持 GitLab酷家乐 2019-09-04 18:57:04 +08:00
Yangkai.Shen
f702e70705 📝 更新 README.md 2019-09-02 17:56:31 +08:00
Yangkai.Shen
a42c80f22d 📝 更新 README.md 2019-08-31 13:24:52 +08:00
Yangkai.Shen
5c406fe4e1 Merge branch 'master' into dev 2019-08-31 13:19:00 +08:00
Yangkai.Shen
5a82dd0792 📝 更新 README.md 2019-08-31 13:12:19 +08:00
Yangkai.Shen
fa88e58055 📝 添加 CHANGELOG 2019-08-31 13:12:06 +08:00
Yangkai.Shen
c0ca2979a7 📝 补充注释 2019-08-31 12:38:42 +08:00
Yangkai.Shen
12369820e5 🎨 优化自动装配 2019-08-31 12:34:34 +08:00
Yangkai.Shen
3f5989c917 自定义prefix和超时时间 2019-08-31 11:35:38 +08:00
Yangkai.Shen
30e7f01a78 集成 redis 缓存配置,用户可以通过配置使用缓存,不需要自行实现 2019-08-31 11:04:25 +08:00
12 changed files with 432 additions and 75 deletions

View File

@@ -1,6 +1,27 @@
## 版本更新记录
### 【1.0.2-SNAPSHOT】2019-08-31
### 【1.3.0】2019-11-04
- 修改 `groupId``com.xkcoding.justauth`
- 升级 `JustAuth` 版本1.13.0,支持 `推特`
### 【1.2.1】2019-10-12
- 紧急修复Oauth列表空指针BUG
### 【1.2.0】2019-10-09
- 升级 `JustAuth` 版本1.12.0,支持 `饿了么``美团`
- 新增配置项,支持`自定义第三方平台扩展`
- 规范包结构
### 【1.1.0】2019-09-04
- 升级 `JustAuth` 版本1.11.0,支持 `GitLab``酷家乐`
- 添加内置的 Redis 缓存策略,通过配置项可配,不需要额外实现
- 支持 Redis 缓存自定义缓存前缀、自定义缓存过期时间
### *【1.0.2-SNAPSHOT】2019-08-31*
- 添加内置的 Redis 缓存策略,通过配置项可配,不需要额外实现
- 支持 Redis 缓存自定义缓存前缀、自定义缓存过期时间

250
README.md
View File

@@ -4,11 +4,9 @@
>
> JustAuth 脚手架
![Maven Central](https://img.shields.io/maven-central/v/com.xkcoding/justauth-spring-boot-starter.svg?color=brightgreen&label=Maven%20Central)
![Travis (.com)](https://img.shields.io/travis/com/xkcoding/justauth-spring-boot-starter.svg?label=Build%20Status)
![GitHub](https://img.shields.io/github/license/xkcoding/justauth-spring-boot-starter.svg)
![Maven Central](https://img.shields.io/maven-central/v/com.xkcoding.justauth/justauth-spring-boot-starter.svg?color=brightgreen&label=Maven%20Central)![Travis (.com)](https://img.shields.io/travis/com/xkcoding/justauth-spring-boot-starter.svg?label=Build%20Status)![GitHub](https://img.shields.io/github/license/xkcoding/justauth-spring-boot-starter.svg)
## Demo
## 1. Demo
懒得看文档的可以直接看demo
@@ -16,21 +14,21 @@ https://github.com/xkcoding/justauth-spring-boot-starter-demo
完整版 demohttps://github.com/xkcoding/spring-boot-demo/tree/master/spring-boot-demo-social
## 更新日志
## 2. 更新日志
[CHANGELOG](./CHANGELOG.md)
## 快速开始
## 3. 快速开始
### 1. 基础配置
### 3.1. 基础配置
- 引用依赖
```xml
<dependency>
<groupId>com.xkcoding</groupId>
<groupId>com.xkcoding.justauth</groupId>
<artifactId>justauth-spring-boot-starter</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>1.3.0</version>
</dependency>
```
@@ -65,35 +63,28 @@ public class TestController {
@GetMapping("/login/{type}")
public void login(@PathVariable String type, HttpServletResponse response) throws IOException {
AuthRequest authRequest = factory.get(getAuthSource(type));
AuthRequest authRequest = factory.get(type);
response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
}
@RequestMapping("/{type}/callback")
public AuthResponse login(@PathVariable String type, AuthCallback callback) {
AuthRequest authRequest = factory.get(getAuthSource(type));
AuthRequest authRequest = factory.get(type);
AuthResponse response = authRequest.login(callback);
log.info("【response】= {}", JSONUtil.toJsonStr(response));
return response;
}
private AuthSource getAuthSource(String type) {
if (StrUtil.isNotBlank(type)) {
return AuthSource.valueOf(type.toUpperCase());
} else {
throw new RuntimeException("不支持的类型");
}
}
}
```
### 2. 缓存配置
### 3.2. 缓存配置
> starter 内置了2种缓存实现一种是上面的默认实现另一种是基于 Redis 的缓存实现。
>
> 当然了,你也可以自定义实现你自己的缓存。
#### 2.1. 默认缓存实现
#### 3.2.1. 默认缓存实现
在配置文件配置如下内容即可
@@ -103,7 +94,7 @@ justauth:
type: default
```
#### 2.2. Redis 缓存实现
#### 3.2.2. Redis 缓存实现
1.添加 Redis 相关依赖
@@ -149,7 +140,7 @@ spring:
min-idle: 0
```
#### 2.3. 自定义缓存实现
#### 3.2.3. 自定义缓存实现
1.配置文件配置如下内容
@@ -240,36 +231,208 @@ public class AuthStateConfiguration {
}
```
## 附录
### 3.3. 自定义第三方平台配置
### 1. 配置
1.创建自定义的平台枚举类
`justauth` 配置列表
```java
/**
* <p>
* 扩展的自定义 source
* </p>
*
* @author yangkai.shen
* @date Created in 2019/10/9 14:14
*/
public enum ExtendSource implements AuthSource {
| 属性名 | 类型 | 默认值 | 可选项 | 描述 |
| ------------------ | ------------------------------------------------------------ | ------ | ---------- | ----------------- |
| `justauth.enabled` | `boolean` | true | true/false | 是否启用 JustAuth |
| `justauth.type` | `java.util.Map<me.zhyd.oauth.config.AuthSource,me.zhyd.oauth.config.AuthConfig>` | 无 | | JustAuth 配置 |
| `justauth.cache` | `com.xkcoding.justauth.properties.CacheProperties` | | | JustAuth缓存配置 |
/**
* 测试
*/
TEST {
/**
* 授权的api
*
* @return url
*/
@Override
public String authorize() {
return "http://authorize";
}
`justauth.type` 配置列表
/**
* 获取accessToken的api
*
* @return url
*/
@Override
public String accessToken() {
return "http://accessToken";
}
/**
* 获取用户信息的api
*
* @return url
*/
@Override
public String userInfo() {
return null;
}
/**
* 取消授权的api
*
* @return url
*/
@Override
public String revoke() {
return null;
}
/**
* 刷新授权的api
*
* @return url
*/
@Override
public String refresh() {
return null;
}
}
}
```
2.创建自定义的请求处理
```java
/**
* <p>
* 测试用自定义扩展的第三方request
* </p>
*
* @author yangkai.shen
* @date Created in 2019/10/9 14:19
*/
public class ExtendTestRequest extends AuthDefaultRequest {
public ExtendTestRequest(AuthConfig config) {
super(config, ExtendSource.TEST);
}
public ExtendTestRequest(AuthConfig config, AuthStateCache authStateCache) {
super(config, ExtendSource.TEST, authStateCache);
}
/**
* 获取access token
*
* @param authCallback 授权成功后的回调参数
* @return token
* @see AuthDefaultRequest#authorize()
* @see AuthDefaultRequest#authorize(String)
*/
@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
return AuthToken.builder().openId("openId").expireIn(1000).idToken("idToken").scope("scope").refreshToken("refreshToken").accessToken("accessToken").code("code").build();
}
/**
* 使用token换取用户信息
*
* @param authToken token信息
* @return 用户信息
* @see AuthDefaultRequest#getAccessToken(AuthCallback)
*/
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
return AuthUser.builder().username("test").nickname("test").gender(AuthUserGender.MALE).token(authToken).source(this.source.toString()).build();
}
/**
* 撤销授权
*
* @param authToken 登录成功后返回的Token信息
* @return AuthResponse
*/
@Override
public AuthResponse revoke(AuthToken authToken) {
return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).msg(AuthResponseStatus.SUCCESS.getMsg()).build();
}
/**
* 刷新access token (续期)
*
* @param authToken 登录成功后返回的Token信息
* @return AuthResponse
*/
@Override
public AuthResponse refresh(AuthToken authToken) {
return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).data(AuthToken.builder().openId("openId").expireIn(1000).idToken("idToken").scope("scope").refreshToken("refreshToken").accessToken("accessToken").code("code").build()).build();
}
}
```
3.在配置文件配置相关信息
```yaml
justauth:
enabled: true
extend:
enum-class: com.xkcoding.justauthspringbootstarterdemo.extend.ExtendSource
config:
TEST:
request-class: com.xkcoding.justauthspringbootstarterdemo.extend.ExtendTestRequest
client-id: xxxxxx
client-secret: xxxxxxxx
redirect-uri: http://oauth.xkcoding.com/demo/oauth/test/callback
```
## 4. 附录
### 4.1. 基础配置
#### 4.1.1. `justauth` 配置列表
| 属性名 | 类型 | 默认值 | 可选项 | 描述 |
| ------------------ | ------------------------------------------------------------ | ------ | ---------- | ---------------------- |
| `justauth.enabled` | `boolean` | true | true/false | 是否启用 JustAuth |
| `justauth.type` | `java.util.Map<me.zhyd.oauth.config.AuthSource,me.zhyd.oauth.config.AuthConfig>` | 无 | | JustAuth 配置 |
| `justauth.cache` | `com.xkcoding.justauth.autoconfigure.CacheProperties` | | | JustAuth缓存配置 |
| `justauth.extend` | `com.xkcoding.justauth.autoconfigure.ExtendProperties` | 无 | | JustAuth第三方平台配置 |
##### 4.1.1.1. `justauth.type` 配置列表
| 属性名 | 描述 |
| --------------------------- | ------------------------------------------------------------ |
| `justauth.type.keys` | `justauth.type``Map` 格式的key 的取值请参考 [`AuthSource`](https://github.com/zhangyd-c/JustAuth/blob/master/src/main/java/me/zhyd/oauth/config/AuthSource.java) |
| `justauth.type.keys.values` | `justauth.type``Map` 格式的value 的取值请参考 [`AuthConfig`](https://github.com/zhangyd-c/JustAuth/blob/master/src/main/java/me/zhyd/oauth/config/AuthConfig.java) |
`justauth.cache` 配置列表
##### 4.1.1.2.`justauth.cache` 配置列表
| 属性名 | 类型 | 默认值 | 可选项 | 描述 |
| ------------------------ | ------------------------------------------------------------ | ----------------- | -------------------- | ------------------------------------------------------------ |
| `justauth.cache.type` | `com.xkcoding.justauth.properties.CacheProperties.CacheType` | default | default/redis/custom | 缓存类型default使用JustAuth默认的缓存实现redis使用默认的redis缓存实现custom用户自定义缓存实现 |
| `justauth.cache.prefix` | `string` | JUSTAUTH::STATE:: | | 缓存前缀目前只对redis缓存生效默认 JUSTAUTH::STATE:: |
| `justauth.cache.timeout` | `java.time.Duration` | 3分钟 | | 超时时长目前只对redis缓存生效默认3分钟 |
| `justauth.cache.type` | `com.xkcoding.justauth.autoconfigure.CacheProperties.CacheType` | default | default/redis/custom | 缓存类型default使用JustAuth默认的缓存实现redis使用默认的redis缓存实现custom用户自定义缓存实现 |
| `justauth.cache.prefix` | `java.lang.String` | JUSTAUTH::STATE:: | | 缓存前缀目前只对redis缓存生效默认 `JUSTAUTH::STATE::` |
| `justauth.cache.timeout` | `java.time.Duration` | 3分钟 | | 超时时长目前只对redis缓存生效默认`3分钟` |
### 2. 私服
##### 4.1.1.3.`justauth.extend` 配置列表
如果想体验快照版本,需要在 `pom.xml` 文件里添加如下配置
| 属性名 | 类型 | 默认值 | 可选项 | 描述 |
| ---------------------------- | -------------------------------------------- | ------ | ------ | ------------ |
| `justauth.extend.enum-class` | `Class<? extends AuthSource>` | 无 | | 枚举类全路径 |
| `justauth.extend.config` | `java.util.Map<String, ExtendRequestConfig>` | 无 | | 对应配置信息 |
###### 4.1.1.3.1.`justauth.extend.config` 配置列表
| 属性名 | 类型 | 默认值 | 可选项 | 描述 |
| ------------------------------- | ------------------------------------------------------------ | ------ | ------ | ------------------------------------------------------------ |
| `justauth.extend.config.keys` | `java.lang.String` | 无 | | key 必须在 `justauth.extend.enum-class` 配置的枚举类中声明 |
| `justauth.extend.config.values` | `com.xkcoding.justauth.autoconfigure.ExtendProperties.ExtendRequestConfig` | 无 | | value 就是 `AuthConfig` 的子类,增加了一个 `request-class` 属性配置请求的全类名,具体参考类[`ExtendProperties.ExtendRequestConfig`](https://github.com/justauth/justauth-spring-boot-starter/blob/master/src/main/java/com/xkcoding/justauth/autoconfigure/ExtendProperties.java#L49-L54) |
### 4.2. SNAPSHOT版本
![https://img.shields.io/badge/snapshots-1.3.0--SNAPSHOT-green](https://img.shields.io/badge/snapshots-1.3.0--SNAPSHOT-green)如果需要体验快照版本,可以在你的 `pom.xml`进行如下配置:
```xml
<repositories>
@@ -279,11 +442,11 @@ public class AuthStateConfiguration {
<name>aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
<!--xkcoding 私服-->
<!--中央仓库-->
<repository>
<id>xkcoding-nexus</id>
<name>xkcoding nexus</name>
<url>https://nexus.xkcoding.com/repository/maven-public/</url>
<id>oss</id>
<name>oss</name>
<url>http://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>true</enabled>
</releases>
@@ -291,5 +454,6 @@ public class AuthStateConfiguration {
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
```
</repositories>
```

18
pom.xml
View File

@@ -21,9 +21,9 @@
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>
<groupId>com.xkcoding</groupId>
<groupId>com.xkcoding.justauth</groupId>
<artifactId>justauth-spring-boot-starter</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>1.3.0</version>
<name>justauth-spring-boot-starter</name>
<url>https://github.com/xkcoding/justauth-spring-boot-starter</url>
@@ -65,9 +65,9 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!--SpringBoot版本-->
<spring-boot.version>2.1.6.RELEASE</spring-boot.version>
<spring-boot.version>2.1.8.RELEASE</spring-boot.version>
<!--JustAuth版本-->
<justauth.version>1.10.1</justauth.version>
<justauth.version>1.13.0</justauth.version>
</properties>
<dependencies>
@@ -260,14 +260,14 @@
<distributionManagement>
<!-- 发布版私服仓库 -->
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<id>xkcoding-nexus</id>
<name>xkcoding Nexus Release Repository</name>
<url>https://nexus.xkcoding.com/repository/maven-releases/</url>
</repository>
<!-- 快照版私服仓库 -->
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<id>xkcoding-nexus</id>
<name>xkcoding Nexus Snapshot Repository</name>
<url>https://nexus.xkcoding.com/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
@@ -296,7 +296,7 @@
<!-- 发布到GitHub仓库 -->
<repository>
<id>github-xkcoding</id>
<name>GitHub Xkcoding Apache Maven Packages</name>
<name>xkcoding</name>
<url>https://maven.pkg.github.com/xkcoding/justauth-spring-boot-starter</url>
</repository>
</distributionManagement>

View File

@@ -17,16 +17,26 @@
package com.xkcoding.justauth;
import com.xkcoding.justauth.properties.JustAuthProperties;
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.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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
@@ -37,6 +47,7 @@ import java.util.stream.Collectors;
* @author yangkai.shen
* @date Created in 2019-07-22 14:21
*/
@Slf4j
@RequiredArgsConstructor
public class AuthRequestFactory {
private final JustAuthProperties properties;
@@ -47,8 +58,22 @@ public class AuthRequestFactory {
*
* @return Oauth列表
*/
@SuppressWarnings("unchecked")
public List<String> oauthList() {
return properties.getType().keySet().stream().map(Enum::name).collect(Collectors.toList());
// 默认列表
List<String> defaultList = properties.getType().keySet().stream().map(Enum::name).collect(Collectors.toList());
// 扩展列表
List<String> extendList = new ArrayList<>();
ExtendProperties extend = properties.getExtend();
if (null != extend) {
Class enumClass = extend.getEnumClass();
List<String> names = EnumUtil.getNames(enumClass);
// 扩展列表
extendList = extend.getConfig().keySet().stream().filter(x -> names.contains(x.toUpperCase())).map(String::toUpperCase).collect(Collectors.toList());
}
// 合并
return (List<String>) CollUtil.addAll(defaultList, extendList);
}
/**
@@ -57,9 +82,85 @@ public class AuthRequestFactory {
* @param source {@link AuthSource}
* @return {@link AuthRequest}
*/
public AuthRequest get(AuthSource source) {
AuthConfig config = properties.getType().get(source);
switch (source) {
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")
private AuthRequest getExtendRequest(Class clazz, String source) {
try {
EnumUtil.fromString(clazz, source.toUpperCase());
} catch (IllegalArgumentException e) {
// 无自定义匹配
return null;
}
Map<String, ExtendProperties.ExtendRequestConfig> extendConfig = properties.getExtend().getConfig();
// key 转大写
Map<String, ExtendProperties.ExtendRequestConfig> upperConfig = new HashMap<>(6);
extendConfig.forEach((k, v) -> upperConfig.put(k.toUpperCase(), v));
ExtendProperties.ExtendRequestConfig extendRequestConfig = upperConfig.get(source.toUpperCase());
if (extendRequestConfig != null) {
Class<? extends AuthRequest> 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);
// 找不到对应关系,直接返回空
if (config == null) {
return null;
}
switch (authDefaultSource) {
case GITHUB:
return new AuthGithubRequest(config, authStateCache);
case WEIBO:
@@ -112,8 +213,18 @@ public class AuthRequestFactory {
return new AuthHuaweiRequest(config, authStateCache);
case WECHAT_ENTERPRISE:
return new AuthWeChatEnterpriseRequest(config, authStateCache);
case GITLAB:
return new AuthGitlabRequest(config, authStateCache);
case KUJIALE:
return new AuthKujialeRequest(config, authStateCache);
case ELEME:
return new AuthElemeRequest(config, authStateCache);
case MEITUAN:
return new AuthMeituanRequest(config, authStateCache);
case TWITTER:
return new AuthTwitterRequest(config, authStateCache);
default:
throw new AuthException(AuthResponseStatus.UNSUPPORTED);
return null;
}
}
}

View File

@@ -15,7 +15,7 @@
*
*/
package com.xkcoding.justauth.properties;
package com.xkcoding.justauth.autoconfigure;
import lombok.Getter;
import lombok.Setter;

View File

@@ -0,0 +1,56 @@
/*
* Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com).
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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 com.xkcoding.justauth.autoconfigure;
import lombok.Getter;
import lombok.Setter;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.request.AuthRequest;
import java.util.HashMap;
import java.util.Map;
/**
* <p>
* 扩展配置
* </p>
*
* @author yangkai.shen
* @date Created in 2019/10/9 11:21
*/
@Getter
@Setter
public class ExtendProperties {
/**
* 枚举类全路径
*/
private Class<? extends AuthSource> enumClass;
private Map<String, ExtendRequestConfig> config = new HashMap<>();
@Getter
@Setter
public static class ExtendRequestConfig extends AuthConfig{
/**
* 请求对应全路径
*/
private Class<? extends AuthRequest> requestClass;
}
}

View File

@@ -15,9 +15,9 @@
*
*/
package com.xkcoding.justauth;
package com.xkcoding.justauth.autoconfigure;
import com.xkcoding.justauth.properties.JustAuthProperties;
import com.xkcoding.justauth.AuthRequestFactory;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.cache.AuthStateCache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

View File

@@ -15,12 +15,12 @@
*
*/
package com.xkcoding.justauth.properties;
package com.xkcoding.justauth.autoconfigure;
import lombok.Getter;
import lombok.Setter;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.config.AuthDefaultSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
@@ -45,14 +45,20 @@ public class JustAuthProperties {
private boolean enabled;
/**
* JustAuth 配置
* JustAuth 默认配置
*/
private Map<AuthSource, AuthConfig> type = new HashMap<>();
private Map<AuthDefaultSource, AuthConfig> type = new HashMap<>();
/**
* JustAuth 自定义配置
*/
@NestedConfigurationProperty
private ExtendProperties extend;
/**
* 缓存配置类
*/
@NestedConfigurationProperty
private CacheProperties cache;
private CacheProperties cache = new CacheProperties();
}

View File

@@ -15,10 +15,9 @@
*
*/
package com.xkcoding.justauth;
package com.xkcoding.justauth.autoconfigure;
import com.xkcoding.justauth.cache.RedisStateCache;
import com.xkcoding.justauth.properties.JustAuthProperties;
import com.xkcoding.justauth.support.cache.RedisStateCache;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.cache.AuthDefaultStateCache;
import me.zhyd.oauth.cache.AuthStateCache;

View File

@@ -15,9 +15,9 @@
*
*/
package com.xkcoding.justauth.cache;
package com.xkcoding.justauth.support.cache;
import com.xkcoding.justauth.properties.CacheProperties;
import com.xkcoding.justauth.autoconfigure.CacheProperties;
import lombok.RequiredArgsConstructor;
import me.zhyd.oauth.cache.AuthStateCache;
import org.springframework.data.redis.core.RedisTemplate;

View File

@@ -4,7 +4,7 @@
"name": "justauth.type",
"type": "java.util.Map<me.zhyd.oauth.config.AuthSource,me.zhyd.oauth.config.AuthConfig>",
"description": "JustAuth 配置.",
"sourceType": "com.xkcoding.justauth.properties.JustAuthProperties"
"sourceType": "com.xkcoding.justauth.autoconfigure.JustAuthProperties"
}
],
"hints": [
@@ -129,4 +129,4 @@
]
}
]
}
}

View File

@@ -1,2 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xkcoding.justauth.JustAuthAutoConfiguration
com.xkcoding.justauth.autoconfigure.JustAuthAutoConfiguration