# Validate **Repository Path**: jie_r/validate ## Basic Information - **Project Name**: Validate - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2017-06-19 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #背景介绍 避免重复处理参数问题,避免处理参数时产生的耦合,避免参数的安全性。 此项目是看到有网友这样处理参数问题(具体什么平台忘记了), 然后在其基础上增加了一些安全处理和更加符合项目使用的处理,同时也能单独作为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: ``` ``` 定义测试接口: ``` @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"); } ```