mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-09-18 17:48:03 +08:00
新增插件:Sa-Token独立Redis
This commit is contained in:
@@ -25,4 +25,13 @@
|
||||
<module>sa-token-temp-jwt</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
<!-- spring-boot-configuration -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<version>2.0.0.RELEASE</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
12
sa-token-plugin/sa-token-alone-redis/.gitignore
vendored
Normal file
12
sa-token-plugin/sa-token-alone-redis/.gitignore
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
target/
|
||||
|
||||
node_modules/
|
||||
bin/
|
||||
.settings/
|
||||
unpackage/
|
||||
.classpath
|
||||
.project
|
||||
|
||||
.factorypath
|
||||
|
||||
.idea/
|
41
sa-token-plugin/sa-token-alone-redis/pom.xml
Normal file
41
sa-token-plugin/sa-token-alone-redis/pom.xml
Normal file
@@ -0,0 +1,41 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-plugin</artifactId>
|
||||
<version>1.20.0</version>
|
||||
</parent>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>sa-token-alone-redis</name>
|
||||
<artifactId>sa-token-alone-redis</artifactId>
|
||||
<description>sa-token-alone-redis</description>
|
||||
|
||||
<dependencies>
|
||||
<!-- Sa-Token Redis Dependency -->
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-dao-redis</artifactId>
|
||||
<version>${sa-token-version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-dao-redis-jackson</artifactId>
|
||||
<version>${sa-token-version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!-- redis pool -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-pool2</artifactId>
|
||||
<version>2.5.0</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@@ -0,0 +1,123 @@
|
||||
package cn.dev33.satoken.dao.alone;
|
||||
|
||||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.data.redis.connection.RedisPassword;
|
||||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
|
||||
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import cn.dev33.satoken.dao.SaTokenDaoDefaultImpl;
|
||||
import cn.dev33.satoken.dao.SaTokenDaoRedis;
|
||||
import cn.dev33.satoken.dao.SaTokenDaoRedisJackson;
|
||||
|
||||
/**
|
||||
* 为 SaTokenDao 单独设置Redis连接信息
|
||||
* @author kong
|
||||
*/
|
||||
@Configuration
|
||||
public class SaAloneRedisInject implements EnvironmentAware{
|
||||
|
||||
/**
|
||||
* 配置信息的前缀
|
||||
*/
|
||||
public static final String ALONE_PREFIX = "spring.sa-token.alone-redis";
|
||||
|
||||
/**
|
||||
* Sa-Token 持久层接口
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public SaTokenDao saTokenDao;
|
||||
|
||||
/**
|
||||
* 开始注入
|
||||
*/
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
|
||||
// 如果为空或者默认实现,则不进行任何操作
|
||||
if(saTokenDao == null || saTokenDao instanceof SaTokenDaoDefaultImpl) {
|
||||
return;
|
||||
}
|
||||
|
||||
// ------------------- 开始注入
|
||||
|
||||
// 获取cfg对象
|
||||
RedisProperties cfg = Binder.get(environment).bind(ALONE_PREFIX, RedisProperties.class).get();
|
||||
|
||||
// 1. Redis配置
|
||||
RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
|
||||
redisConfig.setHostName(cfg.getHost());
|
||||
redisConfig.setPort(cfg.getPort());
|
||||
redisConfig.setDatabase(cfg.getDatabase());
|
||||
redisConfig.setPassword(RedisPassword.of(cfg.getPassword()));
|
||||
|
||||
// 2. 连接池配置
|
||||
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
|
||||
// pool配置
|
||||
Lettuce lettuce = cfg.getLettuce();
|
||||
if(lettuce.getPool() != null) {
|
||||
RedisProperties.Pool pool = cfg.getLettuce().getPool();
|
||||
// 连接池最大连接数
|
||||
poolConfig.setMaxTotal(pool.getMaxActive());
|
||||
// 连接池中的最大空闲连接
|
||||
poolConfig.setMaxIdle(pool.getMaxIdle());
|
||||
// 连接池中的最小空闲连接
|
||||
poolConfig.setMinIdle(pool.getMinIdle());
|
||||
// 连接池最大阻塞等待时间(使用负值表示没有限制)
|
||||
poolConfig.setMaxWaitMillis(pool.getMaxWait().toMillis());
|
||||
}
|
||||
LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder();
|
||||
// timeout
|
||||
if(cfg.getTimeout() != null) {
|
||||
builder.commandTimeout(cfg.getTimeout());
|
||||
}
|
||||
// shutdownTimeout
|
||||
if(lettuce.getShutdownTimeout() != null) {
|
||||
builder.shutdownTimeout(lettuce.getShutdownTimeout());
|
||||
}
|
||||
// 创建Factory对象
|
||||
LettuceClientConfiguration clientConfig = builder.poolConfig(poolConfig).build();
|
||||
LettuceConnectionFactory factory = new LettuceConnectionFactory(redisConfig, clientConfig);
|
||||
factory.afterPropertiesSet();
|
||||
|
||||
// 3. 开始初始化 SaTokenDao
|
||||
// 如果是SaTokenDaoRedis
|
||||
try {
|
||||
Class.forName("cn.dev33.satoken.dao.SaTokenDaoRedis");
|
||||
SaTokenDaoRedis dao = (SaTokenDaoRedis)saTokenDao;
|
||||
dao.isInit = false;
|
||||
dao.init(factory);
|
||||
return;
|
||||
} catch (ClassNotFoundException e) {
|
||||
}
|
||||
// 如果是SaTokenDaoRedisJackson
|
||||
try {
|
||||
Class.forName("cn.dev33.satoken.dao.SaTokenDaoRedisJackson");
|
||||
SaTokenDaoRedisJackson dao = (SaTokenDaoRedisJackson)saTokenDao;
|
||||
dao.isInit = false;
|
||||
dao.init(factory);
|
||||
return;
|
||||
} catch (ClassNotFoundException e) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 骗过编辑器,增加配置文件代码提示
|
||||
* @return 配置对象
|
||||
*/
|
||||
@ConfigurationProperties(prefix = ALONE_PREFIX)
|
||||
public RedisProperties getSaAloneRedisConfig() {
|
||||
return new RedisProperties();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.dev33.satoken.dao.alone.SaAloneRedisInject
|
@@ -36,15 +36,21 @@ public class SaTokenDaoRedisJackson implements SaTokenDao {
|
||||
/**
|
||||
* String专用
|
||||
*/
|
||||
@Autowired
|
||||
public StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
/**
|
||||
* Object专用
|
||||
*/
|
||||
public RedisTemplate<String, Object> objectRedisTemplate;
|
||||
|
||||
/**
|
||||
* 标记:是否已初始化成功
|
||||
*/
|
||||
public boolean isInit;
|
||||
|
||||
@Autowired
|
||||
public void setObjectRedisTemplate(RedisConnectionFactory connectionFactory) {
|
||||
public void init(RedisConnectionFactory connectionFactory) {
|
||||
|
||||
// 指定相应的序列化方案
|
||||
StringRedisSerializer keySerializer = new StringRedisSerializer();
|
||||
GenericJackson2JsonRedisSerializer valueSerializer = new GenericJackson2JsonRedisSerializer();
|
||||
@@ -58,6 +64,10 @@ public class SaTokenDaoRedisJackson implements SaTokenDao {
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
// 构建StringRedisTemplate
|
||||
StringRedisTemplate stringTemplate = new StringRedisTemplate();
|
||||
stringTemplate.setConnectionFactory(connectionFactory);
|
||||
stringTemplate.afterPropertiesSet();
|
||||
// 构建RedisTemplate
|
||||
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
|
||||
template.setConnectionFactory(connectionFactory);
|
||||
@@ -66,8 +76,12 @@ public class SaTokenDaoRedisJackson implements SaTokenDao {
|
||||
template.setValueSerializer(valueSerializer);
|
||||
template.setHashValueSerializer(valueSerializer);
|
||||
template.afterPropertiesSet();
|
||||
if(this.objectRedisTemplate == null) {
|
||||
|
||||
// 开始初始化相关组件
|
||||
if(this.isInit == false) {
|
||||
this.stringRedisTemplate = stringTemplate;
|
||||
this.objectRedisTemplate = template;
|
||||
this.isInit = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -27,18 +27,27 @@ public class SaTokenDaoRedis implements SaTokenDao {
|
||||
/**
|
||||
* String专用
|
||||
*/
|
||||
@Autowired
|
||||
public StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
/**
|
||||
* Objecy专用
|
||||
*/
|
||||
public RedisTemplate<String, Object> objectRedisTemplate;
|
||||
|
||||
/**
|
||||
* 标记:是否已初始化成功
|
||||
*/
|
||||
public boolean isInit;
|
||||
|
||||
@Autowired
|
||||
public void setObjectRedisTemplate(RedisConnectionFactory connectionFactory) {
|
||||
public void init(RedisConnectionFactory connectionFactory) {
|
||||
// 指定相应的序列化方案
|
||||
StringRedisSerializer keySerializer = new StringRedisSerializer();
|
||||
JdkSerializationRedisSerializer valueSerializer = new JdkSerializationRedisSerializer();
|
||||
// 构建StringRedisTemplate
|
||||
StringRedisTemplate stringTemplate = new StringRedisTemplate();
|
||||
stringTemplate.setConnectionFactory(connectionFactory);
|
||||
stringTemplate.afterPropertiesSet();
|
||||
// 构建RedisTemplate
|
||||
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
|
||||
template.setConnectionFactory(connectionFactory);
|
||||
@@ -47,8 +56,12 @@ public class SaTokenDaoRedis implements SaTokenDao {
|
||||
template.setValueSerializer(valueSerializer);
|
||||
template.setHashValueSerializer(valueSerializer);
|
||||
template.afterPropertiesSet();
|
||||
if(this.objectRedisTemplate == null) {
|
||||
|
||||
// 开始初始化相关组件
|
||||
if(this.isInit == false) {
|
||||
this.stringRedisTemplate = stringTemplate;
|
||||
this.objectRedisTemplate = template;
|
||||
this.isInit = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -28,13 +28,6 @@
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
<version>2.0.0.RELEASE</version>
|
||||
</dependency>
|
||||
<!-- spring-boot-configuration -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<version>2.0.0.RELEASE</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
Reference in New Issue
Block a user