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

Springboot+Aop用注解实现阿里云短信验证码校验,校验通过自动删除验证码缓存

1.新建操作类型枚举(这里的IEnum是我自定义的http请求拦截接口,不需要的话可以不用实现)

@Getter
@AllArgsConstructor
public enum OperationType implements IEnum<Integer> {/*** 注册*/SIGN_UP(0),/*** 密码登录*/LOGIN_BY_PWD(1),/*** 验证码登录*/LOGIN_BY_SMS(2),/*** 忘记密码*/FORGET_PWD(3),/*** 修改密码*/MODIFY_PWD(4);@JsonValueprivate final int code;@Overridepublic Integer getCode(){return code;}
}

2.新建校验注解

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SmsValidate {/*** 操作类型* @return*/OperationType operationType() default OperationType.LOGIN_BY_PWD;
}

3.创建验证码校验接口

public interface ISmsValidate {/*** 短信验证码手机号* @return*/String mobile();/*** 短信验证码内容* @return*/String smsCode();
}

4.controller方法请求参数,实现ISmsValidate接口

@Data
public class LoginBySmsDto implements ISmsValidate {@NotBlank(message = "用户名/手机号不能为空")@IsMobileprivate String username;@NotBlank(message = "短信验证码不能为空")private String code;@Overridepublic String mobile() {return this.getUsername();}@Overridepublic String smsCode() {return this.getCode();}
}

5.添加aop切面类

@Aspect
@Component
public class SmsValidateAop {@Autowiredprivate RedisTemplate<String,Object> redisTemplate;@Pointcut(value = "@annotation(com.tfyt.common.annotation.SmsValidate)")public void pointCut(){}@Before(value = "pointCut()")public void before(JoinPoint joinPoint){SmsObj smsObj = getSmsObj(joinPoint);Object cacheCode = redisTemplate.opsForValue().get(RedisKeyConstant.CACHE_SMS_CODE + smsObj.getOperationType() + ":" + smsObj.getMobile());BusinessAssert.notTrue(Objects.equals(cacheCode,smsObj.getCode()),"手机验证码不正确");}@AfterReturning(value = "pointCut()")public void after(JoinPoint joinPoint){SmsObj smsObj = getSmsObj(joinPoint);redisTemplate.delete(RedisKeyConstant.CACHE_SMS_CODE + smsObj.getOperationType() + ":" + smsObj.getMobile());}@Data@AllArgsConstructor@NoArgsConstructorprivate static class SmsObj{private String mobile;private String code;private Integer operationType;}private SmsObj getSmsObj(JoinPoint joinPoint){MethodSignature signature = (MethodSignature) joinPoint.getSignature();SmsValidate annotation = signature.getMethod().getAnnotation(SmsValidate.class);BusinessAssert.isNull(annotation,"系统异常:未查询到注解");BusinessAssert.isNull(annotation.operationType(),"系统异常:未配置操作类型");Object[] args = joinPoint.getArgs();ISmsValidate arg = null;if(args[0] instanceof ISmsValidate){arg = (ISmsValidate) args[0];}BusinessAssert.isNull(arg,"请输入用户名和手机验证码");return new SmsObj(arg.mobile(), arg.smsCode(), annotation.operationType().getCode());}
}

6.往controller的方法上注解

@SmsValidate(operationType = OperationType.LOGIN_BY_SMS)

重启springboot项目,调用接口即可生效,校验通过后会自动删除redis缓存

补一个自定义断言类

public class BusinessAssert {public static void notTrue(boolean condition, String msg){isTrue(!condition, msg);}public static void isTrue(boolean condition, String msg){if(condition){throw new BusinessException(HttpStatus.BAD_REQUEST.value(),msg);}}public static void nonNull(Object object,String msg){if(null!=object){throw new BusinessException(HttpStatus.BAD_REQUEST.value(),msg);}}public static void isNull(Object object,String msg){if(null==object){throw new BusinessException(HttpStatus.BAD_REQUEST.value(),msg);}}public static void isCollectionEmpty(Collection<?> collection,String msg){if(collection == null || collection.isEmpty()){throw new BusinessException(HttpStatus.BAD_REQUEST.value(),msg);}}public static void isCollectionNotEmpty(Collection<?> collection,String msg){if(collection != null && !collection.isEmpty()){throw new BusinessException(HttpStatus.BAD_REQUEST.value(),msg);}}

http://www.lryc.cn/news/400589.html

相关文章:

  • 无线物联网新时代,RFID拣货标签跟随潮流
  • Java8 根据List实体中一个字段去重取最大值,并且根据该字段进行排序
  • 微服务经纬:Eureka驱动的分布式服务网格配置全解
  • 关于前端数据库可视化库的选择,vue3+antd+g2plot录课计划
  • linux进行redis的安装并使用RDB进行数据迁移
  • 深入理解Scikit-learn:决策树与随机森林算法详解
  • AutoHotKey自动热键(十一)下载SciTE4AutoHotkey-Plus的中文增强版脚本编辑器
  • Halcon与C++之间的数据转换
  • MybatisPlus 一些技巧
  • 定制化服务发现:Eureka中服务实例偏好的高级配置
  • 【实战场景】MongoDB迁移的那些事
  • 为什么要使用加密软件?
  • k8s学习笔记——dashboard安装
  • AI艺术创作:掌握Midjourney和DALL-E的技巧与策略
  • 在Mac上免费恢复误删除的Word文档
  • HarmonyOS 屏幕适配设计
  • Netfilter之连接跟踪(Connection Tracking)和反向 SNAT(Reverse SNAT)
  • Linux下使用vs code离线安装各种插件
  • 【常见开源库的二次开发】基于openssl的加密与解密——Base58比特币钱包地址——算法分析(三)
  • Linux操作系统——数据库
  • 【数据结构与算法】希尔排序:基于插入排序的高效排序算法
  • 关于正点原子的alpha开发板的启动函数(汇编,自己的认识)
  • Deep Layer Aggregation【方法部分解读】
  • 大数据面试SQL题-笔记01【运算符、条件查询、语法顺序、表连接】
  • 零基础自学爬虫技术该从哪里开始入手?
  • CV11_模型部署pytorch转ONNX
  • Redis的使用(四)常见使用场景-缓存使用技巧
  • BERT架构的深入解析
  • 数字孪生技术如何助力低空经济飞跃式发展?
  • HTTP背后的故事:理解现代网络如何工作的关键(二)