📝 更新 README.md
This commit is contained in:
300
README.md
300
README.md
@@ -22,13 +22,15 @@ https://github.com/xkcoding/justauth-spring-boot-starter-demo
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 1. 基础配置
|
||||
|
||||
- 引用依赖
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>com.xkcoding</groupId>
|
||||
<artifactId>justauth-spring-boot-starter</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>1.0.2-SNAPSHOT</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
@@ -42,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
|
||||
```
|
||||
|
||||
- 然后就开始玩耍吧~
|
||||
@@ -83,134 +87,170 @@ public class TestController {
|
||||
}
|
||||
```
|
||||
|
||||
- 如果需要自定义 State 的缓存(此处举例 Redis,其余缓存同理)
|
||||
### 2. 缓存配置
|
||||
|
||||
> 1. 自定义缓存实现 `AuthStateCache` 接口
|
||||
> 2. 将自定义缓存加入 Spring 容器
|
||||
> starter 内置了2种缓存实现,一种是上面的默认实现,另一种是基于 Redis 的缓存实现。
|
||||
>
|
||||
> 当然了,你也可以自定义实现你自己的缓存。
|
||||
|
||||
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
|
||||
/**
|
||||
* <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;
|
||||
}
|
||||
}
|
||||
```
|
||||
#### 2.2. Redis 缓存实现
|
||||
|
||||
3.自动装配 `JustAuthConfig`
|
||||
1.添加 Redis 相关依赖
|
||||
|
||||
```java
|
||||
/**
|
||||
* <p>
|
||||
* JustAuth配置类
|
||||
* </p>
|
||||
*
|
||||
* @author yangkai.shen
|
||||
* @date Created in 2019-08-02 15:08
|
||||
*/
|
||||
@Configuration
|
||||
public class JustAuthConfig {
|
||||
/**
|
||||
* 默认情况下的模板只能支持RedisTemplate<String, String>,也就是只能存入字符串,因此支持序列化
|
||||
*/
|
||||
@Bean
|
||||
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
|
||||
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
|
||||
template.setConnectionFactory(redisConnectionFactory);
|
||||
return template;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AuthStateCache authStateCache(RedisTemplate<String,String> redisCacheTemplate) {
|
||||
return new RedisStateCache(redisCacheTemplate);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
```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没过期;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.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` 配置列表
|
||||
|
||||
@@ -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.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>
|
||||
```
|
||||
Reference in New Issue
Block a user