SpringBoot-自定义注解,拦截器

创建自定义注解和拦截器,并使用拦截器去拦截带有自定义注解的类或方法。

创建自定义注解

创建自定义注解并增加一些属性

java 复制代码
package com.shore.my_spring_demo.common.annoation;

import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Verification {
    int flag() default 0;
}

创建拦截器

创建拦截器拦截带有该注解的方法

java 复制代码
package com.shore.my_spring_demo.common.interceptor;

import com.shore.my_spring_demo.common.annoation.Verification;
import com.shore.my_spring_demo.common.enums.VerificationFlagEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

@Slf4j
@Component
public class VerificationInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        return HandlerInterceptor.super.preHandle(request, response, handler);
        if (handler instanceof HandlerMethod handlerMethod) {
            Method method = handlerMethod.getMethod();
            if (method.isAnnotationPresent(Verification.class)) {
                Verification annotation = method.getAnnotation(Verification.class);
                VerificationFlagEnum.getByCode(annotation.flag());
                switch (VerificationFlagEnum.getByCode(annotation.flag())) {
                    case NO_VER -> {
                        log.info("跳过校验");
                    }
                    case LOGIN_VER -> {
                        log.info("登陆校验");
                    }
                    case TOKEN_VER -> {
                        log.info("token校验");
                    }
                    case PERMISSION_VER -> {
                        log.info("权限校验");
                    }
                    default -> {
                    }
                }
                return true;
            }
        }
        return true;
    }
}
java 复制代码
package com.shore.my_spring_demo.common.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum VerificationFlagEnum {
    LOGIN_VER(1, "登陆校验"),
    TOKEN_VER(2, "token 校验"),
    PERMISSION_VER(3, "权限校验"),

    NO_VER(0, "跳过校验"),
    UNKNOWN(999, "未知校验"),

    ;

    private final int code;
    private final String value;

    public static VerificationFlagEnum getByCode(int code) {
        for (VerificationFlagEnum flagEnum : VerificationFlagEnum.values()) {
            if (flagEnum.getCode() == code) {
                return flagEnum;
            }
        }
        return UNKNOWN;
    }
}

配置拦截器

将拦截器注册到 Spring Boot 的拦截链中

java 复制代码
package com.shore.my_spring_demo.common.configure;

import com.shore.my_spring_demo.common.interceptor.VerificationInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class MyConfig implements WebMvcConfigurer {
    @Resource
    private VerificationInterceptor verificationInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(verificationInterceptor);
    }
}

验证

java 复制代码
@Verification(flag = 3)
    @PostMapping("/query")
    public ApiResponse<UserVO> queryUser(@RequestBody UserReq req) {
        System.out.println(req);
        return ApiResponse.success(userService.getUserById(req.getId()));
    }
相关推荐
道友可好14 小时前
AI 测试全绿,代码却是错的
前端·人工智能·后端
布朗克16815 小时前
34 JVM深入理解
java·jvm
Flittly15 小时前
【AgentScope Java新手村系列】(4)结构化输出
java·spring boot·spring·ai
techdashen15 小时前
Rust 基础设施团队 2025 Q4 回顾与 2026 Q1 计划
开发语言·后端·rust
何以解忧,唯有..15 小时前
Python 中的继承机制:从基础到高级用法详解
java·开发语言·python
Yiyaoshujuku15 小时前
化合物数据集API接口(数据结构及样例)
java·网络·数据结构
神奇小汤圆15 小时前
互联网大厂精选面试八股文(附2026最新Java+AI高频题)| 建议收藏
后端
春天花会开13115 小时前
影像上传前置机网络架构设计模板(含VPN)
后端·架构
程序员cxuan15 小时前
Fable 5 的系统提示词被人扒出来了,精彩,太精彩了。
人工智能·后端·程序员
神奇小汤圆15 小时前
颠覆认知:Java 打破双亲委派 ≠ 彻底废弃双亲委派模型
后端