当前位置:网站首页>(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 对比
三、总结
边栏推荐
猜你喜欢
PyTorch模型导出到ONNX文件示例(LeNet-5)
Computer shortcut icon whitening solution
$\text{ARC 145}$
反转链表-就地逆置法
Apache Doris系列之:深入认识实时分析型数据库Apache Doris
VS2017 compile Tars test project
Ningbo Zhongning Pawn will transfer 29.5% of the equity for 2.8338 million yuan, and the owner's equity in 2021 will be 9.6875 million yuan
二进制序列
EasyExcel综合课程实战
MySQL索引常见面试题(2022版)
随机推荐
MySQL索引常见面试题(2022版)
10 个关于自动化发布管理的好处
OpenCV笔记(二十):滤波函数——filter2D
matlab标量场作图
【MySQL】MySQL中对数据库及表的相关操作
2022牛客暑期多校训练营1 J Serval and Essay
反转链表-头插反转法
Navicat cannot connect to mysql super detailed processing method
2021GDCPC Guangdong University Student Programming Competition B.Byfibonacci
Go1.18升级功能 - 泛型 从零开始Go语言
Learning about XML (1)
reindex win10
2022.7.28
PS Basic Learning (1)
ZZULIOJ: 1120: the most value to exchange
Day016 类和对象
Apache Doris series: In-depth understanding of real-time analytical database Apache Doris
mysql跨库关联查询(dblink)
Regular expression syntax and usage
IJCAI2022 Tutorial | Spoken Language Comprehension: Recent Advances and New Fields