在业务的实现过程中,尤其是对外接口开发,我们需要对请求进行大量的验证并返回错误状态码和描述。lombok 框架有很多很赞的注解,但是人家是throw一个异常,这与有些需求不一定能匹配。

该文将基于Solon的一些扩展基础,简单的实现一套定制的业务验证机制。效果如下:


@XController
public class UserController extends VerifyController{
@RepeatSubmit //重复提交验证
@Whitelist //IP白名单验证
@NotNull({"name", "mobile", "icon", "code"}) //非NULL验证
public void addUser(UserModel user){
//...
}
}

一、定制开始

1、先定义一组验证注解

@Inherited
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface NotNull {
String[] value();
} @Inherited
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Whitelist {
} //... 更多略

2、然后,定义一个验证用的拦截器

//
//Solon里的所有拦截器,也都是 XHandler
//
public class VerifyInterceptor implements XHandler {
@Override
public void handle(XContext ctx) throws Throwable {
//获取上下文中的XAction
//
XAction action = ctx.attr("action");
if (action != null) {
handle0(ctx, action);
}
} protected void handle0(XContext ctx, XAction action) throws Throwable {
//这里的顺序,要与业务的期望顺序匹配 //白名单
checkWhitelist(ctx, action); //不能为Null
checkNotNull(ctx, action); //...更多略
} protected void checkWhitelist(XContext ctx, XAction action) throws Throwable{
if(ctx.getHandled()){
return;
} Whitelist anno = action.method().getAnnotation(Whitelist.class);
if (anno != null) {
String ip = IpUtils.getIP(ctx);
if (WhitelistApi.existsOfServerIp(ip) == false) {
ctx.setHandled(true);
ctx.render(UapiCodes.CODE_16);
}
}
} protected void checkNotNull(XContext ctx, XAction action) throws Throwable{
if(ctx.getHandled()){
return;
} NotNull anno = action.method().getAnnotation(NotNull.class);
if (anno != null) {
checkParamsIsOk(ctx, false, anno.value());
}
}
}

3、再是,定义一个支持验证的控制器基类

//
// 创建一个有验证拦截器的基类;@XBefore 注解是可继承的...
//
@XBefore({VerifyInterceptor.class})
public class VerifyController { }

完工了

二、附:关于 XContext 的部分扩展属性

Solon 的上下文对象:XContext,有一组可扩展属性的接口:attr(), attrSet(), attrMap()。用于记录处理过程中的数据或对象。

以下是框架在运行过程中已记录的扩展属性:

扩展属性 说明
ctx.attr("controller") 获取当前控制器
ctx.attr("action") 获取当前活动
ctx.result 获取当前活动的执行结果,可用于统一的业务日志记录
ctx.attr("error") 获取当前错误
ctx.attr("output") 获取当前序列化输出,可用于统一的业务日志记录

Solon详解(六)- 定制业务级别的验证注解的更多相关文章

  1. Springboot mini - Solon详解(六)- Solon的校验框架使用、定制与扩展

    Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...

  2. Solon详解(六)- Solon的校验扩展框架使用与扩展

    Solon详解系列文章: Solon详解(一)- 快速入门 Solon详解(二)- Solon的核心 Solon详解(三)- Solon的web开发 Solon详解(四)- Solon的事务传播机制 ...

  3. Solon详解(八)- Solon的缓存框架使用和定制

    Solon详解系列文章: Solon详解(一)- 快速入门 Solon详解(二)- Solon的核心 Solon详解(三)- Solon的web开发 Solon详解(四)- Solon的事务传播机制 ...

  4. Springboot mini - Solon详解(八)- Solon的缓存框架使用和定制

    Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...

  5. Solon详解(九)- 渲染控制之定制统一的接口输出

    Solon详解系列文章: Solon详解(一)- 快速入门 Solon详解(二)- Solon的核心 Solon详解(三)- Solon的web开发 Solon详解(四)- Solon的事务传播机制 ...

  6. Solon详解(11)- Mybatis 与 Solon 相亲相爱

    Solon详解系列文章: Solon详解(一)- 快速入门 Solon详解(二)- Solon的核心 Solon详解(三)- Solon的web开发 Solon详解(四)- Solon的事务传播机制 ...

  7. Solon详解(十)- 怎么用 Solon 开发基于 undertow jsp tld 的项目?

    Solon详解系列文章: Solon详解(一)- 快速入门 Solon详解(二)- Solon的核心 Solon详解(三)- Solon的web开发 Solon详解(四)- Solon的事务传播机制 ...

  8. Springboot mini - Solon详解(四)- Solon的事务传播机制

    Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...

  9. Springboot mini - Solon详解(七)- Solon Ioc 的注解对比Spring及JSR330

    Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...

随机推荐

  1. shell 格式化数据,转换为execl

    awk '  BEGIN { OFS="\t"} ;{ $1=$1 ; print $8,$NF} ' >/root/log/aa.xlsx awk '  BEGIN { O ...

  2. CSRF与XSS攻击的原理与防范

    CSRF 1.概念与原理 CSRF,跨站请求伪造,攻击方伪装用户身份发送请求从而窃取信息或者破坏系统.例如: 用户访问A网站登陆并生成了cookie,再访问B网站,如果A网站存在CSRF漏洞,此时B网 ...

  3. Seaborn基础2

    import matplotlib.pyplot as plt import seaborn as sns import numpy as np def sinplot(flip = 1): x = ...

  4. 线程_使用multiprocessing启动一个子进程及创建Process 的子类

    from multiprocessing import Process import os # 子进程执行的函数 def run_proc(name): print("子进程运行中,名称:% ...

  5. PHP pow() 函数

    实例 pow() 的实例: <?phpecho(pow(2,4) . "<br>");echo(pow(-2,4) . "<br>" ...

  6. windows:根据特征码查找内核任意函数

    在windows平台做逆向.外挂等,经常需要调用很多未导出的内核函数,怎么方便.快速查找了?可以先用IDA等工具查看硬编码,再根据硬编码定位到需要调用的函数.整个思路大致如下: 1.先查找目标模块   ...

  7. Codechef June Challenge 2020 Division 1 记录

    目录 The Tom and Jerry Game! Operations on a Tuple The Delicious Cake Convenient Airports Guessing Gam ...

  8. git使用-标签管理

    1.查看所有的标签 git tag 2.创建标签 git tag  name 3.指定提交标签的信息 git tag -a name -m "comment" 4.删除标签 git ...

  9. 文件上传Upload 漏洞挖掘思路

    1:尽可能多的找出网站存在的上传点2:尝试使用如上各种绕过方法3:尝试 geshell4:无法上传webshel的情况下: 尝试上传html等,或可造成存储XSS漏洞 上传点构造XSS等,结合上传后的 ...

  10. vue实现自定义表格列

    在我们开发PC端的项目使用表单时,尤其是crm系统,应该经常会遇到这样的需求, 用户需要根据设置来自定义显示列. 查了element的官方文档, 并没有此类组件, 所以手动封装了一个简单的组件, 希望 ...