Browse Source

⬆️ 升级 justauth-spring-boot-starter 版本为 1.1.0

pull/1/head
Yangkai.Shen 5 years ago
parent
commit
9701900061
3 changed files with 1 additions and 110 deletions
  1. +1
    -1
      spring-boot-demo-social/pom.xml
  2. +0
    -39
      spring-boot-demo-social/src/main/java/com/xkcoding/social/config/justauth/JustAuthConfig.java
  3. +0
    -70
      spring-boot-demo-social/src/main/java/com/xkcoding/social/config/justauth/JustAuthRedisStateCache.java

+ 1
- 1
spring-boot-demo-social/pom.xml View File

@@ -20,7 +20,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<justauth-spring-boot.version>1.0.0</justauth-spring-boot.version>
<justauth-spring-boot.version>1.1.0</justauth-spring-boot.version>
</properties>

<dependencies>


+ 0
- 39
spring-boot-demo-social/src/main/java/com/xkcoding/social/config/justauth/JustAuthConfig.java View File

@@ -1,39 +0,0 @@
package com.xkcoding.social.config.justauth;

import me.zhyd.oauth.cache.AuthStateCache;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

/**
* <p>
* JustAuth自动装配
* </p>
*
* @author yangkai.shen
* @date Created in 2019-08-09 14:21
*/
@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 JustAuthRedisStateCache(redisCacheTemplate);
}
}

+ 0
- 70
spring-boot-demo-social/src/main/java/com/xkcoding/social/config/justauth/JustAuthRedisStateCache.java View File

@@ -1,70 +0,0 @@
package com.xkcoding.social.config.justauth;

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-09 14:22
*/
@RequiredArgsConstructor
public class JustAuthRedisStateCache 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;
}
}

Loading…
Cancel
Save