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

🎨 优化自动装配

This commit is contained in:
Yangkai.Shen
2019-08-31 12:34:34 +08:00
parent 3f5989c917
commit 12369820e5
4 changed files with 111 additions and 84 deletions

View File

@@ -17,19 +17,14 @@
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;
import org.springframework.context.annotation.Import;
/**
* <p>
@@ -43,8 +38,6 @@ import org.springframework.data.redis.core.RedisTemplate;
@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)
@@ -52,20 +45,10 @@ public class JustAuthAutoConfiguration {
return new AuthRequestFactory(properties, authStateCache);
}
@Bean
@ConditionalOnMissingBean
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());
} 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;
}
@Configuration
@Import({JustAuthStateCacheConfiguration.Default.class, JustAuthStateCacheConfiguration.Redis.class, JustAuthStateCacheConfiguration.Custom.class})
protected static class AuthStateCacheAutoConfiguration {
}
}

View File

@@ -1,60 +0,0 @@
/*
* 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,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

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