Redis和短信验证码存储到redis

redis入门

安装:略 安装redis和链接redis的工具

1、导入jar包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、在application.yml中配置redis

spring:
    redis:
      host: localhost
      port: 6379

3、常见使用

package com.woniu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisController {
//    springboot只要发现导入了redis的jar包,就会自动创建一个操作redis的工具类redisTemplate,放容器中
    //jdbcTemplate
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/set")
    public String set(){
        redisTemplate.opsForValue().set("yzm","88888");
        return "success";
    }

    @RequestMapping("/get")
    public String get(){
        String yzm = redisTemplate.opsForValue().get("yzm");
        return yzm;

    }
}

4、在项目中使用

package com.woniu.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.woniu.entity.ResData;
import com.woniu.entity.ShortMes;
import com.woniu.entity.User;
import com.woniu.service.IUserService;
import com.woniu.thread.SendSmSThread;
import com.woniu.utils.MatchUtils;
import com.woniu.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
public class SmsController {

    @Autowired
    IUserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/getSmsYzm")
    public ResData getSmsYzm(String tel, HttpSession session) throws IOException {

        if(StrUtil.isBlank(tel))
        {
            return new ResData(10001,"手机号为空",null);
        }

        if(!MatchUtils.isChinaPhoneLegal(tel))
        {
            return new ResData(10002,"手机号格式有误",null);
        }

        //判断这个手机号,是否是本平台的手机号
        User user = userService.queryByTel(tel);
        if(user==null)
        {
            return new ResData(10003,"手机号未注册",null);
        }
        //生成随机的4位数字
        String randomNumbers = RandomUtil.randomNumbers(4);
        System.out.println("给"+tel+"发送信息:"+randomNumbers);
        //todo 验证码下周存redis里面
        //session.setAttribute(tel,randomNumbers);
        stringRedisTemplate.opsForValue().set(tel,randomNumbers);
        //给手机号发送随机值
        ShortMes shortMes = new ShortMes(tel,tel,randomNumbers);
        SendSmSThread sendSmSThread = new SendSmSThread(shortMes);
        new Thread(sendSmSThread).start();

        return new ResData(200,"发送成功",null);
    }
}

用户登录时,取出redis 中的值进行验证

@RequestMapping("/loginSms")
@ResponseBody
public ResData loginSms(@RequestBody User user,HttpSession session)
{
    String tel = user.getTel();
    String yzm = user.getYzm();

    if(StrUtil.isBlank(tel) || StrUtil.isBlank(yzm))
    {
        return new ResData(10001,"手机号或者验证码为空",null);
    }

    //String yzmSession = (String)session.getAttribute(tel);
    String yzmRedis = stringRedisTemplate.opsForValue().get(tel);
    if(!yzm.equals(yzmRedis))
    {
        return new ResData(10002,"验证码错误",null);
    }

    stringRedisTemplate.delete(tel);
    //session.removeAttribute(tel);//使用过后,马上把session中的验证码删除

    User userDb = userService.queryByTel(tel);

    session.setAttribute("user",userDb);
    return new ResData(200,"登录成功",userDb);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胖成范德彪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值