六、Redis模拟手机发送验证码demo
1、导入依赖
-
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.2.0</version> </dependency>
2、编写application.properties配置文件
-
#redis连接地址 spring.redis.host=127.0.0.1 #redis连接端口 spring.redis.port=6379 #redis使用第几个数据库 spring.redis.database=3 #redis连接超时时间 spring.redis.timeout=1800000 #redis密码 spring.redis.password= # 连接池最大连接数(使用负值表示没有限制) spring.redis.jedis.pool.max-active=20 # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.jedis.pool.max-wait=-1 # 连接池中的最大空闲连接 spring.redis.jedis.pool.max-idle=5 # 连接池中的最小空闲连接 spring.redis.jedis.pool.min-idle=0
3、编写配置类
-
@Configuration public class RedisConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.timeout}") private int timeout; @Value("${spring.redis.password}") private String password; @Value("${spring.redis.database}") private int database; @Value("${spring.redis.jedis.pool.max-active}") private int maxActive; @Value("${spring.redis.jedis.pool.max-idle}") private int maxIdle; @Value("${spring.redis.jedis.pool.min-idle}") private int minIdle; @Value("${spring.redis.jedis.pool.max-wait}") private long maxWaitMillis; @Bean public JedisPool redisPoolFactory(){ JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); jedisPoolConfig.setMaxTotal(maxActive); jedisPoolConfig.setMinIdle(minIdle); if(password==null || "".equals(password.trim())) { password=null; } JedisPool jedisPool = new JedisPool(jedisPoolConfig,host,port,timeout,password,database); return jedisPool; } }
4、编写工具类
-
@Component public class PhoneCodeUtils { @Autowired private JedisPool jedisPool; /** * 发送验证码 * @param phone 手机号 */ public void sentVerifyCode(String phone){ Jedis jedis = jedisPool.getResource(); //手机发送次数key String countKey = "VerifyCode" + phone + ":count"; //验证码key String codeKey = "VerifyCode" + phone + ":code"; //获得手机发送次数 String count = jedis.get(codeKey); //如果为空就设置过期时间,初始化1 if(Objects.isNull(count)) { jedis.setex(codeKey, 24*60*60, "1"); //如果小于3就加1 } else if(Integer.parseInt(codeKey) <= 2) { jedis.incr(countKey); //否则就发送不成功 } else if(Integer.parseInt(count) > 2) { System.out.println("今天已经发送验证码超过三次"); jedis.close(); } //最后设置验证码,如果已经超过三次就无法设置 String verifyCode = getCode(); jedis.setex(codeKey, 120, verifyCode); jedis.close(); } /** * 检验验证码 * @param phone 手机号 * @param code 用户输入验证码 */ public void checkVerifyCode(String phone, String code) { Jedis jedis = jedisPool.getResource(); //验证码key String codeKey = "VerifyCode" + phone + ":code"; //从redis获得验证码 String redisCode = jedis.get(codeKey); //判断 if(redisCode.equals(code)) { System.out.println("成功"); }else { System.out.println("失败"); } jedis.close(); } //生成6位数字验证码 public String getCode() { Random random = new Random(); String code = ""; for(int i=0;i<6;i++) { int rand = random.nextInt(10); code += rand; } return code; } }
5、测试
-
发送验证码
-
@Test public void sentVerifyCodeTest(){ //模拟用户手机号 String phone = "123456789"; System.out.println("开始发送验证码"); phoneCodeUtils.sentVerifyCode(phone); }
-
操作响应结果:
-
-
检验验证码
-
@Test public void checkVerifyCodeTest(){ //模拟用户手机号 String phone = "123456789"; System.out.println("开始检验验证码"); phoneCodeUtils.checkVerifyCode(phone,"123456"); System.out.println("==============="); phoneCodeUtils.checkVerifyCode(phone,"540600"); }
-
操作响应结果:
-