Springboot+Aop用注解实现阿里云短信验证码校验,校验通过自动删除验证码缓存
创始人
2024-12-27 04:09:05
0

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

@Getter @AllArgsConstructor public enum OperationType implements IEnum {     /**      * 注册      */     SIGN_UP(0),     /**      * 密码登录      */     LOGIN_BY_PWD(1),     /**      * 验证码登录      */     LOGIN_BY_SMS(2),     /**      * 忘记密码      */     FORGET_PWD(3),     /**      * 修改密码      */     MODIFY_PWD(4);      @JsonValue     private final int code;      @Override     public 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 = "用户名/手机号不能为空")     @IsMobile     private String username;     @NotBlank(message = "短信验证码不能为空")     private String code;      @Override     public String mobile() {         return this.getUsername();     }      @Override     public String smsCode() {         return this.getCode();     } }

5.添加aop切面类

@Aspect @Component public class SmsValidateAop {     @Autowired     private RedisTemplate 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     @NoArgsConstructor     private 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);         }     }

相关内容

热门资讯

6分钟发现!新九哥辅助(外挂透... 6分钟发现!新九哥辅助(外挂透视)原来有挂脚本(详细开挂软件)相信很多朋友都在电脑上玩过新九哥辅助吧...
三分钟辅助!老友广东潮汕麻雀,... 您好:这款指尖四川辅助脚本游戏是可以开挂的,确实是有挂的,很多玩家在这款指尖四川辅助脚本游戏中打牌都...
第7分钟了解!老友广东麻将辅助... 第7分钟了解!老友广东麻将辅助,微信小程序微乐辅助器教程,玩家教程(有挂解惑)-哔哩哔哩;微信小程序...
办法外挂!pokemmo手机版... 您好:这款pokemmo手机版修改器游戏是可以开挂的,确实是有挂的,很多玩家在这款pokemmo手机...
第三分钟开挂!奇迹手机辅助脚本... 第三分钟开挂!奇迹手机辅助脚本怎么下载(外挂透视)原来真的有挂脚本(关于开挂软件);奇迹手机辅助脚本...
1分钟详情!八闽状元郎竹简茶馆... 1分钟详情!八闽状元郎竹简茶馆辅助,三哥玩辅助器免费下载,AI教程(有挂教程)-哔哩哔哩;无需打开直...
第4分钟领会!上饶中至打炸漏洞... 第4分钟领会!上饶中至打炸漏洞,wepoker好友助力码,玩家教程(新版有挂)-哔哩哔哩;人气非常高...
手册外挂!德州透视插件,悦扑克... 【亲,德州透视插件 这款游戏可以开挂的,确实是有挂的,很多玩家在这款德州透视插件中打牌都会发现很多用...
1分钟科普!微乐挖坑内购破解(... 1分钟科普!微乐挖坑内购破解(外挂)原来是有挂安装(了解开挂辅助器);微乐挖坑内购破解软件透视开挂是...
第十分钟辅助!赣牌圈的好牌几率... 第十分钟辅助!赣牌圈的好牌几率,小程序微乐游戏辅助器,新2026版(新版有挂)-哔哩哔哩【无需打开直...