1.配置文件(注意格式呢!!!)
redis:
host: 127.0.0.1
port: 6379
# password: 123456
pool:
max-idle: 8
min-idle: 0
max-active: 8
max-wait: 1000
timeout: 0
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/97dfa62b82cab5fc73f5a5d5910ddd65.png)
2.存:
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
@Autowired
RedisTemplate redisTemplate;
/**
* 存到redis里面
*/
ValueOperations valueOperations = redisTemplate.opsForValue();
String key = "register_user:send_mobile_"+tel;
valueOperations.set(key,random,30, TimeUnit.MINUTES);
//下面这三句话可以不要的呀
redisTemplate.hasKey(key);
valueOperations.get(key);
logger.info("[mobile][code]:{}=={}",tel,random);
3.取了验证
/**
* 验证 手机号的验证码
* @param tel
* @param random
* @return
*/
@Override
public void validCode(String tel,String random) {
String key = "register_user:send_mobile_"+tel;
if (!redisTemplate.hasKey(key)) {
throw new ServiceException("验证信息已失效,请重新登录");
}
String code = (String) redisTemplate.opsForValue().get(key);
if(!StringUtils.equals(code,random)){
throw new ServiceException("验证码不匹配");
}
System.out.println(tel+"---"+random+"---"+"验证码验证成功");
}