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()));
    }
相关推荐
qinyia7 分钟前
WisdomSSH如何高效检查服务器状态并生成运维报告
linux·运维·服务器·数据库·人工智能·后端·ssh
IT_陈寒8 分钟前
Python开发者必知的5个高效技巧,让你的代码性能提升50%
前端·人工智能·后端
q***25110 分钟前
Spring容器的开启与关闭
java·后端·spring
q***448112 分钟前
java进阶--多线程学习
java·开发语言·学习
q***017712 分钟前
SpringBoot实战(三十二)集成 ofdrw,实现 PDF 和 OFD 的转换、SM2 签署OFD
spring boot·后端·pdf
0***m82215 分钟前
Maven Spring框架依赖包
java·spring·maven
艾斯比的日常17 分钟前
Neo4j 完全指南:从核心特性到 Java 实战(附企业级应用场景)
java·开发语言·neo4j
K***430618 分钟前
三大框架-Spring
java·spring·rpc
q***965819 分钟前
springboot3整合knife4j详细版,包会!(不带swagger2玩)
android·前端·后端
风象南20 分钟前
Spring Boot模板引擎在后端开发中的实战应用
后端