1
0
mirror of synced 2025-12-12 00:48:04 +08:00

30 Commits
0.0.2 ... 1.1.0

Author SHA1 Message Date
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
cf6ef0de49 📝 更新 README.md 2019-08-31 13:15:24 +08:00
Yangkai.Shen
59da8a1c61 📝 添加 CHANGELOG 2019-08-31 13:15:24 +08:00
Yangkai.Shen
9dbe77b703 📝 补充注释 2019-08-31 13:15:24 +08:00
Yangkai.Shen
1a890c4497 🎨 优化自动装配 2019-08-31 13:15:24 +08:00
Yangkai.Shen
263930ed34 自定义prefix和超时时间 2019-08-31 13:15:24 +08:00
Yangkai.Shen
e03aa81f33 集成 redis 缓存配置,用户可以通过配置使用缓存,不需要自行实现 2019-08-31 13:15:24 +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
Yangkai.Shen
219f390284 📦 添加 github 打包地址 2019-08-21 09:59:58 +08:00
Yangkai.Shen
2f6946c6b1 ⬆️ 升级 JustAuth 版本:1.10.1,AuthUser添加构造函数,支持反序列化 2019-08-19 09:21:25 +08:00
Yangkai.Shen
1a4d50bbb7 📝 添加 CHANGELOG 2019-08-09 16:22:51 +08:00
Yangkai.Shen
395e390f50 📝 更新 README.md 2019-08-09 16:20:37 +08:00
Yangkai.Shen
1a2b6463a1 ⬆️ 更新 LICENSE 2019-08-08 16:27:37 +08:00
Yangkai.Shen
03977a9567 📝 添加 LICENSE 和 COPYRIGHT 2019-08-08 16:22:36 +08:00
Yangkai.Shen
affb35bcf2 ⬆️ 升级 JustAuth 版本到 1.10.0,支持华为登录、企业微信登录,自定义State缓存 2019-08-08 16:11:48 +08:00
Yangkai.Shen
1182717434 支持 企业微信 2019-08-06 16:25:54 +08:00
Yangkai.Shen
ee2b8e6c7a 提供Oauth列表 2019-08-02 18:31:19 +08:00
Yangkai.Shen
5b5440d223 支持外部自定义 Cache 缓存 state 2019-08-02 16:16:22 +08:00
Yangkai.Shen
1ea5001700 支持外部自定义 Cache 缓存 state 2019-08-02 15:03:09 +08:00
11 changed files with 752 additions and 39 deletions

View File

@@ -1,5 +1,19 @@
## 版本更新记录
### 【1.1.0】2019-09-04
- 升级 `JustAuth` 版本1.11.0,支持 `GitLab``酷家乐`
- 添加内置的 Redis 缓存策略,通过配置项可配,不需要额外实现
- 支持 Redis 缓存自定义缓存前缀、自定义缓存过期时间
### 【1.0.1】2019-08-19
- 升级 `JustAuth` 版本1.10.1AuthUser添加构造函数支持反序列化
### 【1.0.0】2019-08-08
- 升级 `JustAuth` 版本1.10.0,支持 `华为登录``企业微信登录``自定义State缓存`
### 【0.0.2】2019-08-01
- 升级 `JustAuth` 版本1.9.5

64
LICENSE Normal file
View File

@@ -0,0 +1,64 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com).
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
“The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.

238
README.md
View File

@@ -14,15 +14,23 @@
https://github.com/xkcoding/justauth-spring-boot-starter-demo
完整版 demohttps://github.com/xkcoding/spring-boot-demo/tree/master/spring-boot-demo-social
## 更新日志
[CHANGELOG](./CHANGELOG.md)
## 快速开始
### 1. 基础配置
- 引用依赖
```xml
<dependency>
<groupId>com.xkcoding</groupId>
<artifactId>justauth-spring-boot-starter</artifactId>
<version>0.0.2</version>
<version>1.1.0</version>
</dependency>
```
@@ -36,6 +44,8 @@ justauth:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/qq/callback
cache:
type: default
```
- 然后就开始玩耍吧~
@@ -48,30 +58,199 @@ justauth:
public class TestController {
private final AuthRequestFactory factory;
@GetMapping("/login/qq")
public void login(HttpServletResponse response) throws IOException {
AuthRequest authRequest = factory.get(AuthSource.QQ);
@GetMapping
public List<String> list() {
return factory.oauthList();
}
@GetMapping("/login/{type}")
public void login(@PathVariable String type, HttpServletResponse response) throws IOException {
AuthRequest authRequest = factory.get(getAuthSource(type));
response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
}
@RequestMapping("/qq/callback")
public AuthResponse login(AuthCallback callback) {
AuthRequest authRequest2 = factory.get(AuthSource.QQ);
AuthResponse response = authRequest2.login(callback);
@RequestMapping("/{type}/callback")
public AuthResponse login(@PathVariable String type, AuthCallback callback) {
AuthRequest authRequest = factory.get(getAuthSource(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. 缓存配置
> starter 内置了2种缓存实现一种是上面的默认实现另一种是基于 Redis 的缓存实现。
>
> 当然了,你也可以自定义实现你自己的缓存。
#### 2.1. 默认缓存实现
在配置文件配置如下内容即可
```yaml
justauth:
cache:
type: default
```
#### 2.2. Redis 缓存实现
1.添加 Redis 相关依赖
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 对象池使用redis时必须引入 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
```
2.配置文件配置如下内容即可
```yaml
justauth:
cache:
type: redis
# 缓存前缀目前只对redis缓存生效默认 JUSTAUTH::STATE::
prefix: ''
# 超时时长目前只对redis缓存生效默认3分钟
timeout: 1h
spring:
redis:
host: localhost
# 连接超时时间记得添加单位Duration
timeout: 10000ms
# Redis默认情况下有16个分片这里配置具体使用的分片
# database: 0
lettuce:
pool:
# 连接池最大连接数(使用负值表示没有限制) 默认 8
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
max-wait: -1ms
# 连接池中的最大空闲连接 默认 8
max-idle: 8
# 连接池中的最小空闲连接 默认 0
min-idle: 0
```
#### 2.3. 自定义缓存实现
1.配置文件配置如下内容
```yaml
justauth:
cache:
type: custom
```
2.自定义缓存实现 `AuthStateCache` 接口
```java
/**
* <p>
* 自定义缓存实现
* </p>
*
* @author yangkai.shen
* @date Created in 2019/8/31 12:53
*/
public class MyAuthStateCache implements AuthStateCache {
/**
* 存入缓存
*
* @param key 缓存key
* @param value 缓存内容
*/
@Override
public void cache(String key, String value) {
// TODO: 自定义存入缓存
}
/**
* 存入缓存
*
* @param key 缓存key
* @param value 缓存内容
* @param timeout 指定缓存过期时间(毫秒)
*/
@Override
public void cache(String key, String value, long timeout) {
// TODO: 自定义存入缓存
}
/**
* 获取缓存内容
*
* @param key 缓存key
* @return 缓存内容
*/
@Override
public String get(String key) {
// TODO: 自定义获取缓存内容
return null;
}
/**
* 是否存在key如果对应key的value值已过期也返回false
*
* @param key 缓存key
* @return true存在key并且value没过期falsekey不存在或者已过期
*/
@Override
public boolean containsKey(String key) {
// TODO: 自定义判断key是否存在
return false;
}
}
```
3.自动装配 `JustAuthConfig`
```java
/**
* <p>
* 自定义缓存装配
* </p>
*
* @author yangkai.shen
* @date Created in 2019/8/31 12:29
*/
@Configuration
public class AuthStateConfiguration {
@Bean
public AuthStateCache authStateCache() {
return new MyAuthStateCache();
}
}
```
## 附录
### 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.properties.CacheProperties` | | | JustAuth缓存配置 |
`justauth.type` 配置列表
@@ -80,3 +259,46 @@ public class TestController {
| `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` 配置列表
| 属性名 | 类型 | 默认值 | 可选项 | 描述 |
| ------------------------ | ------------------------------------------------------------ | ----------------- | -------------------- | ------------------------------------------------------------ |
| `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分钟 |
### 2. SNAPSHOT版本
如果需要体验快照版本可以在你的maven目录找到 `settings.xml`进行如下配置:
```xml
<profiles>
<profile>
<id>justauth-test</id>
<repositories>
<!--阿里云私服-->
<repository>
<id>aliyun</id>
<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>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>justauth-test</activeProfile>
</activeProfiles>
```

66
pom.xml
View File

@@ -1,4 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
~
-->
<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">
@@ -6,7 +23,7 @@
<groupId>com.xkcoding</groupId>
<artifactId>justauth-spring-boot-starter</artifactId>
<version>0.0.2</version>
<version>1.1.0</version>
<name>justauth-spring-boot-starter</name>
<url>https://github.com/xkcoding/justauth-spring-boot-starter</url>
@@ -30,8 +47,8 @@
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0</url>
<name>GNU Lesser General Public License v3.0</name>
<url>https://www.gnu.org/licenses/lgpl-3.0.html</url>
<distribution>repo</distribution>
</license>
</licenses>
@@ -50,7 +67,7 @@
<!--SpringBoot版本-->
<spring-boot.version>2.1.6.RELEASE</spring-boot.version>
<!--JustAuth版本-->
<justauth.version>1.9.5</justauth.version>
<justauth.version>1.11.0</justauth.version>
</properties>
<dependencies>
@@ -59,9 +76,21 @@
<artifactId>JustAuth</artifactId>
<version>${justauth.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<scope>provided</scope>
</dependency>
<!-- 对象池使用redis时必须引入 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<optional>true</optional>
</dependency>
<!--https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/configuration-metadata.html#configuration-metadata-annotation-processor-->
<dependency>
@@ -243,6 +272,35 @@
</snapshotRepository>
</distributionManagement>
</profile>
<profile>
<id>github</id>
<build>
<plugins>
<!-- GPG -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<!-- 发布到GitHub仓库 -->
<repository>
<id>github-xkcoding</id>
<name>GitHub Xkcoding Apache Maven Packages</name>
<url>https://maven.pkg.github.com/xkcoding/justauth-spring-boot-starter</url>
</repository>
</distributionManagement>
</profile>
</profiles>
</project>

View File

@@ -1,13 +1,34 @@
/*
* 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;
import com.xkcoding.justauth.properties.JustAuthProperties;
import lombok.RequiredArgsConstructor;
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig;
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.List;
import java.util.stream.Collectors;
/**
* <p>
* AuthRequest工厂类
@@ -19,6 +40,16 @@ import me.zhyd.oauth.request.*;
@RequiredArgsConstructor
public class AuthRequestFactory {
private final JustAuthProperties properties;
private final AuthStateCache authStateCache;
/**
* 返回当前Oauth列表
*
* @return Oauth列表
*/
public List<String> oauthList() {
return properties.getType().keySet().stream().map(Enum::name).collect(Collectors.toList());
}
/**
* 返回AuthRequest对象
@@ -30,53 +61,61 @@ public class AuthRequestFactory {
AuthConfig config = properties.getType().get(source);
switch (source) {
case GITHUB:
return new AuthGithubRequest(config);
return new AuthGithubRequest(config, authStateCache);
case WEIBO:
return new AuthWeiboRequest(config);
return new AuthWeiboRequest(config, authStateCache);
case GITEE:
return new AuthGiteeRequest(config);
return new AuthGiteeRequest(config, authStateCache);
case DINGTALK:
return new AuthDingTalkRequest(config);
return new AuthDingTalkRequest(config, authStateCache);
case BAIDU:
return new AuthBaiduRequest(config);
return new AuthBaiduRequest(config, authStateCache);
case CSDN:
return new AuthCsdnRequest(config);
return new AuthCsdnRequest(config, authStateCache);
case CODING:
return new AuthCodingRequest(config);
return new AuthCodingRequest(config, authStateCache);
case TENCENT_CLOUD:
return new AuthTencentCloudRequest(config);
return new AuthTencentCloudRequest(config, authStateCache);
case OSCHINA:
return new AuthOschinaRequest(config);
return new AuthOschinaRequest(config, authStateCache);
case ALIPAY:
return new AuthAlipayRequest(config);
return new AuthAlipayRequest(config, authStateCache);
case QQ:
return new AuthQqRequest(config);
return new AuthQqRequest(config, authStateCache);
case WECHAT:
return new AuthWeChatRequest(config);
return new AuthWeChatRequest(config, authStateCache);
case TAOBAO:
return new AuthTaobaoRequest(config);
return new AuthTaobaoRequest(config, authStateCache);
case GOOGLE:
return new AuthGoogleRequest(config);
return new AuthGoogleRequest(config, authStateCache);
case FACEBOOK:
return new AuthFacebookRequest(config);
return new AuthFacebookRequest(config, authStateCache);
case DOUYIN:
return new AuthDouyinRequest(config);
return new AuthDouyinRequest(config, authStateCache);
case LINKEDIN:
return new AuthLinkedinRequest(config);
return new AuthLinkedinRequest(config, authStateCache);
case MICROSOFT:
return new AuthMicrosoftRequest(config);
return new AuthMicrosoftRequest(config, authStateCache);
case MI:
return new AuthMiRequest(config);
return new AuthMiRequest(config, authStateCache);
case TOUTIAO:
return new AuthToutiaoRequest(config);
return new AuthToutiaoRequest(config, authStateCache);
case TEAMBITION:
return new AuthTeambitionRequest(config);
return new AuthTeambitionRequest(config, authStateCache);
case RENREN:
return new AuthRenrenRequest(config);
return new AuthRenrenRequest(config, authStateCache);
case PINTEREST:
return new AuthPinterestRequest(config);
return new AuthPinterestRequest(config, authStateCache);
case STACK_OVERFLOW:
return new AuthStackOverflowRequest(config);
return new AuthStackOverflowRequest(config, authStateCache);
case HUAWEI:
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);
default:
throw new AuthException(AuthResponseStatus.UNSUPPORTED);
}

View File

@@ -1,10 +1,30 @@
/*
* 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;
import com.xkcoding.justauth.properties.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;
import org.springframework.context.annotation.Import;
/**
* <p>
@@ -14,14 +34,21 @@ import org.springframework.context.annotation.Configuration;
* @author yangkai.shen
* @date Created in 2019-07-22 10:52
*/
@Slf4j
@Configuration
@EnableConfigurationProperties(JustAuthProperties.class)
public class JustAuthAutoConfiguration {
@Bean
@ConditionalOnProperty(prefix = "justauth", value = "enabled", havingValue = "true", matchIfMissing = true)
public AuthRequestFactory authRequestFactory(JustAuthProperties properties) {
return new AuthRequestFactory(properties);
public AuthRequestFactory authRequestFactory(JustAuthProperties properties, AuthStateCache authStateCache) {
return new AuthRequestFactory(properties, authStateCache);
}
@Configuration
@Import({JustAuthStateCacheConfiguration.Default.class, JustAuthStateCacheConfiguration.Redis.class, JustAuthStateCacheConfiguration.Custom.class})
protected static class AuthStateCacheAutoConfiguration {
}
}

View File

@@ -0,0 +1,105 @@
/*
* 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;
import com.xkcoding.justauth.cache.RedisStateCache;
import com.xkcoding.justauth.properties.JustAuthProperties;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.cache.AuthDefaultStateCache;
import me.zhyd.oauth.cache.AuthStateCache;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* <p>
* JustAuth 缓存装配类,{@link JustAuthAutoConfiguration.AuthStateCacheAutoConfiguration}
* </p>
*
* @author yangkai.shen
* @date Created in 2019/8/31 12:00
*/
@Slf4j
abstract class JustAuthStateCacheConfiguration {
/**
* Redis 缓存
*/
@ConditionalOnClass(RedisTemplate.class)
@ConditionalOnMissingBean(AuthStateCache.class)
@AutoConfigureBefore(RedisAutoConfiguration.class)
@ConditionalOnProperty(name = "justauth.cache.type", havingValue = "redis", matchIfMissing = true)
static class Redis {
static {
log.debug("JustAuth 使用 Redis 缓存存储 state 数据");
}
@Bean
public RedisTemplate<String, String> justAuthRedisCacheTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, String> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
public AuthStateCache authStateCache(RedisTemplate<String, String> justAuthRedisCacheTemplate, JustAuthProperties justAuthProperties) {
return new RedisStateCache(justAuthRedisCacheTemplate, justAuthProperties.getCache());
}
}
/**
* 默认缓存
*/
@ConditionalOnMissingBean(AuthStateCache.class)
@ConditionalOnProperty(name = "justauth.cache.type", havingValue = "default", matchIfMissing = true)
static class Default {
static {
log.debug("JustAuth 使用 默认缓存存储 state 数据");
}
@Bean
public AuthStateCache authStateCache() {
return AuthDefaultStateCache.INSTANCE;
}
}
/**
* 默认缓存
*/
@ConditionalOnProperty(name = "justauth.cache.type", havingValue = "custom")
static class Custom {
static {
log.debug("JustAuth 使用 自定义缓存存储 state 数据");
}
@Bean
@ConditionalOnMissingBean(AuthStateCache.class)
public AuthStateCache authStateCache() {
log.error("请自行实现 me.zhyd.oauth.cache.AuthStateCache");
throw new RuntimeException();
}
}
}

View File

@@ -0,0 +1,88 @@
/*
* 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.cache;
import com.xkcoding.justauth.properties.CacheProperties;
import lombok.RequiredArgsConstructor;
import me.zhyd.oauth.cache.AuthStateCache;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.concurrent.TimeUnit;
/**
* <p>
* Redis作为JustAuth的State的缓存
* </p>
*
* @author yangkai.shen
* @date Created in 2019-08-02 15:10
*/
@RequiredArgsConstructor
public class RedisStateCache implements AuthStateCache {
private final RedisTemplate<String, String> redisTemplate;
private final CacheProperties cacheProperties;
/**
* 存入缓存
*
* @param key 缓存key
* @param value 缓存内容
*/
@Override
public void cache(String key, String value) {
this.cache(key, value, cacheProperties.getTimeout().toMillis());
}
/**
* 存入缓存
*
* @param key 缓存key
* @param value 缓存内容
* @param timeout 指定缓存过期时间(毫秒)
*/
@Override
public void cache(String key, String value, long timeout) {
redisTemplate.opsForValue().set(cacheProperties.getPrefix() + key, value, timeout, TimeUnit.MILLISECONDS);
}
/**
* 获取缓存内容
*
* @param key 缓存key
* @return 缓存内容
*/
@Override
public String get(String key) {
return redisTemplate.opsForValue().get(cacheProperties.getPrefix() + key);
}
/**
* 是否存在key如果对应key的value值已过期也返回false
*
* @param key 缓存key
* @return true存在key并且value没过期falsekey不存在或者已过期
*/
@Override
public boolean containsKey(String key) {
Long expire = redisTemplate.getExpire(cacheProperties.getPrefix() + key, TimeUnit.MILLISECONDS);
if (expire == null) {
expire = 0L;
}
return expire > 0;
}
}

View File

@@ -0,0 +1,71 @@
/*
* 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.properties;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.time.Duration;
/**
* <p>
* 缓存配置类
* </p>
*
* @author yangkai.shen
* @date Created in 2019/8/31 10:18
*/
@Getter
@Setter
public class CacheProperties {
/**
* 缓存类型
*/
private CacheType type = CacheType.DEFAULT;
/**
* 缓存前缀目前只对redis缓存生效默认 JUSTAUTH::STATE::
*/
private String prefix = "JUSTAUTH::STATE::";
/**
* 超时时长目前只对redis缓存生效默认3分钟
*/
private Duration timeout = Duration.ofMinutes(3);
/**
* 缓存类型
*/
@Getter
@ToString
public enum CacheType {
/**
* 使用JustAuth内置的缓存
*/
DEFAULT,
/**
* 使用Redis缓存
*/
REDIS,
/**
* 自定义缓存
*/
CUSTOM;
}
}

View File

@@ -1,3 +1,20 @@
/*
* 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.properties;
import lombok.Getter;
@@ -5,6 +22,7 @@ import lombok.Setter;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import java.util.HashMap;
import java.util.Map;
@@ -31,4 +49,10 @@ public class JustAuthProperties {
*/
private Map<AuthSource, AuthConfig> type = new HashMap<>();
/**
* 缓存配置类
*/
@NestedConfigurationProperty
private CacheProperties cache;
}

View File

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