通常我们注册或者登录都可以通过手机号,邮箱进行验证码的验证登录,本篇文章基于QQ邮箱和Redis模拟简单的验证码验证,具体业务可以根据需求进行升级。通过Random随机生成6位数字,设置过期时间为60s,通过QQ邮件进行发送验证码内容。使用SpringBoot作为脚手架搭建简单模拟环境,Redis运行在虚拟机上,QQ邮箱自行申请POP3/SMTP服务。
1:Redis配置
1.1:在Linux上开启Redis服务
[root@LKCentos01 ~]# redis-server /etc/redis.conf
[root@LKCentos01 ~]# redis-cli
127.0.0.1:6379> quit
1.2:查看Linux的IP地址
1.3:创建SpringBoot工程配置Redis
1.3.1:引入依赖
<!--SpringBoot整合redis-->
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.X集成redis所需common-pool2-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
1.3.2:application.yaml配置
新建application.yaml配置Redis
spring:
redis:
#Redis服务器地址 此处的host为通过ifconfig命令获取的linux的IP地址
host: 192.168.111.128
#Redis服务器连接端口
port: 6379
#Redis数据库索引(默认为0)
database: 0
#Redis连接超时时间(毫秒)
timeout: 1800000
#配置Redis连接池
lettuce:
pool:
#Redis连接池的最大连接数(负数表示无限制)
max-active: 20
#Redis最大阻塞等待时间(负数表示没有限制)
max-wait: -1
#Redis连接池中的最大空闲连接数
max-idle: 5
#Redis连接池中的最小空闲连接数
min-idle: 0
1.3.3:Redis配置类
新建RedisConfig配置Redis
@EnableCaching //开启redis操作
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
//创建RedisTemplate类
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setConnectionFactory(factory);
//key序列化方式
template.setKeySerializer(redisSerializer);
//value序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap序列化
template.setHashValueSerializer(jackson2JsonRedisSerializer);
return template;
}
//创建缓存管理
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//解决查询缓存转换异常的问题
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置序列化(解决乱码的问题),过期时间600秒
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(600))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
return cacheManager;
}
}
2:申请QQ邮箱服务
3:配置QQ邮箱
3.1:引入依赖
<!--发送邮件服务-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
3.2:配置mail
spring:
mail:
username: #申请服务的邮箱,也就是发送邮件的邮箱
password: #刚才申请服务产生的授权码
host: smtp.qq.com #默认
default-encoding: UTF-8 #默认
properties: #默认
mail:
smtp:
auth: true
starttls:
enable: true
required: true
4:实现
4.1:自动装配
//自动装配RedisTemplate
@Autowired
private RedisTemplate redisTemplate;
//自动装配JavaMailSender
@Autowired
private JavaMailSender mailSender;
4.2:生成验证码存入Redis
//随机生成6位数字
public String getCode(){
Random random=new Random();
StringBuilder stringBuilder=new StringBuilder();
for(int i=0;i<6;i++){
stringBuilder.append(random.nextInt(10));
}
//将随机得到的验证码存入redis中设置过期时间为60s
redisTemplate.opsForValue().set("RedisCode", stringBuilder.toString(), 60, TimeUnit.SECONDS);
return stringBuilder.toString();
}
4.3:发送邮件
//发送邮件
@GetMapping("/email")
public String sendEmail(){
SimpleMailMessage massage=new SimpleMailMessage();
massage.setFrom("发送邮箱");
massage.setTo("收件邮箱");
massage.setSubject("头信息");
massage.setText(getCode());//发送内容为验证码
mailSender.send(massage);
return "index";
}
4.4:测试
发送邮件成功:
Redis存储成功: