自学内容网 自学内容网

SpringBoot-自定义注解,拦截器

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

创建自定义注解

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

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;
}

创建拦截器

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

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;
    }
}

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 的拦截链中

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);
    }
}

验证

@Verification(flag = 3)
    @PostMapping("/query")
    public ApiResponse<UserVO> queryUser(@RequestBody UserReq req) {
        System.out.println(req);
        return ApiResponse.success(userService.getUserById(req.getId()));
    }


原文地址:https://blog.csdn.net/qq_35201802/article/details/143782854

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!