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 ...
随机推荐
- JAVA\Android 多线程实现方式及并发与同步
转载:https://blog.csdn.net/csdn_aiyang/article/details/65442540 概述 说到线程,就不得不先说线程和进程的关系,这里先简单解释一下,进 ...
- spring资源加载结构解析
1.spring中资源加载使用resources的原因? 在java将不同资源抽象成url,然后通过注册不同的hander来处理不同读取逻辑,一般hander使用协议的前缀来命名,如http,jar, ...
- Shiro简介——《跟我学Shiro》
地址: http://jinnianshilongnian.iteye.com/blog/2018936
- python-web自动化-键盘操作
selenium提供了较为完整的键盘操作引入 from selenium.webdriver.common.keys import Keys使用键盘操作时,需要借助send_keys()来模拟操作.K ...
- OOP AOP
OOP 一切皆对象,,,对象交互---功能,,,功能叠加---模块,,,模块叠加----系统 AOP 面向切面, 业务逻辑外,添加公共逻辑,增加日志功能,权限控制功能,缓存处理,异常处理,事务,性 ...
- oracle 的tnsnames.ora,listener.ora
x:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN listener.ora: # listener.ora Network Conf ...
- 菜鸟教程之学习Shell script笔记(上)
菜鸟教程之学习Shell script笔记 以下内容是,学习菜鸟shell教程整理的笔记 菜鸟教程之shell教程:http://www.runoob.com/linux/linux-shell.ht ...
- 从客户端出现小于等于公式符号引发检测到有潜在危险的Request.Form 值
可以在处理Post方法的Action添加一个特性:[ValidateInput(false)],这样处理就更加有针对性,提高页面的安全性. [HttpPost][ValidateInput(false ...
- C 语言能不能在头文件定义全局变量?
可以,但一般不会将全局变量的定义写在头文件中. 因为如果多个 C 源文件都添加了头文件,很容易引起重定义的问题.这时候一般编译器都会提示:“multiple definition of... firs ...
- windown 下最简单的安装mysql方式
最近自己的mysql要升级,需要重新安装mysql,官网有提供傻瓜式的安装方式.. 记得下载.msi的格式.这个安装最简单.