修复jboot使用独立Redis库时创建连接错误的BUG

This commit is contained in:
星痕 2022-04-22 14:47:25 +08:00
parent 4de21e7391
commit 2e82fc0592
4 changed files with 48 additions and 16 deletions

View File

@ -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<String> 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();

View File

@ -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<String, SaRedisCache> 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<String, JbootRedisConfig> 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();
}

View File

@ -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);
}
}

View File

@ -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
jboot.redis.sa.database=1