当前位置:网站首页>拦截器通过自定义注解来判断是否拦截
拦截器通过自定义注解来判断是否拦截
2022-08-06 01:40:00 【xixihaha_coder】
拦截器通过自定义注解来判断是否拦截
自定义一个注解,又有拦截器,方法上只要有这个注解,就不进行拦截,放行
1.自定义注解
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AuthAccess {
}
2.拦截器配置
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
/*...*/
if(!(handler instanceof HandlerMethod)){
return true;
}else {
HandlerMethod h = (HandlerMethod) handler;
AuthAccess authAccess = h.getMethodAnnotation(AuthAccess.class);
if(authAccess!=null){
return true;
}
}
/*...*/
}
}
3.配置
@Configuration
public class WebConfig implements WebMvcConfigurer{
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor())
.addPathPatterns("/index/**")
.excludePathPatterns("/admin","/admin/login");
}
}
4.controller层使用
@RestController
public class IndexController {
@AuthAccess
@GetMapping("/index")
public Result index(){
return Result.success("index");
}
}
边栏推荐
猜你喜欢
随机推荐
Kubernetes证书过期怎么玩
deque(双端数组)——STL
miniPCIe接口CAN卡为工控机快速扩展CAN通道
案例|工业物联网解决方案•工业互联网云平台
第十八天笔记
小白兔的颜色
剑指offer专项突击版第21天
Kubernetes Cilium展示
剑指offer第24题(反转链表)
阿洛的感悟
Convert the children array to a one-bit array
Using a shell script: Shut down a specific process
淀粉与纤维素
多线程-实现方式
typescript77 - Create ts-enabled projects in CRA
PyTorch笔记 - Attention Is All You Need (3)
typescript72 - Existing type declaration files (type files for third-party libraries)
It's not my fault that programmers write programs
BugKu:Web
新版本的特性









