当前位置:网站首页>(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 对比
三、总结
边栏推荐
- 成功解决ModuleNotFoundError: No module named ‘Image‘
- "Code execution cannot continue because MSVCP140.dll was not found, reinstalling the program may resolve the problem, etc." Solutions
- @RequestBody、 @RequestParam 、 @PathVariable 和 @Vaild 注解
- 【Untitled】
- tcp协议传输中的粘包问题
- Compressing Deep Graph Neural Networks via Adversarial Knowledge Distillation
- 【2022-05-31】JS逆向之易企秀
- 2sk2225代换3A/1500V中文资料【PDF数据手册】
- Go语学习笔记 - gorm使用 - 表增删改查 Web框架Gin(八)
- Introducing the visualization tool Netron
猜你喜欢
通过对抗性知识蒸馏压缩深度图神经网络
2sk2225代换3A/1500V中文资料【PDF数据手册】
【微信小程序】小程序突破小程序二维码数量限制
Go语学习笔记 - gorm使用 - 表增删改查 Web框架Gin(八)
Excel basic study notes
【LeetCode】70. 爬楼梯 - Go 语言题解
EasyExcel comprehensive course combat
StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
TCP 连接 三次握手 四次挥手
2sk2225 Substitute 3A/1500V Chinese Documentation【PDF Data Book】
随机推荐
【MySQL】Mysql事务以及权限管理
网安学习-内网渗透3
mysql锁机制
CPM:A large-scale generative chinese pre-trained lanuage model
grub learning
StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
PhpMetrics usage
MySQL连接时出现2003错误
2022/07/30 学习笔记 (day20) 面试题积累
IJCAI2022教程 | 口语语言理解:最新进展和新领域
2022.7.30
QT 在父类中添加子类的流程,object tree,
正则表达式语法及使用
MySql统计函数COUNT详解
$\text{ARC 145}$
QT开发简介、命名规范、signal&slot信号槽
Introducing the visualization tool Netron
Regular expression syntax and usage
智能创意中的尺寸拓展模块
Successfully resolved ModuleNotFoundError: No module named 'Image'