hutool验证码使用

使用hutool + redis实现登录场景的验证

验证码控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.aixbox.aioj.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import com.aixbox.aioj.common.BaseResponse;
import com.aixbox.aioj.common.ErrorCode;
import com.aixbox.aioj.common.ResultUtils;
import com.aixbox.aioj.exception.BusinessException;
import com.aixbox.aioj.model.dto.verify.MatchCodeReq;
import com.aixbox.aioj.model.vo.VerifyCodeResp;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static com.aixbox.aioj.utils.RedisConstants.VERIFY_CODE_KEY;

/**
* 验证码工具类
*/
@RestController
public class VerifyCodeController {
@Resource
private StringRedisTemplate stringRedisTemplate;

/**
* LineCaptcha
* 图片的base64编码字符串
* redis存储
*
* @return String
*/
@GetMapping("/getVerifyFour")
public BaseResponse<VerifyCodeResp> getVerifyFour() {
String captchaKey = UUID.randomUUID().toString();
//定义图形验证码的长、宽、验证码字符数、干扰线宽度
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100, 40, 4, 100);
// 存入redis并设置过期时间为30分钟
stringRedisTemplate.opsForValue().set(VERIFY_CODE_KEY + captchaKey, lineCaptcha.getCode(), 30L, TimeUnit.MINUTES);
String base64String = "";
try {
//返回 base64
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(lineCaptcha.getImage(), "PNG", bos);
byte[] bytes = bos.toByteArray();
Base64.Encoder encoder = Base64.getEncoder();
base64String = "data:image/png;base64," + encoder.encodeToString(bytes);
} catch (Exception e) {
e.printStackTrace();
}
VerifyCodeResp verifyCodeResp = new VerifyCodeResp();
verifyCodeResp.setCaptchaKey(captchaKey);
verifyCodeResp.setCaptchaImg(base64String);
return ResultUtils.success(verifyCodeResp);
}
}

VerifyCodeResp类

1
2
3
4
5
6
7
8
9
10
11
12
13
@Data
public class VerifyCodeResp implements Serializable {
/**
* 验证码缓存的key
*/
private String captchaKey;

/**
* 验证码图片
*/
private String captchaImg;
}

在登录时使用

用户登录方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 用户登录
*
* @param userLoginRequest
* @param request
* @return
*/
@PostMapping("/login")
public BaseResponse<Map<String, Object>> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
if (userLoginRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}


String s = stringRedisTemplate.opsForValue().get(VERIFY_CODE_KEY + userLoginRequest.getCaptchaKey());
stringRedisTemplate.delete(VERIFY_CODE_KEY + userLoginRequest.getCaptchaKey());
if (!userLoginRequest.getCaptcha().toLowerCase().equals(s)){
throw new BusinessException(ErrorCode.PARAMS_ERROR, "验证码错误");
}



String userAccount = userLoginRequest.getUserAccount();
String userPassword = userLoginRequest.getUserPassword();
if (StringUtils.isAnyBlank(userAccount, userPassword)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Map<String, Object> loginUserVO = userService.userLogin(userAccount, userPassword, request);
return ResultUtils.success(loginUserVO);
}

UserLoginRequest类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Data
public class UserLoginRequest implements Serializable {

private static final long serialVersionUID = 3191241716373120793L;

private String userAccount;

private String userPassword;

//验证码缓存的key
private String captchaKey;

//用户输入的验证码
private String captcha;
}