Compare commits
7 Commits
1.0.1
...
1.0.2-SNAP
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cf6ef0de49 | ||
|
|
59da8a1c61 | ||
|
|
9dbe77b703 | ||
|
|
1a890c4497 | ||
|
|
263930ed34 | ||
|
|
e03aa81f33 | ||
|
|
219f390284 |
@@ -1,5 +1,10 @@
|
|||||||
## 版本更新记录
|
## 版本更新记录
|
||||||
|
|
||||||
|
### 【1.0.2-SNAPSHOT】2019-08-31
|
||||||
|
|
||||||
|
- 添加内置的 Redis 缓存策略,通过配置项可配,不需要额外实现
|
||||||
|
- 支持 Redis 缓存自定义缓存前缀、自定义缓存过期时间
|
||||||
|
|
||||||
### 【1.0.1】2019-08-19
|
### 【1.0.1】2019-08-19
|
||||||
|
|
||||||
- 升级 `JustAuth` 版本:1.10.1,AuthUser添加构造函数,支持反序列化
|
- 升级 `JustAuth` 版本:1.10.1,AuthUser添加构造函数,支持反序列化
|
||||||
|
|||||||
300
README.md
300
README.md
@@ -22,13 +22,15 @@ https://github.com/xkcoding/justauth-spring-boot-starter-demo
|
|||||||
|
|
||||||
## 快速开始
|
## 快速开始
|
||||||
|
|
||||||
|
### 1. 基础配置
|
||||||
|
|
||||||
- 引用依赖
|
- 引用依赖
|
||||||
|
|
||||||
```xml
|
```xml
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.xkcoding</groupId>
|
<groupId>com.xkcoding</groupId>
|
||||||
<artifactId>justauth-spring-boot-starter</artifactId>
|
<artifactId>justauth-spring-boot-starter</artifactId>
|
||||||
<version>1.0.1</version>
|
<version>1.0.2-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -42,6 +44,8 @@ justauth:
|
|||||||
client-id: 10**********6
|
client-id: 10**********6
|
||||||
client-secret: 1f7d08**********5b7**********29e
|
client-secret: 1f7d08**********5b7**********29e
|
||||||
redirect-uri: http://oauth.xkcoding.com/demo/oauth/qq/callback
|
redirect-uri: http://oauth.xkcoding.com/demo/oauth/qq/callback
|
||||||
|
cache:
|
||||||
|
type: default
|
||||||
```
|
```
|
||||||
|
|
||||||
- 然后就开始玩耍吧~
|
- 然后就开始玩耍吧~
|
||||||
@@ -83,134 +87,170 @@ public class TestController {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
- 如果需要自定义 State 的缓存(此处举例 Redis,其余缓存同理)
|
### 2. 缓存配置
|
||||||
|
|
||||||
> 1. 自定义缓存实现 `AuthStateCache` 接口
|
> starter 内置了2种缓存实现,一种是上面的默认实现,另一种是基于 Redis 的缓存实现。
|
||||||
> 2. 将自定义缓存加入 Spring 容器
|
>
|
||||||
|
> 当然了,你也可以自定义实现你自己的缓存。
|
||||||
|
|
||||||
1.添加Redis依赖
|
#### 2.1. 默认缓存实现
|
||||||
|
|
||||||
```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.自定义缓存 `RedisStateCache`
|
```yaml
|
||||||
|
justauth:
|
||||||
|
cache:
|
||||||
|
type: default
|
||||||
|
```
|
||||||
|
|
||||||
```java
|
#### 2.2. Redis 缓存实现
|
||||||
/**
|
|
||||||
* <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 static final long DEF_TIMEOUT = 3 * 60 * 1000;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 存入缓存
|
|
||||||
*
|
|
||||||
* @param key 缓存key
|
|
||||||
* @param value 缓存内容
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void cache(String key, String value) {
|
|
||||||
this.cache(key, value, DEF_TIMEOUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 存入缓存
|
|
||||||
*
|
|
||||||
* @param key 缓存key
|
|
||||||
* @param value 缓存内容
|
|
||||||
* @param timeout 指定缓存过期时间(毫秒)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void cache(String key, String value, long timeout) {
|
|
||||||
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.MILLISECONDS);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取缓存内容
|
|
||||||
*
|
|
||||||
* @param key 缓存key
|
|
||||||
* @return 缓存内容
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public String get(String key) {
|
|
||||||
return redisTemplate.opsForValue().get(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 是否存在key,如果对应key的value值已过期,也返回false
|
|
||||||
*
|
|
||||||
* @param key 缓存key
|
|
||||||
* @return true:存在key,并且value没过期;false:key不存在或者已过期
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean containsKey(String key) {
|
|
||||||
Long expire = redisTemplate.getExpire(key, TimeUnit.MILLISECONDS);
|
|
||||||
if (expire == null) {
|
|
||||||
expire = 0L;
|
|
||||||
}
|
|
||||||
return expire > 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
3.自动装配 `JustAuthConfig`
|
1.添加 Redis 相关依赖
|
||||||
|
|
||||||
```java
|
```xml
|
||||||
/**
|
<dependency>
|
||||||
* <p>
|
<groupId>org.springframework.boot</groupId>
|
||||||
* JustAuth配置类
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
* </p>
|
</dependency>
|
||||||
*
|
|
||||||
* @author yangkai.shen
|
<!-- 对象池,使用redis时必须引入 -->
|
||||||
* @date Created in 2019-08-02 15:08
|
<dependency>
|
||||||
*/
|
<groupId>org.apache.commons</groupId>
|
||||||
@Configuration
|
<artifactId>commons-pool2</artifactId>
|
||||||
public class JustAuthConfig {
|
</dependency>
|
||||||
/**
|
```
|
||||||
* 默认情况下的模板只能支持RedisTemplate<String, String>,也就是只能存入字符串,因此支持序列化
|
|
||||||
*/
|
2.配置文件配置如下内容即可
|
||||||
@Bean
|
|
||||||
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
|
```yaml
|
||||||
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
|
justauth:
|
||||||
template.setKeySerializer(new StringRedisSerializer());
|
cache:
|
||||||
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
|
type: redis
|
||||||
template.setConnectionFactory(redisConnectionFactory);
|
# 缓存前缀,目前只对redis缓存生效,默认 JUSTAUTH::STATE::
|
||||||
return template;
|
prefix: ''
|
||||||
}
|
# 超时时长,目前只对redis缓存生效,默认3分钟
|
||||||
|
timeout: 1h
|
||||||
@Bean
|
spring:
|
||||||
public AuthStateCache authStateCache(RedisTemplate<String,String> redisCacheTemplate) {
|
redis:
|
||||||
return new RedisStateCache(redisCacheTemplate);
|
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没过期;false:key不存在或者已过期
|
||||||
|
*/
|
||||||
|
@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` 配置列表
|
||||||
|
|
||||||
| 属性名 | 类型 | 默认值 | 可选项 | 描述 |
|
| 属性名 | 类型 | 默认值 | 可选项 | 描述 |
|
||||||
| ------------------ | ------------------------------------------------------------ | ------ | ---------- | ----------------- |
|
| ------------------ | ------------------------------------------------------------ | ------ | ---------- | ----------------- |
|
||||||
| `justauth.enabled` | `boolean` | true | true/false | 是否启用 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.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` 配置列表
|
`justauth.type` 配置列表
|
||||||
|
|
||||||
@@ -219,3 +259,37 @@ 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` | `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.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. 私服
|
||||||
|
|
||||||
|
如果想体验快照版本,需要在 `pom.xml` 文件里添加如下配置
|
||||||
|
|
||||||
|
```xml
|
||||||
|
<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>
|
||||||
|
```
|
||||||
42
pom.xml
42
pom.xml
@@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
<groupId>com.xkcoding</groupId>
|
<groupId>com.xkcoding</groupId>
|
||||||
<artifactId>justauth-spring-boot-starter</artifactId>
|
<artifactId>justauth-spring-boot-starter</artifactId>
|
||||||
<version>1.0.1</version>
|
<version>1.0.2-SNAPSHOT</version>
|
||||||
|
|
||||||
<name>justauth-spring-boot-starter</name>
|
<name>justauth-spring-boot-starter</name>
|
||||||
<url>https://github.com/xkcoding/justauth-spring-boot-starter</url>
|
<url>https://github.com/xkcoding/justauth-spring-boot-starter</url>
|
||||||
@@ -76,6 +76,17 @@
|
|||||||
<artifactId>JustAuth</artifactId>
|
<artifactId>JustAuth</artifactId>
|
||||||
<version>${justauth.version}</version>
|
<version>${justauth.version}</version>
|
||||||
</dependency>
|
</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>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||||
@@ -261,6 +272,35 @@
|
|||||||
</snapshotRepository>
|
</snapshotRepository>
|
||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
</profile>
|
</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>
|
</profiles>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -18,13 +18,13 @@
|
|||||||
package com.xkcoding.justauth;
|
package com.xkcoding.justauth;
|
||||||
|
|
||||||
import com.xkcoding.justauth.properties.JustAuthProperties;
|
import com.xkcoding.justauth.properties.JustAuthProperties;
|
||||||
import me.zhyd.oauth.cache.AuthDefaultStateCache;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import me.zhyd.oauth.cache.AuthStateCache;
|
import me.zhyd.oauth.cache.AuthStateCache;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@@ -34,6 +34,7 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
* @author yangkai.shen
|
* @author yangkai.shen
|
||||||
* @date Created in 2019-07-22 10:52
|
* @date Created in 2019-07-22 10:52
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableConfigurationProperties(JustAuthProperties.class)
|
@EnableConfigurationProperties(JustAuthProperties.class)
|
||||||
public class JustAuthAutoConfiguration {
|
public class JustAuthAutoConfiguration {
|
||||||
@@ -44,10 +45,10 @@ public class JustAuthAutoConfiguration {
|
|||||||
return new AuthRequestFactory(properties, authStateCache);
|
return new AuthRequestFactory(properties, authStateCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Configuration
|
||||||
@ConditionalOnMissingBean
|
@Import({JustAuthStateCacheConfiguration.Default.class, JustAuthStateCacheConfiguration.Redis.class, JustAuthStateCacheConfiguration.Custom.class})
|
||||||
public AuthStateCache authStateCache() {
|
protected static class AuthStateCacheAutoConfiguration {
|
||||||
return AuthDefaultStateCache.INSTANCE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
88
src/main/java/com/xkcoding/justauth/cache/RedisStateCache.java
vendored
Normal file
88
src/main/java/com/xkcoding/justauth/cache/RedisStateCache.java
vendored
Normal 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没过期;false:key不存在或者已过期
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean containsKey(String key) {
|
||||||
|
Long expire = redisTemplate.getExpire(cacheProperties.getPrefix() + key, TimeUnit.MILLISECONDS);
|
||||||
|
if (expire == null) {
|
||||||
|
expire = 0L;
|
||||||
|
}
|
||||||
|
return expire > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,6 +22,7 @@ import lombok.Setter;
|
|||||||
import me.zhyd.oauth.config.AuthConfig;
|
import me.zhyd.oauth.config.AuthConfig;
|
||||||
import me.zhyd.oauth.config.AuthSource;
|
import me.zhyd.oauth.config.AuthSource;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.context.properties.NestedConfigurationProperty;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -48,4 +49,10 @@ public class JustAuthProperties {
|
|||||||
*/
|
*/
|
||||||
private Map<AuthSource, AuthConfig> type = new HashMap<>();
|
private Map<AuthSource, AuthConfig> type = new HashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存配置类
|
||||||
|
*/
|
||||||
|
@NestedConfigurationProperty
|
||||||
|
private CacheProperties cache;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xkcoding.justauth.JustAuthAutoConfiguration
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
|
com.xkcoding.justauth.JustAuthAutoConfiguration
|
||||||
|
|||||||
Reference in New Issue
Block a user