使用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;
@GetMapping("/getVerifyFour") public BaseResponse<VerifyCodeResp> getVerifyFour() { String captchaKey = UUID.randomUUID().toString(); LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100, 40, 4, 100); stringRedisTemplate.opsForValue().set(VERIFY_CODE_KEY + captchaKey, lineCaptcha.getCode(), 30L, TimeUnit.MINUTES); String base64String = ""; try { 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 {
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
|
@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;
private String captchaKey;
private String captcha; }
|