源代码,欢迎拿走学习-用redis缓存实现用户登录验证码存储

@Service
@Transactional
public class LoginSmsServiceImpl implements LoginSmsService {

    @Autowired
    RedisTemplate redisTemplate;

    /**
     *
     * @param phone 电话号码
     */
    @Override
    public void SmsVerification(String phone) {
        //判断手机号是否发送过验证码
        if(redisTemplate.hasKey("timeout_"+phone)){
            throw new RuntimeException("已经发送验证码了,请稍后重试");
        }
        //随机4位验证码
        String code = RandomStringUtils.randomNumeric(4);
        try {
            //发送验证码
//            SMSUtils.sendMsg(phone, code);
            //验证码存入redis
            redisTemplate.opsForValue().set("phone_"+phone, code,120, TimeUnit.SECONDS);
            //超时时间
            redisTemplate.opsForValue().set("timeout_"+phone, "true",60, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
            throw  new RuntimeException("发送验证码失败,请稍后重试!");
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/m0_66194642/article/details/122470259#comments_20070061
今日推荐