当前位置:   article > 正文

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

springboot 验证码

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

  1. @Getter
  2. @AllArgsConstructor
  3. public enum OperationType implements IEnum<Integer> {
  4. /**
  5. * 注册
  6. */
  7. SIGN_UP(0),
  8. /**
  9. * 密码登录
  10. */
  11. LOGIN_BY_PWD(1),
  12. /**
  13. * 验证码登录
  14. */
  15. LOGIN_BY_SMS(2),
  16. /**
  17. * 忘记密码
  18. */
  19. FORGET_PWD(3),
  20. /**
  21. * 修改密码
  22. */
  23. MODIFY_PWD(4);
  24. @JsonValue
  25. private final int code;
  26. @Override
  27. public Integer getCode(){
  28. return code;
  29. }
  30. }

2.新建校验注解

  1. @Target({ElementType.METHOD})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface SmsValidate {
  5. /**
  6. * 操作类型
  7. * @return
  8. */
  9. OperationType operationType() default OperationType.LOGIN_BY_PWD;
  10. }

3.创建验证码校验接口

  1. public interface ISmsValidate {
  2. /**
  3. * 短信验证码手机号
  4. * @return
  5. */
  6. String mobile();
  7. /**
  8. * 短信验证码内容
  9. * @return
  10. */
  11. String smsCode();
  12. }

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

  1. @Data
  2. public class LoginBySmsDto implements ISmsValidate {
  3. @NotBlank(message = "用户名/手机号不能为空")
  4. @IsMobile
  5. private String username;
  6. @NotBlank(message = "短信验证码不能为空")
  7. private String code;
  8. @Override
  9. public String mobile() {
  10. return this.getUsername();
  11. }
  12. @Override
  13. public String smsCode() {
  14. return this.getCode();
  15. }
  16. }

5.添加aop切面类

  1. @Aspect
  2. @Component
  3. public class SmsValidateAop {
  4. @Autowired
  5. private RedisTemplate<String,Object> redisTemplate;
  6. @Pointcut(value = "@annotation(com.tfyt.common.annotation.SmsValidate)")
  7. public void pointCut(){}
  8. @Before(value = "pointCut()")
  9. public void before(JoinPoint joinPoint){
  10. SmsObj smsObj = getSmsObj(joinPoint);
  11. Object cacheCode = redisTemplate.opsForValue().get(RedisKeyConstant.CACHE_SMS_CODE + smsObj.getOperationType() + ":" + smsObj.getMobile());
  12. BusinessAssert.notTrue(Objects.equals(cacheCode,smsObj.getCode()),"手机验证码不正确");
  13. }
  14. @AfterReturning(value = "pointCut()")
  15. public void after(JoinPoint joinPoint){
  16. SmsObj smsObj = getSmsObj(joinPoint);
  17. redisTemplate.delete(RedisKeyConstant.CACHE_SMS_CODE + smsObj.getOperationType() + ":" + smsObj.getMobile());
  18. }
  19. @Data
  20. @AllArgsConstructor
  21. @NoArgsConstructor
  22. private static class SmsObj{
  23. private String mobile;
  24. private String code;
  25. private Integer operationType;
  26. }
  27. private SmsObj getSmsObj(JoinPoint joinPoint){
  28. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  29. SmsValidate annotation = signature.getMethod().getAnnotation(SmsValidate.class);
  30. BusinessAssert.isNull(annotation,"系统异常:未查询到注解");
  31. BusinessAssert.isNull(annotation.operationType(),"系统异常:未配置操作类型");
  32. Object[] args = joinPoint.getArgs();
  33. ISmsValidate arg = null;
  34. if(args[0] instanceof ISmsValidate){
  35. arg = (ISmsValidate) args[0];
  36. }
  37. BusinessAssert.isNull(arg,"请输入用户名和手机验证码");
  38. return new SmsObj(arg.mobile(), arg.smsCode(), annotation.operationType().getCode());
  39. }
  40. }

6.往controller的方法上注解

@SmsValidate(operationType = OperationType.LOGIN_BY_SMS)

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

补一个自定义断言类

  1. public class BusinessAssert {
  2. public static void notTrue(boolean condition, String msg){
  3. isTrue(!condition, msg);
  4. }
  5. public static void isTrue(boolean condition, String msg){
  6. if(condition){
  7. throw new BusinessException(HttpStatus.BAD_REQUEST.value(),msg);
  8. }
  9. }
  10. public static void nonNull(Object object,String msg){
  11. if(null!=object){
  12. throw new BusinessException(HttpStatus.BAD_REQUEST.value(),msg);
  13. }
  14. }
  15. public static void isNull(Object object,String msg){
  16. if(null==object){
  17. throw new BusinessException(HttpStatus.BAD_REQUEST.value(),msg);
  18. }
  19. }
  20. public static void isCollectionEmpty(Collection<?> collection,String msg){
  21. if(collection == null || collection.isEmpty()){
  22. throw new BusinessException(HttpStatus.BAD_REQUEST.value(),msg);
  23. }
  24. }
  25. public static void isCollectionNotEmpty(Collection<?> collection,String msg){
  26. if(collection != null && !collection.isEmpty()){
  27. throw new BusinessException(HttpStatus.BAD_REQUEST.value(),msg);
  28. }
  29. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/853189
推荐阅读
相关标签
  

闽ICP备14008679号