您的位置:首页 > 财经 > 金融 > 军事最新消息今天_域名租赁网站_网站推广 方法_sem和seo哪个工作好

军事最新消息今天_域名租赁网站_网站推广 方法_sem和seo哪个工作好

2024/11/20 20:17:41 来源:https://blog.csdn.net/qq_35201802/article/details/143782854  浏览:    关键词:军事最新消息今天_域名租赁网站_网站推广 方法_sem和seo哪个工作好
军事最新消息今天_域名租赁网站_网站推广 方法_sem和seo哪个工作好

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

创建自定义注解

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

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 {@Overridepublic 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 {@Resourceprivate VerificationInterceptor verificationInterceptor;@Overridepublic 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()));}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com