spring拦截器是基于动态代理,注解就是拦截器,所以关于动态代理需要注意的坑,注解同样要注意。

1.创建注解类

/**
* @Target 此注解的作用目标,括号里METHOD的意思说明此注解只能加在方法上面,TYPE意思是可注解于类上
* @Retention 注解的保留位置,括号里RUNTIME的意思说明注解可以存在于运行时,可以用于反射
* @Documented 说明该注解将包含在javadoc中
*/ @Target(value = {ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface IgnoreToken{
}

2.定义拦截器

public class IgnoreTokenHandle extends HandlerInterceptorAdapter{

    /**
* This implementation always returns {@code true}.
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
HandlerMethod handlerMethod = (HandlerMethod) handler;
IgnoreToken ignore = handlerMethod.getBeanType().getAnnotation(IgnoreToken.class);
//Ver ver = handlerMethod.getBeanType().getAnnotation(Ver.class); //定义多个注解
if (null == ignore) {
ignore = handlerMethod.getMethodAnnotation(IgnoreToken.class);//这里可以正确获取到加在方法上的注解
}
if (null == ver) {
ver = handlerMethod.getMethod().getDeclaringClass()
.getAnnotation(Ver.class);//这里不知道哪个大神写的代码,发现不能获取加在方法上的注解,坑了我半天
}
if (ignore != null){
System.out.println("**************************");
}
return true;
}
}

这里踩到了坑。见注释

3.配置拦截地址

@Configuration("admimWebConfig")
@Primary
public class TokenConfiger implements WebMvcConfigurer{ @Bean
IgnoreTokenHandle getIgnoreTokenHandle(){
return new IgnoreTokenHandle();
} @Override
public void addInterceptors(InterceptorRegistry registry) {
ArrayList<String> commonPathPatterns = getExcludeCommonPathPatterns();
registry.addInterceptor(getIgnoreTokenHandle()).addPathPatterns("/**").excludePathPatterns(commonPathPatterns.toArray(new String[]{}));
} private ArrayList<String> getExcludeCommonPathPatterns() {
ArrayList<String> list = new ArrayList<>();
String[] urls = {
"/v2/api-docs",
"/swagger-resources/**",
"/cache/**",
"/api/log/save"
};
Collections.addAll(list, urls);
return list;
}
}

这三部注解就已经可以生效。

完了在你的controller层 类上或方法上加上注解都会生效

springboot添加自定义注解的更多相关文章

  1. SpringBoot 常用注解(持续更新)

    SpringBoot 常用注解 @SpringBootApplication @Bean @ComponentScan @ControllerAdvice @ExceptionHandler @Res ...

  2. 常见的springmvc、SpringBoot的注解

    springMvc的常用注解 : @Controller :用于标记在一个类上,使用它标记的类就是一个springmcv Controller对象,分发处理器将会扫描使用了该注解 的类的方法,并检测该 ...

  3. 浅谈SpringBoot核心注解原理

    SpringBoot核心注解原理 今天跟大家来探讨下SpringBoot的核心注解@SpringBootApplication以及run方法,理解下springBoot为什么不需要XML,达到零配置 ...

  4. SpringBoot的注解注入功能移植到.Net平台(开源)

    *:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...

  5. SpringBoot(14)—注解装配Bean

    SpringBoot(14)-注解装配Bean SpringBoot装配Bean方式主要有两种 通过Java配置文件@Bean的方式定义Bean. 通过注解扫描的方式@Component/@Compo ...

  6. SpringBoot 入门篇(二) SpringBoot常用注解以及自动配置

    一.SpringBoot常用注解二.SpringBoot自动配置机制SpringBoot版本:1.5.13.RELEASE 对应官方文档链接:https://docs.spring.io/spring ...

  7. [技术博客] SPRINGBOOT自定义注解

    SPRINGBOOT自定义注解 在springboot中,有各种各样的注解,这些注解能够简化我们的配置,提高开发效率.一般来说,springboot提供的注解已经佷丰富了,但如果我们想针对某个特定情景 ...

  8. Springboot @ConditionalOnProperty注解

    最近看了一段代码其中用到了@ConditionalOnProperty注解,直接没有了解过这个注解,今天看到了顺便了解一下 具体代码如下 @Configuration public class Web ...

  9. Spring SpringMVC SpringBoot SpringCloud 注解整理大全

    Spring SpringMVC SpringBoot SpringCloud 注解整理 才开的博客所以放了一篇以前整理的文档,如果有需要添加修改的地方欢迎指正,我会修改的φ(๑˃∀˂๑)♪ Spri ...

随机推荐

  1. Java ---- 遍历链表(递归与非递归实现)

    package test; //前序遍历的递归实现与非递归实现 import java.util.Stack; public class Test { public static void main( ...

  2. DRF 认证、权限、限制

    DRF 认证.权限.限制   认证: 定义一个用户表和一个保存用户的Token表 # ======================day96======================= class ...

  3. django Table doesn't exist

    1146 django Table '' doesn't exist   一:出错原因   手动在数据库中drop了一张表,重新执行python manage.py migrate时出错,提示不存在这 ...

  4. 用系统默认mail服务实现邮件发送

    用系统默认mail服务实现邮件发送 1.操作步骤 第一步:设备服务器发送邮件要用的,邮箱地址,账号密码 编辑/etc/mail.rc vim /etc/mail.rc 在文件的结尾追加,账号信息配置 ...

  5. python-day7-静态方法、类方法、属性方法、特殊成员方法、反射、异常处理、socket

    @特殊方法.异常处理.反射.socket @类 属性 实例变量 类变量 私有属性__var 方法 构造方法, 析构函数(python自带,不写也有,写了相当与重构) 私有方法 继承 继承 组合 @7. ...

  6. 网络协议,socket模块

    """网络通讯要素:1.物理介质2.通讯协议 osi五层模型 应用层 传输层 网络层 数据链路层 物理层 物理层能传输010101二进制单纯的二进制是没有意义的,必须得知 ...

  7. MAIL服务器搭建

    一,邮件服务: 优    点 缺   点 应 用 sendmail 有点年代久远,稳定功能多 太过于臃肿,配置文件多且繁琐 6以前默认 postfix 优点更稳定,且交轻便 发布年限较短,市场占有率低 ...

  8. RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa

    两个python,一个是本机自带的,一个是anaconda.先前呢,用自带的安装了Opencv,由于自带的python,对应的numpy版本是13, 而anaconda对应的版本是12,导致impor ...

  9. element-ui:el-table时间格式化

    如果想对表格某一列的内容格式化,可用 formatter 属性.属性绑定格式化的方法即可 <el-table-column prop="update_time" label= ...

  10. 熟悉Junit单元测试方法

    定义: JUnit是一个Java语言的单元测试框架.它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个. JUnit有它自己 ...