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);
}