SpringBoot整合redis并实现输入密码错误限制登录功能

需求:
实现一个登录功能,并且2分钟之内只能输入5次错误密码,若输入五次之后还没有输入正确密码,系统将会将该账号锁定1小时

pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

<!--        jedis 包-->
        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.1.0</version>
        </dependency>
<!--springboot整合redis包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-configuration-processor</artifactId>-->
<!--            <optional>true</optional>-->
<!--        </dependency>-->



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

yml配置

#redis
redis:
  #redis机器ip
  hostname: 127.0.0.1
  #redis端口
  port: 6379
  #redis密码
  password:
  #redis超时时间(毫秒),如果不设置,取默认值2000
  timeout: 10000
  #最大空闲数
  maxIdle: 300
  #连接池的最大数据库连接数。设为0表示无限制,如果是jedis 2.4以后用redis.maxTotal
  #maxActive=600
  #控制一个pool可分配多少个jedis实例,用来替换上面的redis.maxActive,如果是jedis 2.4以后用该属性
  maxTotal: 1000
  #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
  maxWaitMillis: 1000
  #连接的最小空闲时间 默认1800000毫秒(30分钟)
  minEvictableIdleTimeMillis: 300000
  #每次释放连接的最大数目,默认3
  numTestsPerEvictionRun: 1024
  #逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
  timeBetweenEvictionRunsMillis: 30000
  #是否
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,那我来回答你的问题。在SpringBoot中,实现点赞功能通常会使用Redis进行缓存,可以将点赞数存储到Redis中,每当一个用户进行点赞操作时,就会将点赞数从Redis中读取,进行加1操作,再将结果存储回Redis中。具体实现步骤如下: 1. 首先,在pom.xml文件中添加Redis的依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在application.properties文件中配置Redis连接信息: ``` spring.redis.host=127.0.0.1 spring.redis.port=6379 ``` 3. 创建一个Redis工具类,用于封装一些Redis操作: ``` @Component public class RedisUtil { @Autowired private StringRedisTemplate redisTemplate; public void increment(String key) { redisTemplate.opsForValue().increment(key); } public String get(String key) { return redisTemplate.opsForValue().get(key); } public void set(String key, String value) { redisTemplate.opsForValue().set(key, value); } } ``` 4. 在点赞接口中,调用RedisUtil中封装的方法,完成点赞功能的实现: ``` @RestController public class LikeController { @Autowired private RedisUtil redisUtil; @PostMapping("/like") public void like(@RequestParam("id") Integer id) { String key = "like:" + id; redisUtil.increment(key); } @GetMapping("/like") public String getLike(@RequestParam("id") Integer id) { String key = "like:" + id; return redisUtil.get(key); } } ``` 以上就是SpringBoot整合Redis实现点赞功能的具体步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值