刚刚在测试接口的时候发现一个奇怪的问题:通过拦截器获取 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 深层原理。。(仅记录一下问题经验)

拦截器中获取不到controller注解问题的更多相关文章

  1. spring boot拦截器中获取request post请求中的参数

    最近有一个需要从拦截器中获取post请求的参数的需求,这里记录一下处理过程中出现的问题. 首先想到的就是request.getParameter(String )方法,但是这个方法只能在get请求中取 ...

  2. [技巧篇]08.Struts2拦截器中获取Servlet API方法

    讲课中遇到的解决Session拦截器的后腿问题,还有如何在拦截器中获取Servlet API,这里留一个备注,方便学生查找

  3. MVC 在action拦截器中获取当前进入的控制器和aciton名

    我们在实现了action拦截器以后(继承至System.Web.Mvc.IActionFilter),需要在重写的方法OnActionExecuting中去获得当前进入的控制器和action名称,如何 ...

  4. springboot拦截器中获取配置文件值

    package com.zhx.web.interceptor; import com.zhx.util.other.IpUtil; import org.slf4j.Logger; import o ...

  5. spring boot拦截器中获取request post请求中的参数(转)

    文章转自 https://www.jianshu.com/p/69c6fba08c92

  6. 【spring boot】在自定义拦截器中从request中获取json字符串

    又这样的需求,需要在自定义的拦截器中获取request中的数据,想获取到的是JSON字符串 那需要在拦截器中写这样一个方法 public static String getOpenApiRequest ...

  7. 解决SpringMVC拦截器中Request数据只能读取一次的问题

    解决SpringMVC拦截器中Request数据只能读取一次的问题 开发项目中,经常会直接在request中取数据,如Json数据,也经常用到@RequestBody注解,也可以直接通过request ...

  8. Spring拦截器中通过request获取到该请求对应Controller中的method对象

    背景:项目使用Spring 3.1.0.RELEASE,从dao到Controller层全部是基于注解配置.我的需求是想在自定义的Spring拦截器中通过request获取到该请求对应于Control ...

  9. java自定义注解知识实例及SSH框架下,拦截器中无法获得java注解属性值的问题

    一.java自定义注解相关知识 注解这东西是java语言本身就带有的功能特点,于struts,hibernate,spring这三个框架无关.使用得当特别方便.基于注解的xml文件配置方式也受到人们的 ...

随机推荐

  1. Dart接口

    /* 和Java一样,dart也有接口,但是和Java还是有区别的. 首先,dart的接口没有interface关键字定义接口,而是普通类或抽象类都可以作为接口被实现. 同样使用implements关 ...

  2. ISO/IEC 9899:2011 条款6.7.6——声明符

    6.7.6 声明符 语法 1.declarator: pointeropt    direct-declarator direct-declarator: identifier (    declar ...

  3. Spark ML 中 VectorIndexer, StringIndexer等用法(转载)

    VectorIndexer 主要作用:提高决策树或随机森林等ML方法的分类效果.VectorIndexer是对数据集特征向量中的类别(离散值)特征(index categorical features ...

  4. 报错:ModuleNotFoundError: No module named '_ctypes'

    报错背景: CentOS 7 Python3.7 安装 setuptools 插件的时候报错. 报错现象: [root@master setuptools-]# python3. setup.py b ...

  5. python flask框架学习(三)——豆瓣微信小程序案例(二)整理封装block,模板的继承

    我们所要实现的效果: 点击电影的更多,跳转到更多的电影页面:点击电视剧的更多,跳转到更多的电视剧页面. 三个页面的风格相同,可以设置一个模板,三个页面都继承这个模板 1.在指定模板之前,把css放在一 ...

  6. LeetCode_342. Power of Four

    342. Power of Four Easy Given an integer (signed 32 bits), write a function to check whether it is a ...

  7. HDU3853 LOOPS 期望DP基础题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3853 题目大意(只是大意,名字什么的可能和原题描述不一样~): 爱丽丝与华容道 题目描述 爱丽丝是一个 ...

  8. Centos7防火墙关闭和启用iptables操作

    https://yq.aliyun.com/ziliao/33590前序 还是docker惹得最近做的系统都是上的centos7的系统带来的一系列的新环境的适应 补记下:在使用oracle vmbox ...

  9. php+memcache高速缓存

    原文地址:https://blog.csdn.net/qq_33571752/article/details/86494667 #在lnmp基础上修改 依赖libevent程序库 cd lnmp/ta ...

  10. Linux如何查看进程、杀死进程、启动进程

    1.查看进程:ps命令 下面的命令还没实践,仅仅供你参考:可以用man ps查看格式,只不过是一个小工具而已! ps a 显示现行终端机下的所有程序,包括其他用户的程序.    ps -A 显示所有程 ...