mirror of
https://github.com/201206030/novel.git
synced 2025-04-27 07:30:50 +00:00
perf: 优化用户注册流程
This commit is contained in:
parent
a1da483eeb
commit
1fcd085820
@ -1,10 +1,9 @@
|
||||
package io.github.xxyopen.novel.controller.front;
|
||||
|
||||
import io.github.xxyopen.novel.core.common.resp.RestResp;
|
||||
import io.github.xxyopen.novel.core.common.util.IpUtils;
|
||||
import io.github.xxyopen.novel.core.constant.ApiRouterConsts;
|
||||
import io.github.xxyopen.novel.service.FileService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import io.github.xxyopen.novel.dto.resp.ImgVerifyCodeRespDto;
|
||||
import io.github.xxyopen.novel.service.ResourceService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@ -13,24 +12,25 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 文件相关 控制器
|
||||
* 资源(图片/视频/文档)相关 控制器
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/17
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(ApiRouterConsts.API_FRONT_FILE_URL_PREFIX)
|
||||
@RequestMapping(ApiRouterConsts.API_FRONT_RESOURCE_URL_PREFIX)
|
||||
@RequiredArgsConstructor
|
||||
public class FileController {
|
||||
public class ResourceController {
|
||||
|
||||
private final FileService fileService;
|
||||
private final ResourceService resourceService;
|
||||
|
||||
/**
|
||||
* 获取图片验证码接口
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("imgVerifyCode")
|
||||
public RestResp<String> getImgVerifyCode(HttpServletRequest request) throws IOException {
|
||||
return RestResp.ok(fileService.getImgVerifyCode(IpUtils.getRealIp(request)));
|
||||
public RestResp<ImgVerifyCodeRespDto> getImgVerifyCode() throws IOException {
|
||||
return resourceService.getImgVerifyCode();
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package io.github.xxyopen.novel.controller.front;
|
||||
|
||||
import io.github.xxyopen.novel.core.common.resp.RestResp;
|
||||
import io.github.xxyopen.novel.core.common.util.IpUtils;
|
||||
import io.github.xxyopen.novel.core.constant.ApiRouterConsts;
|
||||
import io.github.xxyopen.novel.core.constant.SystemConfigConsts;
|
||||
import io.github.xxyopen.novel.core.util.JwtUtils;
|
||||
@ -10,7 +9,6 @@ import io.github.xxyopen.novel.dto.req.UserLoginReqDto;
|
||||
import io.github.xxyopen.novel.dto.req.UserRegisterReqDto;
|
||||
import io.github.xxyopen.novel.dto.resp.UserLoginRespDto;
|
||||
import io.github.xxyopen.novel.service.UserService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@ -34,8 +32,7 @@ public class UserController {
|
||||
* 用户注册接口
|
||||
*/
|
||||
@PostMapping("register")
|
||||
public RestResp<String> register(@Valid UserRegisterReqDto dto, HttpServletRequest request) {
|
||||
dto.setUserKey(IpUtils.getRealIp(request));
|
||||
public RestResp<String> register(@Valid UserRegisterReqDto dto) {
|
||||
return userService.register(dto);
|
||||
}
|
||||
|
||||
|
@ -53,9 +53,9 @@ public class ApiRouterConsts {
|
||||
public static final String USER_URL_PREFIX = "/user";
|
||||
|
||||
/**
|
||||
* 文件模块请求路径前缀
|
||||
* 资源(图片/视频/文档)模块请求路径前缀
|
||||
* */
|
||||
public static final String FILE_URL_PREFIX = "/file";
|
||||
public static final String RESOURCE_URL_PREFIX = "/resource";
|
||||
|
||||
/**
|
||||
* 前台门户首页API请求路径前缀
|
||||
@ -78,8 +78,8 @@ public class ApiRouterConsts {
|
||||
public static final String API_FRONT_USER_URL_PREFIX = API_FRONT_URL_PREFIX + USER_URL_PREFIX;
|
||||
|
||||
/**
|
||||
* 前台门户文件相关API请求路径前缀
|
||||
* 前台门户资源(图片/视频/文档)相关API请求路径前缀
|
||||
*/
|
||||
public static final String API_FRONT_FILE_URL_PREFIX = API_FRONT_URL_PREFIX + FILE_URL_PREFIX;
|
||||
public static final String API_FRONT_RESOURCE_URL_PREFIX = API_FRONT_URL_PREFIX + RESOURCE_URL_PREFIX;
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package io.github.xxyopen.novel.dto.req;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import lombok.Data;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
/**
|
||||
* 用户注册 请求DTO
|
||||
@ -25,8 +26,10 @@ public class UserRegisterReqDto {
|
||||
private String velCode;
|
||||
|
||||
/**
|
||||
* 请求用户标识,用来表明图形验证码属于哪个用户
|
||||
* 请求会话标识,用来标识图形验证码属于哪个会话
|
||||
* */
|
||||
private String userKey;
|
||||
@NotBlank
|
||||
@Length(min = 32,max = 32)
|
||||
private String sessionId;
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
package io.github.xxyopen.novel.dto.resp;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 图像验证码 响应DTO
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/18
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class ImgVerifyCodeRespDto {
|
||||
|
||||
/**
|
||||
* 当前会话ID,用于标识改图形验证码属于哪个会话
|
||||
* */
|
||||
private String sessionId;
|
||||
|
||||
/**
|
||||
* Base64 编码的验证码图片
|
||||
* */
|
||||
private String img;
|
||||
|
||||
}
|
@ -27,10 +27,10 @@ public class VerifyCodeManager {
|
||||
/**
|
||||
* 生成图片验证码,并放入缓存中
|
||||
*/
|
||||
public String genImgVerifyCode(String userKey) throws IOException {
|
||||
public String genImgVerifyCode(String sessionId) throws IOException {
|
||||
String verifyCode = ImgVerifyCodeUtils.getRandomVerifyCode(4);
|
||||
String img = ImgVerifyCodeUtils.genVerifyCodeImg(verifyCode);
|
||||
stringRedisTemplate.opsForValue().set(CacheConsts.IMG_VERIFY_CODE_CACHE_KEY + userKey
|
||||
stringRedisTemplate.opsForValue().set(CacheConsts.IMG_VERIFY_CODE_CACHE_KEY + sessionId
|
||||
, verifyCode, Duration.ofMinutes(5));
|
||||
return img;
|
||||
}
|
||||
@ -38,10 +38,17 @@ public class VerifyCodeManager {
|
||||
/**
|
||||
* 校验图片验证码
|
||||
*/
|
||||
public boolean imgVerifyCodeOk(String userKey, String verifyCode) {
|
||||
public boolean imgVerifyCodeOk(String sessionId, String verifyCode) {
|
||||
return Objects.equals(
|
||||
stringRedisTemplate.opsForValue().get(CacheConsts.IMG_VERIFY_CODE_CACHE_KEY + userKey)
|
||||
stringRedisTemplate.opsForValue().get(CacheConsts.IMG_VERIFY_CODE_CACHE_KEY + sessionId)
|
||||
, verifyCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除验证码
|
||||
*/
|
||||
public void removeImgVerifyCode(String sessionId) {
|
||||
stringRedisTemplate.delete(CacheConsts.IMG_VERIFY_CODE_CACHE_KEY + sessionId);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,19 +0,0 @@
|
||||
package io.github.xxyopen.novel.service;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 文件相关服务类
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/17
|
||||
*/
|
||||
public interface FileService {
|
||||
|
||||
/**
|
||||
* 获取图片验证码
|
||||
* @param userKey 请求用户的标识,表明该验证码属于谁
|
||||
* @return Base64编码的图片
|
||||
* */
|
||||
String getImgVerifyCode(String userKey) throws IOException;
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package io.github.xxyopen.novel.service;
|
||||
|
||||
import io.github.xxyopen.novel.core.common.resp.RestResp;
|
||||
import io.github.xxyopen.novel.dto.resp.ImgVerifyCodeRespDto;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 资源(图片/视频/文档)相关服务类
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/17
|
||||
*/
|
||||
public interface ResourceService {
|
||||
|
||||
/**
|
||||
* 获取图片验证码
|
||||
* @return Base64编码的图片
|
||||
* */
|
||||
RestResp<ImgVerifyCodeRespDto> getImgVerifyCode() throws IOException;
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
package io.github.xxyopen.novel.service.impl;
|
||||
|
||||
import io.github.xxyopen.novel.manager.VerifyCodeManager;
|
||||
import io.github.xxyopen.novel.service.FileService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 文件相关服务实现类
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/17
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class FileServiceImpl implements FileService {
|
||||
|
||||
private final VerifyCodeManager verifyCodeManager;
|
||||
|
||||
@Override
|
||||
public String getImgVerifyCode(String userKey) throws IOException {
|
||||
return verifyCodeManager.genImgVerifyCode(userKey);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package io.github.xxyopen.novel.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
||||
import io.github.xxyopen.novel.core.common.resp.RestResp;
|
||||
import io.github.xxyopen.novel.dto.resp.ImgVerifyCodeRespDto;
|
||||
import io.github.xxyopen.novel.manager.VerifyCodeManager;
|
||||
import io.github.xxyopen.novel.service.ResourceService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 资源(图片/视频/文档)相关服务实现类
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/17
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ResourceServiceImpl implements ResourceService {
|
||||
|
||||
private final VerifyCodeManager verifyCodeManager;
|
||||
|
||||
@Override
|
||||
public RestResp<ImgVerifyCodeRespDto> getImgVerifyCode() throws IOException {
|
||||
String sessionId = IdWorker.get32UUID();
|
||||
return RestResp.ok(ImgVerifyCodeRespDto.builder()
|
||||
.sessionId(sessionId)
|
||||
.img(verifyCodeManager.genImgVerifyCode(sessionId))
|
||||
.build());
|
||||
}
|
||||
|
||||
}
|
@ -46,7 +46,7 @@ public class UserServiceImpl implements UserService {
|
||||
@Override
|
||||
public RestResp<String> register(UserRegisterReqDto dto) {
|
||||
// 校验图形验证码是否正确
|
||||
if (!verifyCodeManager.imgVerifyCodeOk(dto.getUserKey(), dto.getVelCode())) {
|
||||
if (!verifyCodeManager.imgVerifyCodeOk(dto.getSessionId(), dto.getVelCode())) {
|
||||
// 图形验证码校验失败
|
||||
throw new BusinessException(ErrorCodeEnum.USER_VERIFY_CODE_ERROR);
|
||||
}
|
||||
@ -71,6 +71,9 @@ public class UserServiceImpl implements UserService {
|
||||
userInfo.setSalt("0");
|
||||
userInfoMapper.insert(userInfo);
|
||||
|
||||
// 删除验证码
|
||||
verifyCodeManager.removeImgVerifyCode(dto.getSessionId());
|
||||
|
||||
// 生成JWT 并返回
|
||||
return RestResp.ok(jwtUtils.generateToken(userInfo.getId(), SystemConfigConsts.NOVEL_FRONT_KEY));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user