栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot集成redis模拟手机发送验证码进行验证

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot集成redis模拟手机发送验证码进行验证

1.配置redis依赖

因为springboot框架帮们集成了大部分的依赖和它自动配置类的特点,我们只需要在maven中配置后就可以使用了,极大的挺高了我们开发的效率!

       
            org.springframework.boot
            spring-boot-starter-data-redis
        

2.配置yaml文件

我这里使用的是本地redis服务,所以使用的是本地ip地址,即localhost即可;

如果是虚拟机上配置的redis服务则只需要修改ip地址即可(如果配置了端口号或密码则也要重新配置)

 redis:
    port: 6379
    host: localhost
        
     

3.创建redisConfig配置类

@Configuration
public class RedisConfig {
    @Bean
    //绑定yaml配置
    @ConfigurationProperties(prefix = "redis")
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        //创建RedisTemplate模板
        RedisTemplate template = new RedisTemplate<>();
        //关联redisConnectionFactory
        template.setConnectionFactory(redisConnectionFactory);
        //创建序列化类
        GenericToStringSerializer genericToStringSerializer = new GenericToStringSerializer(Object.class);
        //序列化类,对象映射设置
        //设置value的转化格式和key的转化格式
        template.setValueSerializer(genericToStringSerializer);
        template.setKeySerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
}

4.前端部分,一个简单的测试表单