使用springboot+mybatis-plus+redis完成用户登录系统
一、使用springboot+mybatis-plus+redis完成用户登录系统,
数据库表 users
字段名称 |
中文 |
类型 |
长度 |
主键 |
外键 |
自增 |
约束 |
uid |
用户id |
Int |
|
Y |
|
Y |
|
User_name |
用户名 |
varchar |
255 |
|
|
|
|
password |
用户密码 |
varchar |
255 |
|
|
|
|
如果3分钟内,失败三次,则要求30分钟后才可以再次登录
思路:用户登录就是查询用户表,用户密码都对应上即为登录成功,如果登录失败则产生一个有效时间是3分钟的缓存,累计达到3个缓存时,30分钟内不允许再次登录。
新建一个moduel
引入的依赖,记得引入非关系型数据库redis的依赖
pom.xml文件里面再导入一个mybatis-plus,jedis,thymeleaf模板等依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xzit</groupId> <artifactId>RedisLogin</artifactId> <version>0.0.1-SNAPSHOT</version> <name>RedisLogin</name> <description>RedisLogin</description> <properties> <java.version>11</java.version> </properties> <dependencies> <!--jedis依赖--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>4.0.1</version> </dependency> <!--thymeleaf模板--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--redis依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--mybatis-plus依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <!--springboot--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--springboot热启动--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!--Mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--springtest--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
配置application.yml文件
#数据库连接 spring: datasource: url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8 username: root password: zengyu1234 #开发环境下,关闭缓存,生产环境下,打开缓存 thymeleaf: cache: false web: resources: static-locations: classpath:/static #配置redis的端口和虚拟机地址 redis: port: 6379 host: 192.168.44.4 client-type: lettuce #配置mybatis-plus mybatis-plus: #实体类别名 type-aliases-package: com.xzit.entity #日志打印 configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
如果没有数据库连接就新建一个
使用mybatisX逆向生成users表相关的类
编写UsersController
package com.xzit.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.xzit.entity.Users; import com.xzit.service.UsersService; import lombok.extern.slf4j.Slf4j; import org.apache.catalina.User; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import redis.clients.jedis.Jedis; import javax.annotation.Resource; @RestController @RequestMapping("/users") public class UsersController { @Resource private UsersService service; @GetMapping("/login") public ModelAndView login(@RequestParam String uname, @RequestParam String pwd){ ModelAndView modelAndView= new ModelAndView("index.html"); //实例化一个jedis对象 Jedis jedis = new Jedis("192.168.44.4",6379); //收到一次用户名视为登录一次,统计登录次数 String count_key="v:"+uname+":count"; String s=jedis.get(count_key); //使用wrapper来封装用户名密码到数据库查询,有结果就登录成功,否则登录失败 QueryWrapper<Users> wrapper = new QueryWrapper<>(); wrapper.eq("uname",uname) .eq("pwd",pwd); Users u = service.getOne(wrapper); if (u!=null){ System.out.println("用户登录成功"+u.getUname()); }else if (s==null){ //如果没有成功,新增一个有效时间是30s的缓存,为了方便只设置了30s,30分钟失效正常应该设置1800s jedis.setex(count_key,30,"1"); System.out.println("登录失败"); }else if (Integer.parseInt(s)<3){//如果登录尝试不足3次,每登录1次,记数增加1 jedis.incr(count_key); System.out.println("登录失败"+Integer.parseInt(s)+"次,超过3次将锁定30分钟"); }else { System.out.println("已失败3次,30分钟后再试"); jedis.close(); } return modelAndView; } }
为了前端界面好看点,引入一个bootstrap.min.css
简单的弄一个登录界面
<!DOCTYPE html> <html lang="zh" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" type="text/css" th:href="@{/bootstrap.min.css}"> </head> <body> <div class="container" style="margin-top: 30px"> <div>系统登录</div> <form method="get" action="/users/login"> <div class="form-group"> <label for="uname">用户名</label> <input type="text" name="uname" class="form-control" id="uname" placeholder="请输入账号"> </div> <div class="form-group"> <label for="pwd">密码</label> <input type="text" name="pwd" class="form-control" id="pwd" placeholder="请输入密码"> </div> <button type="submit" class="btn btn-primary">提交</button> </form> </div> </body> </html>
最终实现效果: