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. LeetCode 797. All Paths From Source to Target

    题目链接:https://leetcode.com/problems/all-paths-from-source-to-target/description/ Given a directed, ac ...

  2. SQL中的排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)简介

    排名函数是Sql Server2005新增的功能,下面简单介绍一下他们各自的用法和区别. 在使用排名函数的时候需要注意以下三点: 1.排名函数必须有 OVER 子句. 2.排名函数必须有包含 ORDE ...

  3. redis权限认证及登录

    找到配置文件 redis.conf 找到   requirepass 字段 去掉注释,改为  requirepass  yourpassword 重启redis: service redis rest ...

  4. Flex 布局的教程

    前言:以前也经常用flex布局,但是最近看到别人使用的时候,发觉以前自己还是不够理解这个,重新看了一遍http://www.ruanyifeng.com/blog/2015/07/flex-gramm ...

  5. python-web自动化-文件上传操作(非input标签的上传,需要借助第三方工具)

    文件上传操作 一.文件上传分两种情况:1. 如果是input可以直接输入路径的,可以直接调send_keys输入路径 2. 非input标签的上传,需要借助第三方工具:    2.1 Autolt 需 ...

  6. Hive实现交叉二维分析的小语句

    1. 梳理出你要的列和行维度 列维度: 每一周 行维度: 年级 + 学科 + 班型 2. 对数据按周增序进行聚合 (即根据列维度) ,生成list concat_ws 和 collect_list ( ...

  7. Spring注解测试

    @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicatio ...

  8. MySql CURD操作(数据的增删改查)

    1.增 格式  insert into 表名字 (列名) values(...); 两种方式 1.直接insert into 表名字 values(...);  全部插入 2.insert into ...

  9. python中os.path模块简介

    1.python中获取当前工作目录 curDir = os.getcwd() os.getcwd()返回的是执行命令时所在的目录,而不是脚本本身所在的目录 2.os.path os.path.absp ...

  10. 原生JS实现异步图片上传(预览)

    效果 实现过程分为两步 1. 用户点击添加后通过 H5文件读取 FileReader对象以DataURL的格式读取图片 2. 通过FormData对象生成表单数据,通过ajax上传到后台 HTML & ...