基于spring-aop对springmvc的参数的验证和处理
2017-07-17 00:00
661 查看
#背景介绍
#项目介绍
#项目使用
避免重复处理参数问题,避免处理参数时产生的耦合,避免参数的安全性。 此项目是看到有网友这样处理参数问题(具体什么平台忘记了), 然后在其基础上增加了一些安全处理和更加符合项目使用的处理,同时也能单独作为jar使用,但是并没有上传到maven中央仓库。 其核心代码大部分并没有改变,这也是尊重作者。
#项目介绍
项目基于spring-aop拦截controller接口,前提需要使用@RequestMapping和@ValidateGroup注解接口,这样才能被拦截,注解定义:
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface ValidateGroup { ValidateField[] fields(); }
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface ValidateField { /** * 参数索引位置 */ int index(); /** * 参数名,主要用作当字段验证错误返回信息使用 */ String paramName(); /** * 如果参数是基本数据类型或String ,就不用指定该参数,如果参数是对象,要验证对象里面某个属性,就用该参数指定属性名 */ String filedName() default ""; /** * XSS 漏洞防控 */ boolean encodeXSS() default false; /** * 正则验证 */ String regStr() default ""; /** * 是否能为空 , 为true表示不能为空 , false表示能够为空 */ boolean notNull() default false; /** * 最大长度 , 用户验证字符串 , 大于当前设置值 */ int maxLen() default -1; /** * 最小长度 , 用户验证字符串 ,小于当前设置值 */ int minLen() default -1; /** * 最大值 ,用于验证数字类型数据 , 大于当前设置值 */ int maxVal() default -1; /** * 最小值 ,用于验证数值类型数据 ,小于当前设置值 */ int minVal() default -1; }
同时还有一个aop处理类ValidateAspectHandel,代码详情请看项目。
#项目使用
在自己的项目中引入/jar/XXX.jar包,由于技术原因,并没有上传到开源库,也可以自己下载项目运行获得jar; 然后创建一个类ParamValidate,继承ValidateAspectHandel,重写error(),该方法作为参数处理失败后的处理; 同时需要使用@Component和@Aspect注解类ParamValidate,两个注解的使用说明请自行百度。
@Component @Aspect public class ParamValidate extends ValidateAspectHandel { @Override public Object error(Method method, String errorMsg) throws Exception { //得到方法返回值类型 Class returnType = method.getReturnType(); if (returnType == BaseResponse.class) { //如果返回值为String return new BaseResponse("1", errorMsg); } else if (returnType == ModelAndView.class) { //返回错误页面 return new ModelAndView("redirect:" + value("projectAdminLogin")); } else { //当使用Ajax的时候 可能会出现这种情况,请在拦截的接口方法最后一个参数使用HttpServletResponse HttpServletResponse res = (HttpServletResponse) args[args.length - 1]; res.setCharacterEncoding("UTF-8"); res.setContentType("application/json;charset=utf-8"); res.setStatus(200); PrintWriter printWriter = res.getWriter(); printWriter.println("{\"code\": 1, \"message\": \"" + errorMsg + "!\"}"); printWriter.close(); return null; } } }
在springmvc容器配置中开启aop:
<aop:aspectj-autoproxy proxy-target-class="true"/>
定义测试接口:
@ValidateGroup(fields = { @ValidateField(index = 0, filedName = "user.name", encodeXSS = true, paramName = "用户名称", notNull = true), @ValidateField(index = 0, filedName = "user.address", encodeXSS = true, paramName = "用户地址", notNull = true), @ValidateField(index = 2, encodeXSS = true, paramName = "测试", notNull = true) }) @RequestMapping(value = "/user", method = RequestMethod.POST, produces = "application/json;") public BaseResponse insertUser(User user, Boolean isManager, String cs) throws Exception { return toSuccess("update_successful"); }
git地址:https://git.oschina.net/jie_r/validate
相关文章推荐
- SpringMVC Spring3 MVC 注解,注释 用@RequestMapping处理请求,多个请求,提交,.do,带参数,url重写
- 基于xml配置的spring aop 的实现日志 和为通知传递参数
- spring-aop中around切面中处理参数
- 基于 Annotation 拦截的 Spring AOP 权限验证方法
- 使用SpringAop 验证方法参数是否合法
- 基于spring自动注入及AOP的表单二次提交验证
- Spring AOP 处理通知中的参数【Spring 入门】
- 基于SpringAop 的统一验证
- SpringMVC Spring3 MVC 注解,注释 用@RequestMapping处理请求,多个请求,提交,.do,带参数,url重写
- spring Aop 实现对http请求参数的验证,及对功能权限的验证
- 基于 Annotation 拦截的 Spring AOP 权限验证方法
- 通过 Spring AOP 验证方法的参数是否合法
- 用AOP拦截自定义注解并获取注解属性与上下文参数(基于Springboot框架)
- SpringMVC Spring3 MVC 注解,注释 用@RequestMapping处理请求,多个请求,提交,.do,带参数,url重写
- 使用SpringAop 验证方法参数是否合法
- SpringMVC数据验证(AOP处理Errors和方法验证)
- SpringMVC或SpringBoot请求时间参数报错处理方法
- 基于 Annotation 拦截的 Spring AOP 权限验证方法
- 使用Spring AOP预处理Controller的参数
- 【SpringBoot】表单验证和使用AOP处理请求