1
0
mirror of synced 2025-12-09 23:42:38 +08:00

集成 redis 缓存配置,用户可以通过配置使用缓存,不需要自行实现

This commit is contained in:
Yangkai.Shen
2019-08-31 11:04:25 +08:00
parent 219f390284
commit 30e7f01a78
7 changed files with 259 additions and 4 deletions

View File

@@ -17,14 +17,19 @@
package com.xkcoding.justauth;
import com.xkcoding.justauth.cache.RedisStateCache;
import com.xkcoding.justauth.properties.CacheProperties;
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.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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.data.redis.core.RedisTemplate;
/**
* <p>
@@ -34,9 +39,12 @@ 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 {
@Autowired(required = false)
private RedisTemplate<String, String> justAuthRedisCacheTemplate;
@Bean
@ConditionalOnProperty(prefix = "justauth", value = "enabled", havingValue = "true", matchIfMissing = true)
@@ -46,8 +54,18 @@ public class JustAuthAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public AuthStateCache authStateCache() {
return AuthDefaultStateCache.INSTANCE;
public AuthStateCache authStateCache(JustAuthProperties justAuthProperties) {
if (CacheProperties.CacheType.REDIS == justAuthProperties.getCache().getType()) {
log.debug("JustAuth 使用 Redis 缓存存储 state 数据");
assert justAuthRedisCacheTemplate != null;
return new RedisStateCache(justAuthRedisCacheTemplate, justAuthProperties.getCache().getTimeout());
} else if (CacheProperties.CacheType.CUSTOM == justAuthProperties.getCache().getType()) {
log.debug("JustAuth 使用 自定义缓存存储 state 数据,请自行实现 me.zhyd.oauth.cache.AuthStateCache");
return null;
} else {
log.debug("JustAuth 使用 默认缓存存储 state 数据");
return AuthDefaultStateCache.INSTANCE;
}
}
}

View File

@@ -0,0 +1,60 @@
/*
* 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 lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import java.io.Serializable;
/**
* <p>
* Redis缓存配置类
* </p>
*
* @author yangkai.shen
* @date Created in 2019/8/31 10:26
*/
@Configuration
@ConditionalOnBean(RedisConnectionFactory.class)
@AutoConfigureBefore(RedisAutoConfiguration.class)
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class JustAuthRedisCacheConfig {
/**
* 默认情况下的模板只能支持RedisTemplate<String, String>,也就是只能存入字符串,因此支持序列化
*/
@Bean
public RedisTemplate<String, Serializable> justAuthRedisCacheTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}

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 lombok.RequiredArgsConstructor;
import me.zhyd.oauth.cache.AuthStateCache;
import org.springframework.data.redis.core.RedisTemplate;
import java.time.Duration;
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 Duration timeout;
/**
* 存入缓存
*
* @param key 缓存key
* @param value 缓存内容
*/
@Override
public void cache(String key, String value) {
this.cache(key, value, timeout.toMillis());
}
/**
* 存入缓存
*
* @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没过期falsekey不存在或者已过期
*/
@Override
public boolean containsKey(String key) {
Long expire = redisTemplate.getExpire(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;
/**
* 缓存前缀,只在配置外部缓存时生效
*/
private String prefix = "";
/**
* 超时时长默认3分钟
*/
private Duration timeout = Duration.ofMinutes(3);
/**
* 缓存类型
*/
@Getter
@ToString
public enum CacheType {
/**
* 使用JustAuth内置的缓存
*/
DEFAULT,
/**
* 使用Redis缓存
*/
REDIS,
/**
* 自定义缓存
*/
CUSTOM;
}
}

View File

@@ -48,4 +48,9 @@ public class JustAuthProperties {
*/
private Map<AuthSource, AuthConfig> type = new HashMap<>();
/**
* 缓存配置类
*/
private CacheProperties cache;
}

View File

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