mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-09-20 10:37:57 +08:00
v1.0.3版本更新
This commit is contained in:
@@ -18,7 +18,7 @@ public class MySaTokenConfig extends WebMvcConfigurationSupport {
|
||||
// public SaTokenConfig getSaTokenConfig() {
|
||||
// SaTokenConfig config = new SaTokenConfig();
|
||||
// config.setTokenName("satoken"); // token名称(同时也是cookie名称)
|
||||
// config.setTimeout(30 * 24 * 60 * 60); // token有效期,单位s 默认30天,-1为永不过期
|
||||
// config.setTimeout(30 * 24 * 60 * 60); // token有效期,单位s 默认30天
|
||||
// config.setIsShare(true); // 在多人登录同一账号时,是否共享会话(为true时共用一个,为false时新登录挤掉旧登录)
|
||||
// config.setIsReadHead(true); // 是否在cookie读取不到token时,继续从请求header里继续尝试读取
|
||||
// config.setIsReadBody(true); // 是否在cookie读取不到token时,继续从请求header里继续尝试读取
|
||||
@@ -29,7 +29,8 @@ public class MySaTokenConfig extends WebMvcConfigurationSupport {
|
||||
// 注册sa-token的拦截器,打开注解式鉴权功能
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(new SaCheckInterceptor()).addPathPatterns("/**");
|
||||
registry.addInterceptor(new SaCheckInterceptor()).addPathPatterns("/**"); // 全局拦截器
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -6,7 +6,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
// import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -29,10 +28,8 @@ public class SaTokenDaoRedis implements SaTokenDao {
|
||||
@Autowired
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void setRedisTemplate(RedisTemplate redisTemplate) {
|
||||
RedisSerializer stringSerializer = new StringRedisSerializer();
|
||||
redisTemplate.setKeySerializer(stringSerializer);
|
||||
JdkSerializationRedisSerializer jrSerializer = new JdkSerializationRedisSerializer();
|
||||
redisTemplate.setValueSerializer(jrSerializer);
|
||||
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
|
||||
this.redisTemplate = redisTemplate;
|
||||
}
|
||||
|
||||
|
@@ -6,7 +6,6 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* ajax返回Json的封装
|
||||
* 【此类封装了Meta和Body的功能,写法不同,但是返回数据格式相同】
|
||||
*/
|
||||
public class AjaxJson implements Serializable{
|
||||
|
||||
|
@@ -31,6 +31,7 @@ public class TestController {
|
||||
// System.out.println("当前是否登录:" + StpUtil.isLogin());
|
||||
// System.out.println("当前登录账号:" + StpUtil.getLoginId_defaultNull());
|
||||
// StpUtil.setLoginId(id); // 在当前会话登录此账号
|
||||
// System.out.println("根据token找登录id:" + StpUtil.getLoginIdByToken(StpUtil.getTokenValue()));
|
||||
|
||||
System.out.println("当前token信息:" + StpUtil.getTokenInfo()); // 获取登录id并转为int
|
||||
System.out.println("当前登录账号:" + StpUtil.getLoginId_defaultNull());
|
||||
|
@@ -17,7 +17,7 @@ import cn.dev33.satoken.exception.NotPermissionException;
|
||||
/**
|
||||
* 加强版控制器
|
||||
*/
|
||||
@ControllerAdvice // 可指定包前缀,比如:(basePackages = "com.zyd.blog.controller.admin")
|
||||
@ControllerAdvice // 可指定包前缀,比如:(basePackages = "com.pj.admin")
|
||||
public class TopController {
|
||||
|
||||
// 在每个控制器之前触发的操作
|
||||
@@ -31,27 +31,23 @@ public class TopController {
|
||||
public void handlerException(Exception e, HttpServletRequest request, HttpServletResponse response)
|
||||
throws Exception {
|
||||
|
||||
e.printStackTrace(); // 打印堆栈,以供调试
|
||||
// 打印堆栈,以供调试
|
||||
e.printStackTrace();
|
||||
|
||||
response.setContentType("application/json; charset=utf-8"); // http说明,我要返回JSON对象
|
||||
|
||||
// 如果是未登录异常
|
||||
if (e instanceof NotLoginException) {
|
||||
String jsonStr = new ObjectMapper().writeValueAsString(AjaxJson.getNotLogin());
|
||||
response.getWriter().print(jsonStr);
|
||||
return;
|
||||
}
|
||||
// 如果是权限异常
|
||||
if (e instanceof NotPermissionException) {
|
||||
// 不同异常返回不同状态码
|
||||
AjaxJson aj = null;
|
||||
if (e instanceof NotLoginException) { // 如果是未登录异常
|
||||
aj = AjaxJson.getNotLogin();
|
||||
} else if(e instanceof NotPermissionException) { // 如果是权限异常
|
||||
NotPermissionException ee = (NotPermissionException) e;
|
||||
String jsonStr = new ObjectMapper().writeValueAsString(AjaxJson.getNotJur("无此权限:" + ee.getCode()));
|
||||
response.getWriter().print(jsonStr);
|
||||
return;
|
||||
aj = AjaxJson.getNotJur("无此权限:" + ee.getCode());
|
||||
} else { // 普通异常, 输出:500 + 异常信息
|
||||
aj = AjaxJson.getError(e.getMessage());
|
||||
}
|
||||
|
||||
// 普通异常输出:500 + 异常信息
|
||||
response.getWriter().print(new ObjectMapper().writeValueAsString(AjaxJson.getError(e.getMessage())));
|
||||
|
||||
// 输出到客户端
|
||||
response.setContentType("application/json; charset=utf-8"); // http说明,我要返回JSON对象
|
||||
response.getWriter().print(new ObjectMapper().writeValueAsString(aj));
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user