springboot添加自定义注解
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添加自定义注解的更多相关文章
- SpringBoot 常用注解(持续更新)
SpringBoot 常用注解 @SpringBootApplication @Bean @ComponentScan @ControllerAdvice @ExceptionHandler @Res ...
- 常见的springmvc、SpringBoot的注解
springMvc的常用注解 : @Controller :用于标记在一个类上,使用它标记的类就是一个springmcv Controller对象,分发处理器将会扫描使用了该注解 的类的方法,并检测该 ...
- 浅谈SpringBoot核心注解原理
SpringBoot核心注解原理 今天跟大家来探讨下SpringBoot的核心注解@SpringBootApplication以及run方法,理解下springBoot为什么不需要XML,达到零配置 ...
- SpringBoot的注解注入功能移植到.Net平台(开源)
*:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...
- SpringBoot(14)—注解装配Bean
SpringBoot(14)-注解装配Bean SpringBoot装配Bean方式主要有两种 通过Java配置文件@Bean的方式定义Bean. 通过注解扫描的方式@Component/@Compo ...
- SpringBoot 入门篇(二) SpringBoot常用注解以及自动配置
一.SpringBoot常用注解二.SpringBoot自动配置机制SpringBoot版本:1.5.13.RELEASE 对应官方文档链接:https://docs.spring.io/spring ...
- [技术博客] SPRINGBOOT自定义注解
SPRINGBOOT自定义注解 在springboot中,有各种各样的注解,这些注解能够简化我们的配置,提高开发效率.一般来说,springboot提供的注解已经佷丰富了,但如果我们想针对某个特定情景 ...
- Springboot @ConditionalOnProperty注解
最近看了一段代码其中用到了@ConditionalOnProperty注解,直接没有了解过这个注解,今天看到了顺便了解一下 具体代码如下 @Configuration public class Web ...
- Spring SpringMVC SpringBoot SpringCloud 注解整理大全
Spring SpringMVC SpringBoot SpringCloud 注解整理 才开的博客所以放了一篇以前整理的文档,如果有需要添加修改的地方欢迎指正,我会修改的φ(๑˃∀˂๑)♪ Spri ...
随机推荐
- idea properties文件中文无法正常显示
引用:https://blog.csdn.net/u010999809/article/details/81204457 问题:在idea打开配置文件,已经设置了全局编码格式为UTF-8,和项目编码格 ...
- 微信小程序+没有找到node_modules目录
第一步:设置-->项目设置-->使用npm模块 第二步:右键目录下miniprogram-->终端打开-->输入npm init-->在packagename下输入:sm ...
- OpenCV模板匹配函数matchTemplate详解
参考文档:http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/histograms/template_matchin ...
- linux 退出当前命令的编辑
有时候输入命令,linux既不响应命令,也不提示错误.可以用如下方法结束当前命令: ctrl+z挂起当前进程如果需要恢复到前台输入fg,恢复到后台输入bgctrl+c,彻底终止该进程
- 在WINDOWS服务器下设置MARIADB自动备份的方法
WINDOWS服务器下自动备份有三个步骤 1.首先新建一个.BAT文件里面输入 @echo offset "Ymd=%date:~,4%%date:~5,2%%date:~8,2%" ...
- python函数与模块(装饰器,文件处理,迭代器等)
os模块 os.system('命令') 利用python调用系统命令,命令可以是以列表或者元组内的元素形式* res import os res=os.system('ipconfig') prin ...
- svg在vue-cli3中的配置
svg矢量图有时候在项目中用起来当然要比图片用起来好.之前没有用过svg,这次花了一点时间在项目中支持了svg的使用,个人感觉用起来还是没有直接用font class的爽,然后又改成font clas ...
- vue-Swiper-awsome
<swiper :options="swiperOption"> <swiper-slide><img src="static/images ...
- Spring Boot实现文件下载功能
我们只需要创建一个控制器(Controler)文件,即Controller目录下的File_Download.java,其完整目录如下: @Controller public class File_D ...
- Javascript 使用 async 声明符和 await 操作符进行异步操作
async function 声明用于定义一个返回 AsyncFunction 对象的异步函数 await 操作符用于等待一个Promise 对象.它只能在异步函数 async function 中 ...