# Redis redis: host: localhost port: 6379 password: #密码(没有密码可以不设置) database: 0 #连接0库 timeout: 0 #连接超时时间(毫秒) # 对应 RedisProperties.Jedis 内部类 jedis: pool: max-active: 8 # 连接池最大连接数,默认为 8 。使用负数表示没有限制。 max-idle: 8 # 默认连接数最小空闲的连接数,默认为 8 。使用负数表示没有限制。 min-idle: 0 # 默认连接池最小空闲的连接数,默认为 0 。允许设置 0 和 正数。 max-wait: -1 # 连接池最大阻塞等待时间,单位:毫秒。默认为 -1 ,表示不限制。
package cn.pet.service.thirdparty.redis; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; @Component public class RedisService { private static final Logger logger= LoggerFactory.getLogger(RedisService.class); @Autowired private StringRedisTemplate stringRedisTemplate; //定义方法,向redis存数据 public void setValue(String key,String value){ try{ ValueOperations<String, String> vo = stringRedisTemplate.opsForValue(); vo.set(key,value); logger.info("set redis success key={},value={}",key,value); }catch (Exception e){ logger.info("set redis fail e={}",e); } } //定义方法,向redis存数据,设置有效时间 public void setValue(String key,String value,long time){ try{ ValueOperations<String, String> vo = stringRedisTemplate.opsForValue(); //redis中time有效时间,使用秒表示 vo.set(key,value,time, TimeUnit.SECONDS); logger.info("set redis success key={},value={},time={}",key,value,time); }catch (Exception e){ logger.info("set redis fail e={}",e); } } //定义方法,获取有限时间 public Long getTime(String key){ try{ Long time = stringRedisTemplate.getExpire(key); logger.info("有效时间为{},:",time); return time; }catch (Exception e){ logger.info("没有有限时间"); return null; } } }
@Service @Transactional public class UmsSmsServiceImpl implements UmsSmsService { private static final Logger logger= LoggerFactory.getLogger(AliYunService.class); @Autowired private AliYunService aliYunService; @Autowired private RedisService redisService; @Override public Boolean sendSms(String phone, Integer codeType) { //需要生成验证码 String code = MathUtils.random(); logger.info("手机号码:{} 短信验证码为:{}",phone,code); String result = aliYunService.sendSms(phone, code); if (result.equals(Constants.Sms.ALI_SMS_RESULT)){ //短信验证码发送成功 //验证码存入redis //通过工具shengchengkey String key = UmsUtils.generateSmsRedisKey(codeType.toString(), phone); redisService.setValue(key,code,Constants.Time.ALI_SMS_EFFECTIVE_TIME); return true; } return false; } }
package cn.pet.controller; import cn.pet.constant.Constants; import cn.pet.result.Result; import cn.pet.result.ResultEnum; import cn.pet.result.ResultUtils; import cn.pet.service.UmsSmsService; import cn.pet.service.thirdparty.redis.RedisService; import cn.pet.utils.UmsUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/api/ums/sms") @RestController public class UmsSmsController { private static final Logger logger= LoggerFactory.getLogger(UmsSmsController.class); @Autowired private UmsSmsService umsSmsService; @Autowired private RedisService redisService; @PostMapping("/sendSms") public Result sendSms(String phone,Integer codeType){ //判断手机号 if (StringUtils.isEmpty(phone) | !UmsUtils.checkPhone(phone)){ return ResultUtils.returnResult(ResultEnum.FAIL_UMS_PHONE_ERROR); } if (!UmsUtils.checkCodeType(codeType)){ return ResultUtils.returnResult(ResultEnum.FAIL_UMS_SMS_CODE_TYPE_ERROR); } //判断验证码在redis中存放够不够一分钟 不够一分钟,不能调用sendSms,如果超过一分钟调用sendSms if (checkIsExpire(codeType,phone)){ logger.info("不能频繁发送验证码"); return ResultUtils.returnFail("一分钟之内不能重复发送验证码"); } //调用service接口 Boolean aBoolean = umsSmsService.sendSms(phone, codeType); if (aBoolean){ return ResultUtils.returnSuccess(); }else { return ResultUtils.returnFail(); } } private Boolean checkIsExpire(Integer codeType,String phone){ //从redis中获取有效时间 通过key String key = UmsUtils.generateSmsRedisKey(codeType.toString(), phone); Long time = redisService.getTime(key); if (time> Constants.Time.ALI_SMS_CHECK_TIME){ //表示验证码在redis存放不到一分钟 return true; } return false; } }