当前位置:网站首页>(WebFlux)001、如何自定义注解实现功能
(WebFlux)001、如何自定义注解实现功能
2022-07-30 22:56:00 【InfoQ】
一、背景
二、正文
2.1 MVC 实现方案
HandlerInterceptorHandlerInterceptor#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.RequestMappingHandlerMapping2.3 对比
三、总结
边栏推荐
- PhpMetrics 使用
- TCP 连接 三次握手 四次挥手
- 力扣题(2)—— 两数相加
- “蔚来杯“2022牛客暑期多校训练营2 H.Take the Elevator
- proemthues 服务发现配置
- Successfully resolved ModuleNotFoundError: No module named 'Image'
- MySQL的一个问题
- “蔚来杯“2022牛客暑期多校训练营4 DHKLN
- ML之shap:基于FIFA 2018 Statistics(2018年俄罗斯世界杯足球赛)球队比赛之星分类预测数据集利用RF随机森林+计算SHAP值单样本力图/依赖关系贡献图可视化实现可解释性之攻略
- Chapter 8 Intermediate Shell Tools II
猜你喜欢

Introducing the visualization tool Netron

ML's shap: Based on FIFA 2018 Statistics (2018 Russia World Cup) team match star classification prediction data set using RF random forest + calculating SHAP value single-sample force map/dependency c

反转链表-就地逆置法

Go语学习笔记 - gorm使用 - gorm处理错误 Web框架Gin(十)

482-静态库、动态库的制作、使用及区别

QT 在父类中添加子类的流程,object tree,

关于XML的学习(一)

Golang go-redis cluster模式下不断创建新连接,效率下降问题解决

【2022-05-31】JS逆向之易企秀

抽象类和接口(学习笔记)
随机推荐
MySQL联合查询(多表查询)
vulnhub靶机AI-Web-1.0渗透笔记
“蔚来杯“2022牛客暑期多校训练营4 N.Particle Arts 规律 方差
反转链表-头插反转法
"NIO Cup" 2022 Nioke Summer Multi-School Training Camp 2 H.Take the Elevator
Chapter 8 Intermediate Shell Tools II
Golang go-redis cluster模式下不断创建新连接,效率下降问题解决
win10重建索引
[MySQL] Mysql transaction and authority management
Reverse linked list - head insertion inversion method
HF2022-EzPHP复现
【无标题】
IDEA usage skills
Py's pdpbox: a detailed introduction to pdpbox, installation, and case application
IJCAI2022教程 | 口语语言理解:最新进展和新领域
Abstract classes and interfaces (study notes)
基于 Docker Compose 的 Nacos(MySQL 持久化)的搭建
Apache Doris系列之:安装与部署详细步骤
Py之pdpbox:pdpbox的简介、安装、案例应用之详细攻略
VS2017 compile Tars test project