路由拦截鉴权业务内部500错误也报错“未能读取到有效 token”
使用版本:
涉及的功能模块:
测试步骤:
- 我经过以下步骤测试:
`package com.cdn.flex.config;
import cn.dev33.satoken.interceptor.SaInterceptor; import cn.dev33.satoken.router.SaRouter; import cn.dev33.satoken.stp.StpUtil; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
-
蔡定努
-
2024/11/13 17:02 */ @Configuration public class SaTokenConfigure implements WebMvcConfigurer { // 注册 Sa-Token 拦截器,打开注解式鉴权功能 @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin())) .addPathPatterns("/**") .excludePathPatterns("/user/addMoney"); } }
@RequestMapping("addMoney") public SaResult addMoney() {if (1==1){ throw new RuntimeException("test"); } return SaResult.ok();} `
- 得出以下结果:
此时应该报错RuntimeException,但是实际是NotLoginException,注解@SaCheckLogin 方式没问题
底层拦截了,可以全局异常处理
@ExceptionHandler(NotPermissionException.class)
public Result<Void> handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
return Result.noAuth();
}
@ExceptionHandler(NotLoginException.class)
public Result<Void> handleNotLoginException(NotLoginException e, HttpServletRequest request) {
return Result.noAuth();
}
邮件已经收到,谢谢!
邮件已经收到,谢谢!