From 2e82fc0592a4e3b74ef3b8f4a9c0e8f407de64c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E7=97=95?= Date: Fri, 22 Apr 2022 14:47:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Djboot=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=8B=AC=E7=AB=8BRedis=E5=BA=93=E6=97=B6=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E9=94=99=E8=AF=AF=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/dev33/satoken/jboot/SaRedisCache.java | 10 +++---- .../dev33/satoken/jboot/SaTokenCacheDao.java | 27 +++++++++++++++++-- .../cn/dev33/satoken/jboot/test/AppRun.java | 20 +++++++++----- .../src/test/resources/jboot.properties | 7 +++-- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/sa-token-starter/sa-token-jboot-plugin/src/main/java/cn/dev33/satoken/jboot/SaRedisCache.java b/sa-token-starter/sa-token-jboot-plugin/src/main/java/cn/dev33/satoken/jboot/SaRedisCache.java index 982e6158..2b0df356 100644 --- a/sa-token-starter/sa-token-jboot-plugin/src/main/java/cn/dev33/satoken/jboot/SaRedisCache.java +++ b/sa-token-starter/sa-token-jboot-plugin/src/main/java/cn/dev33/satoken/jboot/SaRedisCache.java @@ -1,7 +1,6 @@ package cn.dev33.satoken.jboot; import com.jfinal.plugin.ehcache.IDataLoader; -import io.jboot.Jboot; import io.jboot.components.cache.JbootCache; import io.jboot.components.cache.JbootCacheConfig; import io.jboot.core.spi.JbootSpi; @@ -22,13 +21,11 @@ import java.util.List; public class SaRedisCache implements JbootCache { protected JbootRedisConfig config; protected JedisPool jedisPool; - protected JbootCacheConfig cacheConfig; private ThreadLocal CACHE_NAME_PREFIX_TL = new ThreadLocal<>(); - public SaRedisCache(JbootCacheConfig cacheConfig) { - this.cacheConfig = cacheConfig; + public SaRedisCache(JbootRedisConfig config) { + this.config = config; - config = Jboot.config(JbootRedisConfig.class); String host = config.getHost(); Integer port = config.getPort(); Integer timeout = config.getTimeout(); @@ -111,7 +108,7 @@ public class SaRedisCache implements JbootCache { @Override public JbootCacheConfig getConfig() { - return cacheConfig; + return null; } @Override @@ -238,6 +235,7 @@ public class SaRedisCache implements JbootCache { } } + public void returnResource(Jedis jedis) { if (jedis != null) { jedis.close(); diff --git a/sa-token-starter/sa-token-jboot-plugin/src/main/java/cn/dev33/satoken/jboot/SaTokenCacheDao.java b/sa-token-starter/sa-token-jboot-plugin/src/main/java/cn/dev33/satoken/jboot/SaTokenCacheDao.java index 3baad822..502f11ad 100644 --- a/sa-token-starter/sa-token-jboot-plugin/src/main/java/cn/dev33/satoken/jboot/SaTokenCacheDao.java +++ b/sa-token-starter/sa-token-jboot-plugin/src/main/java/cn/dev33/satoken/jboot/SaTokenCacheDao.java @@ -4,12 +4,16 @@ import cn.dev33.satoken.dao.SaTokenDao; import cn.dev33.satoken.session.SaSession; import cn.dev33.satoken.util.SaFoxUtil; import io.jboot.components.serializer.JbootSerializer; -import io.jboot.utils.CacheUtil; +import io.jboot.exception.JbootIllegalConfigException; +import io.jboot.support.redis.JbootRedisConfig; +import io.jboot.utils.ConfigUtil; import redis.clients.jedis.Jedis; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; /** * 使用Jboot的缓存方法存取Token数据 @@ -19,12 +23,31 @@ public class SaTokenCacheDao implements SaTokenDao { protected SaRedisCache saRedisCache; protected JbootSerializer serializer; + private Map saRedisMap = new ConcurrentHashMap(); /** * 调用的Cache名称 * @param cacheName 使用的缓存配置名,默认为 default */ public SaTokenCacheDao(String cacheName) { - saRedisCache = (SaRedisCache) CacheUtil.use(cacheName); + SaRedisCache saCache = (SaRedisCache) this.saRedisMap.get(cacheName); + if (saCache == null) { + synchronized (this) { + saCache = (SaRedisCache) this.saRedisMap.get(cacheName); + if (saCache == null) { + Map configModels = ConfigUtil.getConfigModels(JbootRedisConfig.class); + if (!configModels.containsKey(cacheName)) { + throw new JbootIllegalConfigException("Please config \"jboot.redis." + cacheName + ".host\" in your jboot.properties."); + } + + JbootRedisConfig jbootRedisConfig = (JbootRedisConfig) configModels.get(cacheName); + saCache = new SaRedisCache(jbootRedisConfig); + if (saCache != null) { + this.saRedisMap.put(cacheName, saCache); + } + } + } + } + this.saRedisCache = saCache; serializer = new SaJdkSerializer(); } diff --git a/sa-token-starter/sa-token-jboot-plugin/src/test/java/cn/dev33/satoken/jboot/test/AppRun.java b/sa-token-starter/sa-token-jboot-plugin/src/test/java/cn/dev33/satoken/jboot/test/AppRun.java index 5b7f9372..5ad91108 100644 --- a/sa-token-starter/sa-token-jboot-plugin/src/test/java/cn/dev33/satoken/jboot/test/AppRun.java +++ b/sa-token-starter/sa-token-jboot-plugin/src/test/java/cn/dev33/satoken/jboot/test/AppRun.java @@ -1,7 +1,9 @@ package cn.dev33.satoken.jboot.test; import cn.dev33.satoken.annotation.SaCheckRole; +import cn.dev33.satoken.session.SaSession; import cn.dev33.satoken.stp.StpUtil; +import io.jboot.Jboot; import io.jboot.app.JbootApplication; import io.jboot.web.controller.JbootController; import io.jboot.web.controller.annotation.RequestMapping; @@ -12,24 +14,30 @@ public class AppRun extends JbootController { JbootApplication.run(args); } - public void index(){ + public void index() { renderText("index"); } - public void doLogin(){ + public void doLogin() { StpUtil.login(10001); //赋值角色 renderText("登录成功"); } - public void getLoginInfo(){ - System.out.println("是否登录:"+StpUtil.isLogin()); - System.out.println("登录信息"+StpUtil.getTokenInfo()); + public void getLoginInfo() { + System.out.println("是否登录:" + StpUtil.isLogin()); + System.out.println("登录信息" + StpUtil.getTokenInfo()); renderJson(StpUtil.getTokenInfo()); } @SaCheckRole("super-admin") - public void add(){ + public void add() { renderText("超级管理员方法!"); } + + public void token(String token) { + Object t = Jboot.getRedis().get("xxxxx"); //默认redis库 + SaSession saSession = StpUtil.getSessionByLoginId(StpUtil.getLoginIdByToken(token), false); //satoken redis库 + renderJson(saSession); + } } diff --git a/sa-token-starter/sa-token-jboot-plugin/src/test/resources/jboot.properties b/sa-token-starter/sa-token-jboot-plugin/src/test/resources/jboot.properties index f5912197..c5069fe8 100644 --- a/sa-token-starter/sa-token-jboot-plugin/src/test/resources/jboot.properties +++ b/sa-token-starter/sa-token-jboot-plugin/src/test/resources/jboot.properties @@ -1,12 +1,15 @@ +undertow.devMode=true +undertow.port=9980 +undertow.host=0.0.0.0 #other redis config jboot.cache.type=redis jboot.redis.host=127.0.0.1 jboot.redis.port=6379 jboot.redis.password=123456 -jboot.redis.database=1 +jboot.redis.database=3 #satoken redis config jboot.cache.sa.type=sacache jboot.redis.sa.host=127.0.0.1 jboot.redis.sa.port=6379 jboot.redis.sa.password=123456 -jboot.redis.sa.database=0 \ No newline at end of file +jboot.redis.sa.database=1 \ No newline at end of file