feat(新增): 生成验证码信息
This commit is contained in:
parent
486802e293
commit
c27f4f482b
|
@ -1,17 +1,16 @@
|
|||
package com.atguigu.spzx.manger.controller;
|
||||
|
||||
import com.atguigu.spzx.manger.service.SysUserService;
|
||||
import com.atguigu.spzx.manger.service.ValidateCodeService;
|
||||
import com.atguigu.spzx.model.dto.system.LoginDto;
|
||||
import com.atguigu.spzx.model.vo.result.Result;
|
||||
import com.atguigu.spzx.model.vo.result.ResultCodeEnum;
|
||||
import com.atguigu.spzx.model.vo.system.LoginVo;
|
||||
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Tag(name = "用户接口")
|
||||
@RestController
|
||||
|
@ -19,6 +18,8 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
public class IndexController {
|
||||
@Autowired
|
||||
private SysUserService sysUserService;
|
||||
@Autowired
|
||||
private ValidateCodeService validateCodeService;
|
||||
|
||||
@Operation(summary = "登录请求", description = "登录请求实现")
|
||||
@PostMapping("login")
|
||||
|
@ -26,4 +27,11 @@ public class IndexController {
|
|||
LoginVo vo = sysUserService.login(loginDto);
|
||||
return Result.success(vo, ResultCodeEnum.SUCCESS);
|
||||
}
|
||||
|
||||
@Operation(summary = "生成验证码", description = "生成验证码信息")
|
||||
@GetMapping("generateValidateCode")
|
||||
public Result<ValidateCodeVo> generateValidateCode() {
|
||||
ValidateCodeVo vo = validateCodeService.generateValidateCode();
|
||||
return Result.success(vo, ResultCodeEnum.SUCCESS);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
package com.atguigu.spzx.manger.service;
|
||||
|
||||
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
|
||||
|
||||
public interface ValidateCodeService {
|
||||
/**
|
||||
* 生成验证码信息
|
||||
*
|
||||
* @return 验证码响应结果实体类
|
||||
*/
|
||||
ValidateCodeVo generateValidateCode();
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.atguigu.spzx.manger.service.impl;
|
||||
|
||||
import cn.hutool.captcha.CaptchaUtil;
|
||||
import cn.hutool.captcha.CircleCaptcha;
|
||||
import com.atguigu.spzx.manger.service.ValidateCodeService;
|
||||
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Service
|
||||
public class ValidateCodeServiceImpl implements ValidateCodeService {
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
/**
|
||||
* 生成验证码信息
|
||||
*
|
||||
* @return 验证码响应结果实体类
|
||||
*/
|
||||
@Override
|
||||
public ValidateCodeVo generateValidateCode() {
|
||||
// 1. 通过工具生成验证码
|
||||
CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(150, 48, 4, 2);
|
||||
String code = captcha.getCode();
|
||||
String base64Image = "data:image/png;base64," + captcha.getImageBase64();
|
||||
// 2. 验证码存储到Redis中,Redis的key为UUID,值为验证码的值
|
||||
String key = UUID.randomUUID().toString();
|
||||
// 3. 返回ValidateCodeVo
|
||||
redisTemplate.opsForValue().set(key, code, 1, TimeUnit.MINUTES);
|
||||
return ValidateCodeVo.builder().codeKey(key).codeValue(base64Image).build();
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -1,16 +1,20 @@
|
|||
package com.atguigu.spzx.model.vo.system;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Schema(description = "验证码响应结果实体类")
|
||||
public class ValidateCodeVo {
|
||||
|
||||
@Schema(description = "验证码key")
|
||||
private String codeKey ; // 验证码的key
|
||||
private String codeKey; // 验证码的key
|
||||
|
||||
@Schema(description = "验证码value")
|
||||
private String codeValue ; // 图片验证码对应的字符串数据
|
||||
|
||||
private String codeValue; // 图片验证码对应的字符串数据
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue