mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-05-05 13:17:55 +08:00
处理因jfinal,jboot默认序列化方法不同导致的乱码问题,统一使用SaJdkSerializer进行缓存序列化
This commit is contained in:
parent
f05818d0ea
commit
cce907fd20
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>sa-token-starter</artifactId>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<version>1.28.0</version>
|
||||
<version>1.29.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<packaging>jar</packaging>
|
||||
|
@ -1,13 +1,11 @@
|
||||
package cn.dev33.satoken.jboot;
|
||||
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
import com.jfinal.log.Log;
|
||||
import io.jboot.Jboot;
|
||||
import io.jboot.components.serializer.JbootSerializer;
|
||||
import io.jboot.components.serializer.JbootSerializerManager;
|
||||
import io.jboot.exception.JbootIllegalConfigException;
|
||||
import io.jboot.support.redis.JbootRedisBase;
|
||||
import io.jboot.utils.StrUtil;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
@ -34,12 +32,13 @@ public class SaJedisImpl {
|
||||
serializer = JbootSerializerManager.me().getSerializer(config.getSerializer());
|
||||
}
|
||||
this.config = config;
|
||||
String host = config.getHost();
|
||||
Integer port = config.getPort();
|
||||
Integer timeout = config.getTimeout();
|
||||
String password = config.getPassword();
|
||||
Integer database = config.getDb()==null?config.getDatabase():config.getDb();
|
||||
String clientName = config.getClientName();
|
||||
assert this.config != null;
|
||||
String host = this.config.getHost();
|
||||
Integer port = this.config.getPort();
|
||||
Integer timeout = this.config.getTimeout();
|
||||
String password = this.config.getPassword();
|
||||
Integer database = this.config.getSaDb()==null?this.config.getDatabase():this.config.getSaDb();
|
||||
String clientName = this.config.getClientName();
|
||||
|
||||
if (host.contains(":")) {
|
||||
port = Integer.valueOf(host.split(":")[1]);
|
||||
|
@ -4,21 +4,24 @@ import io.jboot.app.config.annotation.ConfigModel;
|
||||
import io.jboot.support.redis.JbootRedisConfig;
|
||||
|
||||
/**
|
||||
* SA-Token redis缓存配置
|
||||
* SA-Token redis缓存配置,获取database
|
||||
*/
|
||||
@ConfigModel(
|
||||
prefix = "sa.redis"
|
||||
prefix = "jboot.redis"
|
||||
)
|
||||
public class SaRedisConfig extends JbootRedisConfig{
|
||||
|
||||
private Integer db;
|
||||
private Integer saDb;
|
||||
|
||||
public Integer getDb() {
|
||||
return this.db;
|
||||
public SaRedisConfig(){
|
||||
|
||||
}
|
||||
public Integer getSaDb() {
|
||||
return this.saDb;
|
||||
}
|
||||
|
||||
public void setDb(Integer db) {
|
||||
this.db = db;
|
||||
public void setSaDb(Integer saDb) {
|
||||
this.saDb = saDb;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import cn.dev33.satoken.servlet.model.SaStorageForServlet;
|
||||
import io.jboot.web.controller.JbootControllerContext;
|
||||
|
||||
/**
|
||||
* Sa-Token 上下文处理器 [Jboot 版本实现]
|
||||
* Sa-Token 上线文处理器 [Jboot 版本实现]
|
||||
*/
|
||||
public class SaTokenContextForJboot implements SaTokenContext {
|
||||
/**
|
||||
|
@ -0,0 +1,154 @@
|
||||
package cn.dev33.satoken.jboot;
|
||||
|
||||
import cn.dev33.satoken.exception.SaTokenException;
|
||||
import cn.dev33.satoken.filter.SaFilterAuthStrategy;
|
||||
import cn.dev33.satoken.filter.SaFilterErrorStrategy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class SaTokenPathFilter {
|
||||
|
||||
// ------------------------ 设置此过滤器 拦截 & 放行 的路由
|
||||
|
||||
/**
|
||||
* 拦截路由
|
||||
*/
|
||||
private List<String> includeList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 放行路由
|
||||
*/
|
||||
private List<String> excludeList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 添加 [拦截路由]
|
||||
* @param paths 路由
|
||||
* @return 对象自身
|
||||
*/
|
||||
public SaTokenPathFilter addInclude(String... paths) {
|
||||
includeList.addAll(Arrays.asList(paths));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加 [放行路由]
|
||||
* @param paths 路由
|
||||
* @return 对象自身
|
||||
*/
|
||||
public SaTokenPathFilter addExclude(String... paths) {
|
||||
excludeList.addAll(Arrays.asList(paths));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入 [拦截路由] 集合
|
||||
* @param pathList 路由集合
|
||||
* @return 对象自身
|
||||
*/
|
||||
public SaTokenPathFilter setIncludeList(List<String> pathList) {
|
||||
includeList = pathList;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入 [放行路由] 集合
|
||||
* @param pathList 路由集合
|
||||
* @return 对象自身
|
||||
*/
|
||||
public SaTokenPathFilter setExcludeList(List<String> pathList) {
|
||||
excludeList = pathList;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 [拦截路由] 集合
|
||||
* @return see note
|
||||
*/
|
||||
public List<String> getIncludeList() {
|
||||
return includeList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 [放行路由] 集合
|
||||
* @return see note
|
||||
*/
|
||||
public List<String> getExcludeList() {
|
||||
return excludeList;
|
||||
}
|
||||
|
||||
|
||||
// ------------------------ 钩子函数
|
||||
|
||||
/**
|
||||
* 认证函数:每次请求执行
|
||||
*/
|
||||
public SaFilterAuthStrategy auth = r -> {};
|
||||
|
||||
/**
|
||||
* 异常处理函数:每次[认证函数]发生异常时执行此函数
|
||||
*/
|
||||
public SaFilterErrorStrategy error = e -> {
|
||||
throw new SaTokenException(e);
|
||||
};
|
||||
|
||||
/**
|
||||
* 前置函数:在每次[认证函数]之前执行
|
||||
*/
|
||||
public SaFilterAuthStrategy beforeAuth = r -> {};
|
||||
|
||||
/**
|
||||
* 写入[认证函数]: 每次请求执行
|
||||
* @param auth see note
|
||||
* @return 对象自身
|
||||
*/
|
||||
public SaTokenPathFilter setAuth(SaFilterAuthStrategy auth) {
|
||||
this.auth = auth;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入[异常处理函数]:每次[认证函数]发生异常时执行此函数
|
||||
* @param error see note
|
||||
* @return 对象自身
|
||||
*/
|
||||
public SaTokenPathFilter setError(SaFilterErrorStrategy error) {
|
||||
this.error = error;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入[前置函数]:在每次[认证函数]之前执行
|
||||
* @param beforeAuth see note
|
||||
* @return 对象自身
|
||||
*/
|
||||
public SaTokenPathFilter setBeforeAuth(SaFilterAuthStrategy beforeAuth) {
|
||||
this.beforeAuth = beforeAuth;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/*@Override
|
||||
public void doFilter(Controller ctx, FilterChain chain) throws Throwable {
|
||||
try {
|
||||
// 执行全局过滤器
|
||||
SaRouter.match(includeList).notMatch(excludeList).check(r -> {
|
||||
beforeAuth.run(null);
|
||||
auth.run(null);
|
||||
});
|
||||
|
||||
} catch (StopMatchException e) {
|
||||
|
||||
} catch (Throwable e) {
|
||||
// 1. 获取异常处理策略结果
|
||||
String result = (e instanceof BackResultException) ? e.getMessage() : String.valueOf(error.run(e));
|
||||
// 2. 写入输出流
|
||||
ctx.renderText(result);
|
||||
return;
|
||||
}
|
||||
|
||||
// 执行
|
||||
chain.doFilter(ctx);
|
||||
}*/
|
||||
}
|
@ -1,27 +1,35 @@
|
||||
package cn.dev33.satoken.jboot.test;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.config.SaCookieConfig;
|
||||
import cn.dev33.satoken.config.SaTokenConfig;
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
import cn.dev33.satoken.jboot.SaAnnotationInterceptor;
|
||||
import cn.dev33.satoken.jboot.SaTokenContextForJboot;
|
||||
import cn.dev33.satoken.jboot.SaTokenDaoRedis;
|
||||
import cn.dev33.satoken.util.SaTokenConsts;
|
||||
import com.jfinal.config.Constants;
|
||||
import com.jfinal.config.Interceptors;
|
||||
import com.jfinal.config.Routes;
|
||||
import com.jfinal.template.Engine;
|
||||
import cn.dev33.satoken.jboot.SaAnnotationInterceptor;
|
||||
import cn.dev33.satoken.jboot.SaTokenContextForJboot;
|
||||
import cn.dev33.satoken.jboot.SaTokenDaoRedis;
|
||||
import io.jboot.aop.jfinal.JfinalHandlers;
|
||||
import io.jboot.aop.jfinal.JfinalPlugins;
|
||||
import io.jboot.core.listener.JbootAppListener;
|
||||
|
||||
public class AtteStartListener implements JbootAppListener {
|
||||
public void onInit() {
|
||||
//注册权限验证功能,由saToken处理请求上下文
|
||||
SaTokenContext saTokenContext = new SaTokenContextForJboot();
|
||||
SaManager.setSaTokenContext(saTokenContext);
|
||||
//加载权限角色设置数据接口
|
||||
SaManager.setStpInterface(new StpInterfaceImpl());
|
||||
//增加redis缓存,需要先配置redis地址
|
||||
// SaManager.setSaTokenDao(new SaTokenDaoRedis());
|
||||
SaTokenConfig saTokenConfig = new SaTokenConfig();
|
||||
saTokenConfig.setTokenStyle(SaTokenConsts.TOKEN_STYLE_SIMPLE_UUID);
|
||||
saTokenConfig.setTimeout(60*60*4); //登录有效时间4小时
|
||||
saTokenConfig.setActivityTimeout(30*60); //半小时无操作过期处理
|
||||
saTokenConfig.setIsShare(false);
|
||||
saTokenConfig.setTokenName("token"); //更换satoken的名称
|
||||
saTokenConfig.setCookie(new SaCookieConfig().setHttpOnly(true)); //开启cookies的httponly属性
|
||||
SaManager.setConfig(saTokenConfig);
|
||||
SaManager.setSaTokenDao(new SaTokenDaoRedis());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -42,7 +42,7 @@ public class PathAnalyzer {
|
||||
p = p.replace("$", "\\$");
|
||||
p = p.replace("**", ".[]");
|
||||
p = p.replace("*", "[^/]*");
|
||||
if (p.indexOf("{") >= 0) {
|
||||
if (p.contains("{")) {
|
||||
if (p.indexOf("_}") > 0) {
|
||||
p = p.replaceAll("\\{[^\\}]+?\\_\\}", "(.+?)");
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import com.jfinal.render.RenderManager;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
|
||||
public class SaTokenActionHandler extends ActionHandler {
|
||||
protected boolean devMode;
|
||||
protected ActionMapping actionMapping;
|
||||
|
@ -11,9 +11,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 缓存处理类
|
||||
*/
|
||||
public class SaTokenDaoRedis implements SaTokenDao {
|
||||
|
||||
protected Cache redis;
|
||||
|
@ -1,10 +1,14 @@
|
||||
package cn.dev33.satoken.jfinal.test;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.config.SaCookieConfig;
|
||||
import cn.dev33.satoken.config.SaTokenConfig;
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
import cn.dev33.satoken.jfinal.*;
|
||||
import cn.dev33.satoken.util.SaTokenConsts;
|
||||
import com.jfinal.config.*;
|
||||
import com.jfinal.plugin.redis.RedisPlugin;
|
||||
import com.jfinal.plugin.redis.serializer.ISerializer;
|
||||
import com.jfinal.template.Engine;
|
||||
|
||||
public class Config extends JFinalConfig {
|
||||
@ -15,7 +19,17 @@ public class Config extends JFinalConfig {
|
||||
SaManager.setSaTokenContext(saTokenContext);
|
||||
//加载权限角色设置数据接口
|
||||
SaManager.setStpInterface(new StpInterfaceImpl());
|
||||
|
||||
//设置token生成类型
|
||||
SaTokenConfig saTokenConfig = new SaTokenConfig();
|
||||
saTokenConfig.setTokenStyle(SaTokenConsts.TOKEN_STYLE_SIMPLE_UUID);
|
||||
saTokenConfig.setTimeout(60*60*4); //登录有效时间4小时
|
||||
saTokenConfig.setActivityTimeout(30*60); //半小时无操作过期处理
|
||||
saTokenConfig.setIsShare(false);
|
||||
saTokenConfig.setTokenName("token"); //更改satoken的cookies名称
|
||||
SaCookieConfig saCookieConfig = new SaCookieConfig();
|
||||
saCookieConfig.setHttpOnly(true); //开启cookies 的httponly属性
|
||||
saTokenConfig.setCookie(saCookieConfig);
|
||||
SaManager.setConfig(saTokenConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -37,7 +51,7 @@ public class Config extends JFinalConfig {
|
||||
@Override
|
||||
public void configPlugin(Plugins plugins) {
|
||||
//添加redis扩展
|
||||
// plugins.add(createRedisPlugin("satoken",10));
|
||||
plugins.add(createRedisPlugin("satoken",1, SaJdkSerializer.me));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -56,16 +70,17 @@ public class Config extends JFinalConfig {
|
||||
* 创建Redis插件
|
||||
* @param name 名称
|
||||
* @param dbIndex 使用的库ID
|
||||
* @param serializer 自定义序列化方法
|
||||
* @return
|
||||
*/
|
||||
private RedisPlugin createRedisPlugin(String name, Integer dbIndex) {
|
||||
RedisPlugin redisPlugin=new RedisPlugin(name, "redis-host", 6379, 3000,"pwd",dbIndex);
|
||||
redisPlugin.setSerializer(SaJdkSerializer.me);
|
||||
private RedisPlugin createRedisPlugin(String name, Integer dbIndex, ISerializer serializer) {
|
||||
RedisPlugin redisPlugin = new RedisPlugin(name, "redis-host", 6379, 3000,"pwd",dbIndex);
|
||||
redisPlugin.setSerializer(serializer);
|
||||
return redisPlugin;
|
||||
}
|
||||
@Override
|
||||
public void onStart(){
|
||||
//增加redis缓存,需要先配置redis地址
|
||||
// SaManager.setSaTokenDao(new SaTokenDaoRedis("satoken"));
|
||||
SaManager.setSaTokenDao(new SaTokenDaoRedis("satoken"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user