refactor: 补正一些缺失方法

This commit is contained in:
click33 2025-03-08 15:22:33 +08:00
parent 850af6c131
commit c3be6304db
10 changed files with 43 additions and 28 deletions

View File

@ -243,18 +243,16 @@ public class SaTerminalInfo implements Serializable {
//
@Override
public String toString() {
return "TokenSign [" +
return "SaTerminalInfo [" +
"index=" + index +
", tokenValue=" + tokenValue +
", deviceType=" + deviceType +
", deviceId=" + deviceId +
", tokenValue='" + tokenValue +
", deviceType='" + deviceType +
", deviceId='" + deviceId +
", extraData=" + extraData +
", createTime=" + createTime +
"]";
']';
}
/*
* Expand in the future:
* deviceName 登录设备端名称一般为浏览器名称

View File

@ -20,7 +20,7 @@
<module>sa-token-demo-dubbo/sa-token-demo-dubbo3-provider</module>
<module>sa-token-demo-dubbo/sa-token-demo-dubbo3-consumer</module>
<module>sa-token-demo-freemarker</module>
<module>sa-token-demo-grpc</module>
<!-- <module>sa-token-demo-grpc</module>-->
<module>sa-token-demo-hutool-timed-cache</module>
<module>sa-token-demo-jwt</module>
<module>sa-token-demo-oauth2/sa-token-demo-oauth2-server</module>

View File

@ -3,8 +3,7 @@ package com.pj.satoken;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.dao.SaTokenDaoOfRedis;
import cn.dev33.satoken.dao.SaTokenDaoOfRedisJson;
import cn.dev33.satoken.dao.SaTokenDaoForRedisx;
import cn.dev33.satoken.solon.integration.SaTokenInterceptor;
import com.pj.util.AjaxJson;
import org.noear.solon.annotation.Bean;
@ -61,7 +60,7 @@ public class SaTokenConfigure {
//如果需要 redis dao加这段代表
@Bean
public SaTokenDao saTokenDaoInit(@Inject("${sa-token-dao.redis}") SaTokenDaoOfRedisJson saTokenDao) {
public SaTokenDao saTokenDaoInit(@Inject("${sa-token-dao.redis}") SaTokenDaoForRedisx saTokenDao) {
return saTokenDao;
}

View File

@ -2,11 +2,13 @@ package com.pj.satoken;
import cn.dev33.satoken.application.ApplicationInfo;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.dao.SaTokenDaoRedisJackson;
import cn.dev33.satoken.dao.SaTokenDaoForRedisTemplate;
import cn.dev33.satoken.json.SaJsonTemplateForJackson;
import cn.dev33.satoken.log.SaLog;
import cn.dev33.satoken.plugin.SaTokenPluginHolder;
import cn.dev33.satoken.spring.SaBeanInject;
import cn.dev33.satoken.spring.SaTokenContextForSpring;
import cn.dev33.satoken.spring.json.SaJsonTemplateForJackson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
/**
@ -19,13 +21,14 @@ public class SaTokenBeanInjection {
public SaTokenBeanInjection(
SaLog log,
SaTokenConfig config,
@Autowired(required = false) SaTokenPluginHolder pluginHolder,
RedisConnectionFactory connectionFactory,
String routePrefix
) {
System.out.println("---------------- 手动注入 Sa-Token 所需要的组件 start ----------------");
// 日志组件配置信息
SaBeanInject inject = new SaBeanInject(log, config);
SaBeanInject inject = new SaBeanInject(log, config, pluginHolder);
// 基于 Spring 的上下文处理器
inject.setSaTokenContext(new SaTokenContextForSpring());
@ -34,9 +37,9 @@ public class SaTokenBeanInjection {
inject.setSaJsonTemplate(new SaJsonTemplateForJackson());
// 基于 Jackson 序列化的 Redis 持久化组件
SaTokenDaoRedisJackson saTokenDaoRedisJackson = new SaTokenDaoRedisJackson();
saTokenDaoRedisJackson.init(connectionFactory);
inject.setSaTokenDao(saTokenDaoRedisJackson);
SaTokenDaoForRedisTemplate saTokenDaoForRedisTemplate = new SaTokenDaoForRedisTemplate();
saTokenDaoForRedisTemplate.init(connectionFactory);
inject.setSaTokenDao(saTokenDaoForRedisTemplate);
// 权限和角色数据
inject.setStpInterface(new StpInterfaceImpl());

View File

@ -51,6 +51,11 @@ public class SaTokenDaoForHutoolTimedCache implements SaTokenDaoByStringFollowOb
return timedCache.get(key, false);
}
@Override
public <T> T getObject(String key, Class<T> classType) {
return (T) getObject(key);
}
@Override
public void setObject(String key, Object object, long timeout) {
if(timeout == 0 || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) {

View File

@ -167,6 +167,11 @@ public class SaTokenCacheDao implements SaTokenDaoBySessionFollowObject {
}
}
@Override
public <T> T getObject(String key, Class<T> classType) {
return (T) getObject(key);
}
@Override
public void setObject(String key, Object object, long timeout) {
if (timeout == 0 || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) {

View File

@ -150,6 +150,11 @@ public class SaTokenDaoRedis implements SaTokenDaoBySessionFollowObject {
}
}
@Override
public <T> T getObject(String key, Class<T> classType) {
return (T) getObject(key);
}
/**
* 写入Object并设定存活时间 (单位: )
*/

View File

@ -107,7 +107,7 @@ public class SaSessionTest {
// 测试token 签名
@Test
public void testTokenSign() {
public void testSaTerminalInfo() {
SaSession session = new SaSession("session-1002");
// 添加 Token 签名
@ -123,7 +123,7 @@ public class SaSessionTest {
session.removeTerminal("xxxx-xxxx-xxxx-xxxx-1");
Assertions.assertEquals(session.getTerminalList().size(), 1);
// 删除一个不存在的则不影响 TokenSign 列表
// 删除一个不存在的则不影响 SaTerminalInfo 列表
session.removeTerminal("xxxx-xxxx-xxxx-xxxx-999");
Assertions.assertEquals(session.getTerminalList().size(), 1);

View File

@ -20,7 +20,7 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* TokenSign 相关测试
* SaTerminalInfo 相关测试
*
* @author click33
* @since 2022-9-4
@ -30,14 +30,14 @@ public class SaTerminalInfoTest {
// 测试
@Test
public void testSaTerminalInfo() {
SaTerminalInfo tokenSign = new SaTerminalInfo();
tokenSign.setDeviceType("PC");
tokenSign.setTokenValue("ttt-value");
SaTerminalInfo terminal = new SaTerminalInfo();
terminal.setDeviceType("PC");
terminal.setTokenValue("ttt-value");
Assertions.assertEquals(tokenSign.getDeviceType(), "PC");
Assertions.assertEquals(tokenSign.getTokenValue(), "ttt-value");
Assertions.assertEquals(terminal.getDeviceType(), "PC");
Assertions.assertEquals(terminal.getTokenValue(), "ttt-value");
Assertions.assertNotNull(tokenSign.toString());
Assertions.assertNotNull(terminal.toString());
}
}

View File

@ -114,8 +114,8 @@ public class ManyLoginTest {
Assertions.assertEquals(dao.get("satoken:login:token:" + token1), "-4");
// Account-Session里的 token1 签名会被移除
List<SaTerminalInfo> tokenSignList = StpUtil.getSessionByLoginId(10001).getTerminalList();
for (SaTerminalInfo terminal : tokenSignList) {
List<SaTerminalInfo> terminalList = StpUtil.getSessionByLoginId(10001).getTerminalList();
for (SaTerminalInfo terminal : terminalList) {
Assertions.assertNotEquals(terminal.getTokenValue(), token1);
}
}