当前位置: 首页 > news >正文

宝安做网站公司乐云seo亚洲足球最新排名

宝安做网站公司乐云seo,亚洲足球最新排名,做网站设计的论文中摘要怎么写,网站服务器基本要素Spring boot实现基于注解的aop面向切面编程 背景 从最开始使用Spring,AOP和IOC的理念就深入我心。正好,我需要写一个基于注解的AOP,被这个注解修饰的参数和属性,就会被拿到参数并校验参数。 一,引入依赖 当前sprin…

Spring boot实现基于注解的aop面向切面编程

背景

从最开始使用Spring,AOP和IOC的理念就深入我心。正好,我需要写一个基于注解的AOP,被这个注解修饰的参数和属性,就会被拿到参数并校验参数。

一,引入依赖

当前spring boot版本为2.7.18,我引入的aspectjweaver版本为1.9.5。

     <dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.5</version></dependency>

二,写注解

这个注解用来修饰方法,相当于一个切点,加上这个注解,这个方法就被被aop执行。

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ParamInterceptor {boolean checkParam() default true;
}

在这里插入图片描述
这个注解是用来修饰参数和属性的。被修饰的参数和属性在aop中会通过反射取出数据并判断

@Target({ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface VerifyParam {int min() default -1;int max() default -1;boolean required() default false;VerifyRegexEnum regex() default VerifyRegexEnum.NO;
}

这个是用到的VerifyRegexEnum 枚举类

@AllArgsConstructor
@Getter
public enum VerifyRegexEnum {NO("", "不校验"),IP("^((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})$", "ip地址"),POSITIVE_INTEGER("^\\d+$", "正整数"),NUMBER_LETTER_UNDER_LINE("\\w+$", "数字字母下划线"),PHONE("^1[3-9]\\d{9}$", "手机号"),EMAIL("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", "邮箱"),ID_CARD("^\\d{17}(\\d|x|X)$", "身份证"),PASSWORD("^[a-zA-Z0-9]{6,20}$", "密码"),MONEY("^\\d+(\\.\\d+)?$", "金额"),;private String regex;private String desc;public static boolean isMatch(String regex, String str) {return str.matches(regex);}}

三,编写切面aspect

该切面类就实现了,对参数和属性的校验。

@Aspect
@Component
@Slf4j
public class CdesAspect {@Before("@annotation(com.cdes.annotation.ParamInterceptor)")public void before(JoinPoint point) {Object[] args = point.getArgs();Method method = ((MethodSignature) point.getSignature()).getMethod();log.info("参数校验,方法名:{}",method.getName());ParamInterceptor paramInterceptor = method.getAnnotation(ParamInterceptor.class);if (paramInterceptor == null ) {return;}if(!paramInterceptor.checkParam()){return;}try{checkParam(method,args);}catch (Exception e){throw new BizException("400",e.getMessage());}}private void checkParam(Method method, Object[] args) throws IllegalAccessException {Parameter[] parameters = method.getParameters();for (int i = 0; i < parameters.length; i++) {Parameter parameter = parameters[i];Object paramValue = args[i];VerifyParam paramAnnotation = parameter.getAnnotation(VerifyParam.class);//取方法参数上的注解,如果有,则校验参数,如果没有,取该参数的属性,看是否有该注解if (paramAnnotation != null) {if(paramAnnotation.required() && paramValue == null){throw new BizException( "400",parameter.getName()+":参数不能为空");}if(paramAnnotation.max() != -1){if (paramValue.toString().length() > paramAnnotation.max()) {throw new BizException( "400",parameter.getName()+":参数长度不能超过"+paramAnnotation.max());}}if(paramAnnotation.min() != -1){if (paramValue.toString().length() < paramAnnotation.min()) {throw new BizException( "400",parameter.getName()+":参数长度不能小于"+paramAnnotation.min());}}if(paramAnnotation.regex() != null){if(!VerifyRegexEnum.isMatch(paramAnnotation.regex().getRegex(),paramValue.toString())){throw new BizException( "400",parameter.getName()+":参数不符合正则表达式");}}}else{//取属性上的注解,看是否需要校验Field[] fields = parameter.getClass().getDeclaredFields();if(fields == null || fields.length == 0){continue;}for (Field field : fields) {field.setAccessible(true);VerifyParam ann = field.getAnnotation(VerifyParam.class);if(ann == null){continue;}Object filedValue = field.get(paramValue);if(ann.required() && filedValue == null){throw new BizException( "400",field.getName()+":参数不能为空");}if(ann.max() != -1){if (filedValue.toString().length() > ann.max()) {throw new BizException( "400",field.getName()+":参数长度不能超过"+ann.max());}}if(ann.min() != -1){if (filedValue.toString().length() < ann.min()) {throw new BizException( "400",field.getName()+":参数长度不能小于"+ann.min());}}if(ann.regex() != null){if(!VerifyRegexEnum.isMatch(ann.regex().getRegex(),filedValue.toString())){throw new BizException( "400",field.getName()+":参数不符合正则表达式");}}}}}}
}

四,使用

controller中的login方法加上了ParamInterceptor注解,然后这个方法的入参LoginDTO中的属性加上了VerifyParam注解
在这里插入图片描述

@Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginDTO {@VerifyParam(regex = VerifyRegexEnum.PHONE)private String phone;@VerifyParam(regex = VerifyRegexEnum.PASSWORD)private String password;@VerifyParam(required = true)private String verifyCode;@VerifyParam(required = true)private String codeId;
}

总结

经过测试,校验参数功能正常。可以看到,spring boot的aop功能使用起来还是相当简单的。

http://www.hrbkazy.com/news/31916.html

相关文章:

  • 阿里云主机 搭建网站泰安百度推广公司
  • 可以做网络推广的网站福州百度分公司
  • 免费自己创建个人网站网络营销的特点包括
  • 做网站如何月入10万建立免费网站
  • 2017招远网站建设佛山seo代理计费
  • 免费注册网站空间网站建设情况
  • 凡科可以做淘宝客网站吗西安网站维护公司
  • 工商局官网登录seo网站建设公司
  • 玛多县网站建设公司什么叫做seo
  • 股权分配方案创业初期新手怎么入行seo
  • 网站建设行业咨讯文章排名轻松seo 网站推广
  • 儋州网站建设制作百度大数据中心
  • 福州营销型网站建设百度seo排名优化助手
  • 做推广用那个网站吗百度一下电脑版首页
  • 网站怎么做访客收藏链接做网站企业
  • wordpress 滑块河北网站优化公司
  • 网站开发与维护 专业站长工具seo综合查询访问
  • 大连品牌官网建站优化推广网站排名
  • 免费b站推广网站复制码百度竞价开户联系方式
  • 做网站视频下载网络营销的特点是什么?
  • 看动漫是怎么做视频网站中国网络优化公司排名
  • 网站建设报价模块不要手贱搜这15个关键词
  • 网站有信心做的更好seo建站网络公司
  • 程序员帮人做黑彩网站百度搜索词排名
  • 珠海市网站建设公司国家认可的赚钱软件
  • h5和网站的区别源码之家
  • 上海做网站谁好新的网络推广方式
  • 门户网站盈利百度网站登录
  • 微信公众号配置 网站建设韶关今日头条新闻
  • 佛山怎么做网站自己制作网页的网站