feat: 新增用户注册,忘记密码接口,修复第三方注册默认权限和删除报错问题

This commit is contained in:
King
2025-03-20 02:43:21 +00:00
committed by Charles7c
parent 603b12d10d
commit 94b093e9d4
20 changed files with 193 additions and 33 deletions

View File

@@ -26,6 +26,7 @@ import io.swagger.v3.oas.annotations.Hidden;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.spring.EnableFileStorage;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
@@ -52,6 +53,7 @@ import top.continew.starter.web.model.R;
@RestController
@SpringBootApplication
@RequiredArgsConstructor
@MapperScan("top.continew.admin.system.mapper")
public class ContiNewAdminApplication implements ApplicationRunner {
private final ProjectProperties projectProperties;

View File

@@ -24,24 +24,26 @@ import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.common.config.properties.CaptchaProperties;
import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.common.controller.BaseController;
import top.continew.admin.common.constant.RegexConstants;
import top.continew.admin.common.util.SecureUtils;
import top.continew.admin.system.model.entity.UserDO;
import top.continew.admin.system.model.query.UserQuery;
import top.continew.admin.system.model.req.user.UserImportReq;
import top.continew.admin.system.model.req.user.UserPasswordResetReq;
import top.continew.admin.system.model.req.user.UserReq;
import top.continew.admin.system.model.req.user.UserRoleUpdateReq;
import top.continew.admin.system.model.req.user.*;
import top.continew.admin.system.model.resp.user.UserDetailResp;
import top.continew.admin.system.model.resp.user.UserImportParseResp;
import top.continew.admin.system.model.resp.user.UserImportResp;
import top.continew.admin.system.model.resp.user.UserResp;
import top.continew.admin.system.service.UserService;
import top.continew.starter.cache.redisson.util.RedisUtils;
import top.continew.starter.core.util.ExceptionUtils;
import top.continew.starter.core.validation.ValidationUtils;
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
@@ -65,6 +67,56 @@ import java.io.IOException;
Api.EXPORT})
public class UserController extends BaseController<UserService, UserResp, UserDetailResp, UserQuery, UserReq> {
private final UserService userService;
private final CaptchaProperties captchaProperties;
@Operation(summary = "用户注册", description = "用户注册")
@PostMapping(value = "/signup")
public BaseIdResp<Long> signup(@Validated(CrudValidationGroup.Add.class) @RequestBody UserReq req) {
String captcha = req.getCaptcha();
if (!StringUtil.equals(captcha, captchaProperties.getSms().getCode())) {
String key = StringUtil.isNotBlank(req.getUuid())
? req.getUuid()
: StringUtil.isNotBlank(req.getPhone())
? req.getPhone()
: StringUtil.isNotBlank(req.getEmail()) ? req.getEmail() : "";
ValidationUtils.throwIfBlank(captcha, "验证码不能为空");
ValidationUtils.throwIfBlank(key, "验证码标识不能为空");
String captchaKey = CacheConstants.CAPTCHA_KEY_PREFIX + key;
String captcha1 = RedisUtils.get(captchaKey);
ValidationUtils.throwIfBlank(captcha1, "验证码已失效");
ValidationUtils.throwIfNotEqualIgnoreCase(captcha, captcha1, "验证码错误");
// RedisUtils.delete(captchaKey);
}
String rawPassword = ExceptionUtils.exToNull(() -> SecureUtils.decryptByRsaPrivateKey(req.getPassword()));
ValidationUtils.throwIfNull(rawPassword, "密码解密失败");
ValidationUtils.throwIf(!ReUtil
.isMatch(RegexConstants.PASSWORD, rawPassword), "密码长度为 8-32 个字符,支持大小写字母、数字、特殊字符,至少包含字母和数字");
req.setPassword(rawPassword);
return super.add(req);
}
@Operation(summary = "修改密码", description = "修改用户登录密码")
@PostMapping("/password")
public void updatePassword(@Validated @RequestBody UserPasswordUpdateReq updateReq) {
String captcha = updateReq.getCaptcha();
if (!StringUtil.equals(captcha, captchaProperties.getSms().getCode())) {
String captchaKey = CacheConstants.CAPTCHA_KEY_PREFIX + updateReq.getEmail();
String captcha1 = RedisUtils.get(captchaKey);
ValidationUtils.throwIfBlank(captcha1, "验证码已失效");
ValidationUtils.throwIfNotEqualIgnoreCase(captcha, captcha1, "验证码错误");
RedisUtils.delete(captchaKey);
}
String newPassword = ExceptionUtils.exToNull(() -> SecureUtils.decryptByRsaPrivateKey(updateReq
.getNewPassword()));
ValidationUtils.throwIfNull(newPassword, "新密码解密失败");
ValidationUtils.throwIf(!ReUtil
.isMatch(RegexConstants.PASSWORD, newPassword), "密码长度为 8-32 个字符,支持大小写字母、数字、特殊字符,至少包含字母和数字");
UserDO user = userService.getByUsername(updateReq.getUsername());
ValidationUtils.throwIfEmpty(user, "用户名错误或不存在");
userService.updatePassword("", newPassword, user.getId());
}
@Override
@Operation(summary = "新增数据", description = "新增数据")
public BaseIdResp<Long> add(@Validated(CrudValidationGroup.Add.class) @RequestBody UserReq req) {

View File

@@ -12,7 +12,7 @@ server:
spring.datasource:
type: com.zaxxer.hikari.HikariDataSource
# 请务必提前创建好名为 continew_admin 的数据库,如果使用其他数据库名请注意同步修改 DB_NAME 配置
url: jdbc:p6spy:mysql://${DB_HOST:127.0.0.1}:${DB_PORT:3306}/${DB_NAME:continew_admin}?serverTimezone=Asia/Shanghai&useSSL=true&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&autoReconnect=true&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
url: jdbc:p6spy:mysql://${DB_HOST:127.0.0.1}:${DB_PORT:3306}/${DB_NAME:continew_admin}?serverTimezone=Asia/Shanghai&useSSL=true&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&autoReconnect=true&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true&maxReconnects=10&failOverReadOnly=false
username: ${DB_USER:root}
password: ${DB_PWD:123456}
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
@@ -126,8 +126,10 @@ captcha:
templatePath: mail/captcha.ftl
## 短信验证码配置
sms:
# 万能验证码(限调试时使用)
code: 111111
# 内容长度
length: 4
length: 6
# 过期时间
expirationInMinutes: 5
# 模板 ID
@@ -232,6 +234,8 @@ sa-token.extension:
- /swagger-ui/**
- /swagger-resources/**
- /*/api-docs/**
- /system/user/signup
- /system/user/password
# 本地存储资源
- /file/**

View File

@@ -156,8 +156,9 @@ INSERT INTO `sys_role`
(`id`, `name`, `code`, `data_scope`, `description`, `sort`, `is_system`, `create_user`, `create_time`)
VALUES
(1, '系统管理员', 'admin', 1, '系统初始角色', 1, b'1', 1, NOW()),
(547888897925840927, '测试人员', 'tester', 5, NULL, 2, b'0', 1, NOW()),
(547888897925840928, '研发人员', 'developer', 4, NULL, 3, b'0', 1, NOW());
(2, '普通用户', 'general', 4, '系统初始角色', 2, b'0', 1, NOW()),
(547888897925840927, '测试人员', 'tester', 5, NULL, 3, b'0', 1, NOW()),
(547888897925840928, '研发人员', 'developer', 4, NULL, 4, b'0', 1, NOW());
-- 初始化默认用户admin/admin123test/test123
INSERT INTO `sys_user`

View File

@@ -156,8 +156,9 @@ INSERT INTO "sys_role"
("id", "name", "code", "data_scope", "description", "sort", "is_system", "create_user", "create_time")
VALUES
(1, '系统管理员', 'admin', 1, '系统初始角色', 1, true, 1, NOW()),
(547888897925840927, '测试人员', 'tester', 5, NULL, 2, false, 1, NOW()),
(547888897925840928, '研发人员', 'developer', 4, NULL, 3, false, 1, NOW());
(2, '普通用户', 'general', 4, '系统初始角色', 2, false, 1, NOW()),
(547888897925840927, '测试人员', 'tester', 5, NULL, 3, false, 1, NOW()),
(547888897925840928, '研发人员', 'developer', 4, NULL, 4, false, 1, NOW());
-- 初始化默认用户admin/admin123test/test123
INSERT INTO "sys_user"