当前位置:网站首页>(WebFlux)001、如何自定义注解实现功能
(WebFlux)001、如何自定义注解实现功能
2022-07-30 22:56:00 【InfoQ】
一、背景
二、正文
2.1 MVC 实现方案
HandlerInterceptor
HandlerInterceptor#preHandle()
/**
* <p>校验权限</p>
*
* @author [email protected]
* @since 2022/7/24
*/
@Target({ElementType.TYPE, ElementType.METHOD}) // 可用于方法和类上
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CheckPermission {
boolean check() default true;
}
/**
* <p>拦截器</p>
*
* @author [email protected]
* @since 2022/7/24
*/
@Component
public class CheckPermissionInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod methodHandle = (HandlerMethod) handler;
CheckPermission permission = methodHandle.getMethodAnnotation(CheckPermission.class);
if (Objects.isNull(permission)) {
// handler 所有bean 上面寻找
permission = AnnotationUtils.findAnnotation(methodHandle.getBeanType(), CheckPermission.class);
}
if (Objects.nonNull(permission)) {
// TODO do something..
}
}
return true;
}
}
2.2 WebFlux 实现方案
HandlerInterceptor
/**
* <p>WebFlux实现方案</p>
*
* @author [email protected]
* @since 2022/7/24
*/
@Component
public class CheckPermissionWebFilter implements WebFilter {
@Autowired
// 关键,通过RequestMappingHandlerMapping 我们可以获取到MethodHandler
private RequestMappingHandlerMapping handlerMapping;
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return handlerMapping.getHandler(exchange).switchIfEmpty(chain.filter(exchange))
.flatMap(handler -> {
// 熟悉的味道,将handler转换成HandlerMethod
if (handler instanceof HandlerMethod) {
HandlerMethod methodHandle = (HandlerMethod) handler;
CheckPermission permission = methodHandle.getMethodAnnotation(CheckPermission.class);
if (Objects.isNull(permission)) {
// handler 所有bean 上面寻找
permission = AnnotationUtils.findAnnotation(methodHandle.getBeanType(), CheckPermission.class);
}
if (Objects.nonNull(permission)) {
// TODO do something..
}
}
return chain.filter(exchange);
}
);
}
}
org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping
2.3 对比
三、总结
边栏推荐
猜你喜欢
el-upload添加请求头
# # yyds dry goods inventory interview will brush TOP101: to determine whether there is a part of the list
[MySQL] Related operations on databases and tables in MySQL
微软商店出现【0x800706D9】解决方法
MySql统计函数COUNT详解
Go语学习笔记 - gorm使用 - 表增删改查 Web框架Gin(八)
2022/07/30 学习笔记 (day20) 面试题积累
Py之pdpbox:pdpbox的简介、安装、案例应用之详细攻略
$\text{ARC 145}$
【LeetCode】70. 爬楼梯 - Go 语言题解
随机推荐
ZZULIOJ:1120: 最值交换
2sk2225 Substitute 3A/1500V Chinese Documentation【PDF Data Book】
【CTF】buuctf web 详解(持续更新)
Successfully solved ImportError: always import the name '_validate_lengths'
Computer shortcut icon whitening solution
Achievements of Science and Technology (31)
EasyExcel comprehensive course combat
“蔚来杯“2022牛客暑期多校训练营2 H.Take the Elevator
HF2022-EzPHP reproduction
Apache Doris系列之:深入认识实时分析型数据库Apache Doris
Py's pdpbox: a detailed introduction to pdpbox, installation, and case application
Apache Doris series: detailed steps for installation and deployment
正则表达式语法及使用
Golang go-redis cluster模式下不断创建新连接,效率下降问题解决
Detailed operator
Go的Gin框架学习
智能创意中的尺寸拓展模块
2021GDCPC Guangdong University Student Programming Competition B.Byfibonacci
Alibaba Cloud video on demand + project combat
The problem of sticky packets in tcp protocol transmission