拦截器中获取不到controller注解问题
刚刚在测试接口的时候发现一个奇怪的问题:通过拦截器获取 controller 类注解,有些能获取到,有些又不能获取到,见鬼了。
【环境】:
1. springboot :2.2.0.RELEASE
【场景】:
1. 定义一个登陆拦截器,对请求的 token 进行校验;
2. 定义两个注解:
RequiredLogin :要求登录注解
NoRequiredLogin :不要求登录注解
3. 在要求登录的 controller 类上添加 RequiredLogin 注解,然后在拦截器中获取 controller 是否有该注解,如果有,则进行token校验;
【问题】:
两个不同的 controller ,都添加了 RequiredLogin 注解,在拦截器中使用同样的代码获取注解,期中一个 controller 能够获取到,一个不能。
【代码】
拦截器代码:
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
startMillis.set(System.currentTimeMillis());
if (handler instanceof HandlerMethod) {
HandlerMethod myHandlerMethod = (HandlerMethod) handler;
Object bean = myHandlerMethod.getBean();
Annotation classLoginAnnotation = bean.getClass().getAnnotation(RequiredLogin.class);// 类级别的要求登录标记
System.out.println("类:"+bean.getClass()+",类注解:"+classLoginAnnotation);
Method method = myHandlerMethod.getMethod();
Annotation methodLoginAnnotation = method.getAnnotation(RequiredLogin.class);// 方法级别的要求登录标记
Annotation methodNologinAnnotation = method.getAnnotation(NoRequiredLogin.class);// 方法级别的不要求登录标记
if ((classLoginAnnotation != null && methodNologinAnnotation == null)
|| (classLoginAnnotation == null && methodLoginAnnotation != null)) {
//验证登陆
if (isLogin(request))
return true;
else {
// 未登录
ApiResp apiResp=ApiRespBuilder.buildFailResp(ErrorCodeEnum.code_1001);
response.setHeader("content-type", "application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(JSONObject.fromObject(apiResp).toString());
return false;
}
}
}
return true;
}
controller 代码:
@RequestMapping("/device") @RestController @RequiredLogin public class DeviceController extends BaseController {
@RequestMapping("/hotel") @RestController @RequiredLogin public class HotelController extends BaseController {
【结果】
分别对两个不同 controller 的接口进行请求,并且都不带token,发现 HotelController 返回要求登录,而 DeviceController 接口进入业务代码然后由于没有token报错
HotelController:
DeviceController:
打印结果:
HotelController:
DeviceController:
从打印结果看,两个类的打印信息确实有些不一样,但是我看不懂。。。我也仔细对比两个 Controller 类,但是好像没啥不同。
【说明】
这个拦截器我是从以前的代码拷贝来的,在很多项目中使用过了,一直没有这个问题。于是我对比之前代码的 springboot 版本。之前的版本的 2.2.1,现在的版本是 2.2.0,于是以为是版本问题,但是改成 2.2.1 后也没有用。
【解决】
拦截器代码改一下,直接使用 HandlerMethod 的 getBeanType 方法获取 controller 的 class 信息,而不是先 获取 bean,在 getClass():
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException { startMillis.set(System.currentTimeMillis()); if (handler instanceof HandlerMethod) { HandlerMethod myHandlerMethod = (HandlerMethod) handler; Annotation classLoginAnnotation = myHandlerMethod.getBeanType().getAnnotation(RequiredLogin.class);// 类级别的要求登录标记 Method method = myHandlerMethod.getMethod(); Annotation methodLoginAnnotation = method.getAnnotation(RequiredLogin.class);// 方法级别的要求登录标记 Annotation methodNologinAnnotation = method.getAnnotation(NoRequiredLogin.class);// 方法级别的不要求登录标记 if ((classLoginAnnotation != null && methodNologinAnnotation == null) || (classLoginAnnotation == null && methodLoginAnnotation != null)) { //验证登陆 if (isLogin(request)) return true; else { // 未登录 ApiResp apiResp=ApiRespBuilder.buildFailResp(ErrorCodeEnum.code_1001); response.setHeader("content-type", "application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(JSONObject.fromObject(apiResp).toString()); return false; } } } return true; }
【原因】
还不知道,不太懂 spring 深层原理。。(仅记录一下问题经验)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构