mirror of
https://gitee.com/dromara/sa-token.git
synced 2026-05-14 12:52:08 +08:00
简化 redis 和 rpc 相关集成包的包名
This commit is contained in:
36
sa-token-plugin/sa-token-redisx/README.md
Normal file
36
sa-token-plugin/sa-token-redisx/README.md
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
sa-token-redisx 是中立的扩展。可任何应用开发框架下使用(springboot, solon, jfinal 等..)
|
||||
|
||||
### 使用示例
|
||||
|
||||
#### 1.配置
|
||||
|
||||
```yaml
|
||||
sa-token: #名字可以随意取
|
||||
redis:
|
||||
server: "localhost:6379"
|
||||
password: 123456
|
||||
db: 1
|
||||
# serializer: "org.noear.redisx.utils.SerializerJson" #指定自定义序列化实现(默认为 SerializerDefault)
|
||||
```
|
||||
|
||||
#### 2.代码
|
||||
|
||||
**注入风格**
|
||||
|
||||
```java
|
||||
@Configuration
|
||||
public class Config {
|
||||
@Bean
|
||||
public SaTokenDao saTokenDaoInit(@Inject("${sa-token.redis}") SaTokenDaoOfRedis saTokenDao) {
|
||||
return saTokenDao;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**手动风格**
|
||||
|
||||
```java
|
||||
SaTokenDaoOfRedis saTokenDao = new SaTokenDaoOfRedis(props);
|
||||
SaManager.setSaTokenDao(saTokenDao);
|
||||
```
|
||||
46
sa-token-plugin/sa-token-redisx/pom.xml
Normal file
46
sa-token-plugin/sa-token-redisx/pom.xml
Normal file
@@ -0,0 +1,46 @@
|
||||
<?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>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>sa-token-redisx</name>
|
||||
<artifactId>sa-token-redisx</artifactId>
|
||||
<description>sa-token integrate redis</description>
|
||||
|
||||
<dependencies>
|
||||
<!-- sa-token-spring-boot-starter -->
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.noear</groupId>
|
||||
<artifactId>redisx</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.noear</groupId>
|
||||
<artifactId>snack3</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.noear</groupId>
|
||||
<artifactId>solon-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,73 @@
|
||||
package cn.dev33.satoken.dao;
|
||||
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
import org.noear.snack.ONode;
|
||||
|
||||
/**
|
||||
* Snack3 定制版 SaSession,重写类型转换API
|
||||
*
|
||||
* @author noear
|
||||
* @since 1.12
|
||||
*/
|
||||
public class SaSessionForJson extends SaSession {
|
||||
|
||||
private static final long serialVersionUID = -7600983549653130681L;
|
||||
|
||||
public SaSessionForJson() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建一个 SaSession 对象
|
||||
* @param id Session 的 id
|
||||
*/
|
||||
public SaSessionForJson(String id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取值 (指定转换类型)
|
||||
* @param <T> 泛型
|
||||
* @param key key
|
||||
* @param cs 指定转换类型
|
||||
* @return 值
|
||||
*/
|
||||
@Override
|
||||
public <T> T getModel(String key, Class<T> cs) {
|
||||
if(SaFoxUtil.isBasicType(cs)) {
|
||||
return SaFoxUtil.getValueByType(get(key), cs);
|
||||
}
|
||||
return ONode.deserialize(getString(key), cs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取值 (指定转换类型, 并指定值为Null时返回的默认值)
|
||||
* @param <T> 泛型
|
||||
* @param key key
|
||||
* @param cs 指定转换类型
|
||||
* @param defaultValue 值为Null时返回的默认值
|
||||
* @return 值
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getModel(String key, Class<T> cs, Object defaultValue) {
|
||||
Object value = get(key);
|
||||
if(valueIsNull(value)) {
|
||||
return (T)defaultValue;
|
||||
}
|
||||
if(SaFoxUtil.isBasicType(cs)) {
|
||||
return SaFoxUtil.getValueByType(get(key), cs);
|
||||
}
|
||||
return ONode.deserialize(getString(key), cs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 忽略 timeout 字段的序列化
|
||||
*/
|
||||
@Override
|
||||
public long getTimeout() {
|
||||
return super.getTimeout();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cn.dev33.satoken.dao;
|
||||
|
||||
import org.noear.redisx.RedisClient;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* SaTokenDao 的 redis 适配
|
||||
*
|
||||
* @author noear
|
||||
* @since 1.6
|
||||
*/
|
||||
public class SaTokenDaoOfRedis extends SaTokenDaoOfRedisBase64 {
|
||||
|
||||
public SaTokenDaoOfRedis(Properties props) {
|
||||
super(props);
|
||||
}
|
||||
|
||||
public SaTokenDaoOfRedis(RedisClient redisClient) {
|
||||
super(redisClient);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package cn.dev33.satoken.dao;
|
||||
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
import org.noear.redisx.RedisClient;
|
||||
import org.noear.redisx.plus.RedisBucket;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* SaTokenDao 的 redis 适配(可以完全精准还原所有序列化类型)
|
||||
*
|
||||
* @author noear
|
||||
* @since 1.6
|
||||
*/
|
||||
public class SaTokenDaoOfRedisBase64 implements SaTokenDao {
|
||||
private final RedisBucket redisBucket;
|
||||
|
||||
public SaTokenDaoOfRedisBase64(Properties props) {
|
||||
this(new RedisClient(props));
|
||||
}
|
||||
|
||||
public SaTokenDaoOfRedisBase64(RedisClient redisClient) {
|
||||
redisBucket = redisClient.getBucket();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取Value,如无返空
|
||||
*/
|
||||
@Override
|
||||
public String get(String key) {
|
||||
return redisBucket.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入Value,并设定存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public void set(String key, String value, long timeout) {
|
||||
if (timeout > 0 || timeout == SaTokenDao.NEVER_EXPIRE) {
|
||||
redisBucket.store(key, value, (int) timeout);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改指定key-value键值对 (过期时间不变)
|
||||
*/
|
||||
@Override
|
||||
public void update(String key, String value) {
|
||||
long expire = getTimeout(key);
|
||||
this.set(key, value, expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除Value
|
||||
*/
|
||||
@Override
|
||||
public void delete(String key) {
|
||||
redisBucket.remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Value的剩余存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public long getTimeout(String key) {
|
||||
return redisBucket.ttl(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改Value的剩余存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public void updateTimeout(String key, long timeout) {
|
||||
if (redisBucket.exists(key)) {
|
||||
redisBucket.delay(key, (int) timeout);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取Object,如无返空
|
||||
*/
|
||||
@Override
|
||||
public Object getObject(String key) {
|
||||
return redisBucket.getAndDeserialize(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入Object,并设定存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public void setObject(String key, Object object, long timeout) {
|
||||
if (timeout > 0 || timeout == SaTokenDao.NEVER_EXPIRE) {
|
||||
redisBucket.storeAndSerialize(key, object, (int) timeout);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新Object (过期时间不变)
|
||||
*/
|
||||
@Override
|
||||
public void updateObject(String key, Object object) {
|
||||
long expire = getObjectTimeout(key);
|
||||
this.setObject(key, object, expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除Object
|
||||
*/
|
||||
@Override
|
||||
public void deleteObject(String key) {
|
||||
redisBucket.remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Object的剩余存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public long getObjectTimeout(String key) {
|
||||
return redisBucket.ttl(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改Object的剩余存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public void updateObjectTimeout(String key, long timeout) {
|
||||
if (redisBucket.exists(key)) {
|
||||
redisBucket.delay(key, (int) timeout);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 搜索数据
|
||||
*/
|
||||
@Override
|
||||
public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) {
|
||||
Set<String> keys = redisBucket.keys(prefix + "*" + keyword + "*");
|
||||
List<String> list = new ArrayList<String>(keys);
|
||||
return SaFoxUtil.searchList(list, start, size, sortType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package cn.dev33.satoken.dao;
|
||||
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
import cn.dev33.satoken.strategy.SaStrategy;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
import org.noear.redisx.RedisClient;
|
||||
import org.noear.redisx.plus.RedisBucket;
|
||||
import org.noear.snack.ONode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* SaTokenDao 的 redis 适配(基于json序列化,不能完全精准还原所有类型)
|
||||
*
|
||||
* @author noear
|
||||
* @since 1.6
|
||||
*/
|
||||
public class SaTokenDaoOfRedisJson implements SaTokenDao {
|
||||
private final RedisBucket redisBucket;
|
||||
|
||||
public SaTokenDaoOfRedisJson(Properties props) {
|
||||
this(new RedisClient(props));
|
||||
}
|
||||
|
||||
public SaTokenDaoOfRedisJson(RedisClient redisClient) {
|
||||
redisBucket = redisClient.getBucket();
|
||||
|
||||
// 重写 SaSession 生成策略
|
||||
SaStrategy.me.createSession = (sessionId) -> new SaSessionForJson(sessionId);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public SaSession getSession(String sessionId) {
|
||||
Object obj = getObject(sessionId);
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
return ONode.deserialize(obj.toString(), SaSessionForJson.class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取Value,如无返空
|
||||
*/
|
||||
@Override
|
||||
public String get(String key) {
|
||||
return redisBucket.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入Value,并设定存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public void set(String key, String value, long timeout) {
|
||||
if (timeout > 0 || timeout == SaTokenDao.NEVER_EXPIRE) {
|
||||
redisBucket.store(key, value, (int) timeout);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改指定key-value键值对 (过期时间不变)
|
||||
*/
|
||||
@Override
|
||||
public void update(String key, String value) {
|
||||
long expire = getTimeout(key);
|
||||
this.set(key, value, expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除Value
|
||||
*/
|
||||
@Override
|
||||
public void delete(String key) {
|
||||
redisBucket.remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Value的剩余存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public long getTimeout(String key) {
|
||||
return redisBucket.ttl(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改Value的剩余存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public void updateTimeout(String key, long timeout) {
|
||||
if (redisBucket.exists(key)) {
|
||||
redisBucket.delay(key, (int) timeout);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取Object,如无返空
|
||||
*/
|
||||
@Override
|
||||
public Object getObject(String key) {
|
||||
return get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入Object,并设定存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public void setObject(String key, Object object, long timeout) {
|
||||
if (timeout > 0 || timeout == SaTokenDao.NEVER_EXPIRE) {
|
||||
String value = ONode.serialize(object);
|
||||
set(key, value, timeout);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新Object (过期时间不变)
|
||||
*/
|
||||
@Override
|
||||
public void updateObject(String key, Object object) {
|
||||
long expire = getObjectTimeout(key);
|
||||
this.setObject(key, object, expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除Object
|
||||
*/
|
||||
@Override
|
||||
public void deleteObject(String key) {
|
||||
redisBucket.remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Object的剩余存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public long getObjectTimeout(String key) {
|
||||
return redisBucket.ttl(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改Object的剩余存活时间 (单位: 秒)
|
||||
*/
|
||||
@Override
|
||||
public void updateObjectTimeout(String key, long timeout) {
|
||||
if (redisBucket.exists(key)) {
|
||||
redisBucket.delay(key, (int) timeout);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 搜索数据
|
||||
*/
|
||||
@Override
|
||||
public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) {
|
||||
Set<String> keys = redisBucket.keys(prefix + "*" + keyword + "*");
|
||||
List<String> list = new ArrayList<String>(keys);
|
||||
return SaFoxUtil.searchList(list, start, size, sortType);
|
||||
}
|
||||
}
|
||||
12
sa-token-plugin/sa-token-redisx/src/test/java/demo/App.java
Normal file
12
sa-token-plugin/sa-token-redisx/src/test/java/demo/App.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package demo;
|
||||
|
||||
import org.noear.solon.Solon;
|
||||
|
||||
/**
|
||||
* @author noear 2022/3/30 created
|
||||
*/
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
Solon.start(App.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package demo;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import cn.dev33.satoken.dao.SaTokenDaoOfRedis;
|
||||
import org.noear.solon.annotation.Bean;
|
||||
import org.noear.solon.annotation.Configuration;
|
||||
import org.noear.solon.annotation.Inject;
|
||||
|
||||
/**
|
||||
* @author noear 2022/3/30 created
|
||||
*/
|
||||
@Configuration
|
||||
public class Config {
|
||||
@Bean
|
||||
public void saTokenDaoInit(@Inject("${sa-token.redis}") SaTokenDaoOfRedis saTokenDao) {
|
||||
//手动操作,可适用于任何框架
|
||||
SaManager.setSaTokenDao(saTokenDao);
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public SaTokenDao saTokenDaoInit2(@Inject("${sa-token.redis}") SaTokenDaoOfRedis saTokenDao) {
|
||||
//Solon 项目,可用此案
|
||||
return saTokenDao;
|
||||
}
|
||||
}
|
||||
10
sa-token-plugin/sa-token-redisx/src/test/resources/app.yml
Normal file
10
sa-token-plugin/sa-token-redisx/src/test/resources/app.yml
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
|
||||
|
||||
|
||||
sa-token: #名字可以随意取
|
||||
redis:
|
||||
server: "localhost:6379"
|
||||
password: 123456
|
||||
db: 1
|
||||
maxTotal: 200
|
||||
Reference in New Issue
Block a user