springboot拦截器解耦——自定义注解
云中志
共 6516字,需浏览 14分钟
· 2021-04-27
什么是注解
public Configuration {
annotation = Component.class
)
String value() default "";
boolean proxyBeanMethods() default true;
}
创建注解的关键字是@interface 注解上也可以加注解 注解可以有属性
@Retention:保留期,声明注解的存活时间 RetentionPolicy.SOURCE:仅源代码时保留,在编译时会被丢弃忽略 RetentionPolicy.CLASS:编译为class时保留,但不会被加载到jvm中 RetentionPolicy.RUNTIME:运行环境保留,会被加载到jvm中 @Documented:保留本类中的注解并能够被javadoc识别 @Target:指定注解的添加位置(类/方法/变量等) ElementType.TYPE:类注解 ElementType.FIELD:字段注解 ElementType.METHOD:方法注解 ElementType.PARAMETER:方法内的参数注解 ElementType.CONSTRUCTOR:构造方法注解 ElementType.LOCAL_VARIABLE:局部变量注解 ElementType.ANNOTATION_TYPE:注解注解 ElementType.PACKAGE:包注解 ElementType.TYPE_PARAMETER ElementType.TYPE_USE @Inherited:注解是否能够被子类继承
创建springboot项目
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.syske</groupId>
<artifactId>custom-annotation-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>custom-annotation-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.61</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
自定义注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckAuth {
}
自定义注解应用
public class AuthenticationInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
if (handlerMethod.hasMethodAnnotation(CheckAuth.class)) {
System.out.println("有CheckAuth注解");
String token = request.getParameter("token");
if (!"ABCDEF12345".equals(token)) {
throw new AuthException();
}
}
}
return true;
}
}
// 鉴权拦截器
registry.addInterceptor(new AuthenticationInterceptor()).addPathPatterns("/**");
handlerMethod.hasMethodAnnotation(CheckAuth.class)
,即判断当前方法是否有CheckAuth.class
注解。HandlerMethod:方法 ResourceHttpRequestHandler:静态资源
总结
评论
使用 YOLO 进行自定义对象检测
点击上方“小白学视觉”,选择加"星标"或“置顶”重磅干货,第一时间送达我们知道我们可以专门检测一些对象。那么我们如何训练系统检测自定义对象呢?让我们一步一步来。1. 创建数据集机器是通过数据集学习的。数据集必须包含图像和标签。例如,让我的目标是创建一个检测坦克的系统。我准备了从网上下载的坦克图片。然
小白学视觉
0
SpringBoot 实现图片防盗链功能
程序员的成长之路互联网/程序员/技术/资料共享 关注阅读本文大概需要 4 分钟。来自:blog.csdn.net/weixin_46157208/article/details/138051737前言出于安全考虑,我们需要后端返回的图片只允许在某个网站内展示,不想被爬虫拿到图片地
程序员的成长之路
0
springboot第70集:字节跳动后端三面经,一文让你走出微服务迷雾架构周刊
创建一个使用Kubernetes (K8s) 和 Jenkins 来自动化 GitLab 前端项目打包的CI/CD流水线,需要配置多个组件。下面,我将概述一个基本的设置步骤和示例脚本,以帮助你理解如何使用这些工具整合一个自动化流程。前提条件确保你已经有:Kubernetes 集群:用于部署 Jenk
程序源代码
0
性能爆表:SpringBoot利用ThreadPoolTaskExecutor批量插入百万级数据实测!
关注我们,设为星标,每天7:40不见不散,架构路上与您共享回复架构师获取资源大家好,我是你们的朋友架构君,一个会写代码吟诗的架构师。来源:azdebug.blog.csdn.net/article/details/103697108前言具体实现细节测试结果总结前言开发目的: 提高百万级数据
Java架构师社区
0
美团一面:为什么 Spring 和 IDEA 都不推荐使用 @Autowired 注解??
点击关注公众号,Java 干货及时推送↓推荐阅读:铜三铁四,怒拿 35K * 14 薪!作者:小亮哥Ya链接:https://juejin.cn/post/7080441168462348319大家在使用IDEA开发的时候有没有注意到过一个提示,在字段上使用Spring的依赖注入注解@Au
Java技术栈
0
低空经济20人|白鲸航线胡震东:无人货运飞机,提供未来航空货运的最优解
白鲸航线专注于国产自主可控的大型无人货运飞机研制。本期“低空经济20人”邀请到白鲸航线董事长兼CEO胡震东,聊聊无人货运飞机的研发与应用、W5000的机型开发与商业规划,以及他对低空经济发展的理解与观点。冲击万店,汉堡界能跑出下一个蜜雪冰城吗?文 | 黄岚清2023年,在国家大力发展无人机产业的背景
亿欧网
1
炫技Groovy!SpringBoot中的动态编程实战
来源:juejin.cn/post/7139877924676567048👉 欢迎加入小哈的星球 ,你将获得: 专属的项目实战 / Java 学习路线 / 一对一提问 / 学习打卡 / 赠书福利全栈前后端分离博客项目 2.0 版本完结啦, 演示链接
小哈学Java
10
mica-mqtt 2.2.12 发布,简化 MqttClientTemplate 构造,方便自定义
一、简介mica-mqtt 基于 java aio 实现的简单、低延迟、高性能 的 mqtt 物联网开源组件。mica-mqtt 更加易于集成到已有服务和二次开发,降低自研物联网平台开发成本。二、功能支持 MQTT v3.1、v3.1.1 以及 v5.0 协议。支持 websocket mqtt 子
JAVA架构日记
0