SpringBoot整合Redis
# SpringBoot整合Redis
# 验证码登录业务操作
# 1.业务流程图
# 2.准备静态页面
准备两个静态的页面做测试
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>手机验证码</title>
</head>
<body>
<h1>=============</h1>
<h1>= 手机验证码功能 =</h1>
<h1>=============</h1>
<form action="/user/login/phoneNum/codeId" method="post"><br>
手机号: <input type="text" name="phoneNum"><br>
验证码: <input type="text" name="codeId"><br>
发送验证码: <input type="submit" name="提交"><br>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>手机验证码</title>
</head>
<body>
<h1>=============</h1>
<h1>= 手机验证码功能 =</h1>
<h1>=============</h1>
<form action="/user/code/phoneNum/codeId" method="post"><br>
手机号: <input type="text" name="phoneNum"><br>
验证码: <input type="text" name="codeId"><br>
<input type="submit" name="提交"><br>
</form>
</body>
</html>
# 3.导入坐标和配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
server:
port: 8080
spring:
redis:
host: 192.168.0.123
port: 6379
jedis:
pool:
max-active: 50
mvc:
view:
prefix: /
suffix: .html
freemarker:
suffix: .html
template-loader-path: classpath:/static/
# 4.配置实体类
@Data
public class Code {
private String phoneNum;
private String codeId;
}
# 5. 配置业务层
public interface CodeService {
public String verifyCode(String phone);
public String getCode();
public Boolean getRedisCode(String phone,String code);
}
@Service
public class CodeServiceImpl implements CodeService {
/*
* 1.验证码只能每天发三次
* 2.验证码存放到Redis中
* 3.设置过期的时间
*/
public String countKey;
// 验证码
public String codeKey;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public String verifyCode(String phone) {
// 连接Redis
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
// 拼接key
// 手机发送的次数key
countKey = "VerifyCode"+ phone+ ":count";
// 验证码
codeKey = "VerifyCode"+phone+":code";
String count = ops.get(countKey);
if (count == null){
ops.set(countKey,"1");
}else if (Integer.parseInt(count) >2){
ops.increment(countKey);
}else if (Integer.parseInt(count) > 2) {
// 发送三次不能再发送了
System.out.println("今天发送次数已经超过了三次");
}
String vcode = getCode();
System.out.println("=========================");
System.out.println("验证码是: " + vcode);
ops.set(codeKey,vcode);
return vcode;
}
// 生成随机验证码
@Override
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;
}
@Override
public Boolean getRedisCode(String phone, String code) {
// 连接Redis
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
String redisCode = ops.get(codeKey);
// 判断
if (redisCode.equals(code)){
System.out.println("成功");
}else {
System.out.println("失败");
return false;
}
return true;
}
}
# 6.配置web层
@RestController
@RequestMapping("/user")
public class LoginController {
@Autowired
private CodeService codeService;
private Code checkCode = new Code();
@PostMapping("/login/{phoneNum}/{codeId}")
public ModelAndView check(@RequestParam(value = "phoneNum",required = false)String phoneNum,
@RequestParam(value = "codeId",required = false)String codeId){
System.out.println(phoneNum);
System.out.println(codeId);
// 调用业务层的获取验证码和存入Redis中
String verifyCode = codeService.verifyCode(phoneNum);
System.out.println(verifyCode);
// 将提交的数据更新到实体类中
checkCode = new Code();
checkCode.setCodeId(verifyCode);
checkCode.setPhoneNum(phoneNum);
// 创建视图解析器
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login");
return modelAndView;
}
@PostMapping("/code/{phoneNum}/{codeId}")
public String checkCodeIdAndPhoneNum(@RequestParam(value = "phoneNum",required = false)String phoneNum,
@RequestParam(value = "codeId",required = false)String codeId){
System.out.println(phoneNum);
System.out.println(codeId);
System.out.println(checkCode);
Boolean redisCode = codeService.getRedisCode(phoneNum, codeId);
if (!redisCode){
return "验证码错误";
}else {
return "登录成功";
}
}
}
上次更新: 2023/11/28, 22:03:59