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()));
    }
相关推荐
cipher2 小时前
ERC-4626 通胀攻击:DeFi 金库的"捐款陷阱"
前端·后端·安全
毅航2 小时前
自然语言处理发展史:从规则、统计到深度学习
人工智能·后端
JxWang052 小时前
Task04:字符串
后端
树獭叔叔3 小时前
10-让模型更小更聪明,学而不忘:知识蒸馏与持续学习
后端·aigc·openai
JxWang053 小时前
Task02:链表
后端
只会cv的前端攻城狮4 小时前
Elpis-Core — 融合 Koa 洋葱圈模型实现服务端引擎
前端·后端
codetown4 小时前
2026年Zig编程语言权威指南:从系统级底层架构到现代软件工程实践
后端·程序员
cg336 小时前
cc-connect,十分钟帮你把 claude code 连接到微信,飞书,钉钉等等平台
后端·openai
用户1427868669326 小时前
Java多态的底层真相:JVM到底怎么知道该调哪个方法?(面试高频)
后端