mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 15:13:55 +08:00
登录。注册。验证码 (#271)
* 修改shiro配置,对/user/*的请求不继续登录验证拦截 --ShiroConfig.java * 添加了注册,登录接口 --LoginController.java 修改登录请求dto,使得符合Apifox定义接口请求属性 --LoginDTO.java * 为数据库sys_user表补全创建者的字段 --202503101631.sql --db.changelog-master.yaml * 修改系统上下文路径的前缀,使得符合Apifox的接口请求前缀 --application.yml * 注册接口补上验证码判断 --LoginController.java
This commit is contained in:
@@ -69,6 +69,7 @@ public class ShiroConfig {
|
||||
filterMap.put("/captcha", "anon");
|
||||
filterMap.put("/favicon.ico", "anon");
|
||||
filterMap.put("/mobile/**", "anon");
|
||||
filterMap.put("/user/**", "anon");
|
||||
filterMap.put("/**", "oauth2");
|
||||
shiroFilter.setFilterChainDefinitionMap(filterMap);
|
||||
|
||||
|
||||
+47
-16
@@ -2,41 +2,37 @@ package xiaozhi.modules.security.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.AllArgsConstructor;
|
||||
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 org.springframework.web.bind.annotation.*;
|
||||
import xiaozhi.common.exception.ErrorCode;
|
||||
import xiaozhi.common.redis.RedisUtils;
|
||||
import xiaozhi.common.utils.PropertiesUtils;
|
||||
import xiaozhi.common.exception.RenException;
|
||||
import xiaozhi.common.utils.Result;
|
||||
import xiaozhi.common.validator.AssertUtils;
|
||||
import xiaozhi.modules.security.dto.LoginDTO;
|
||||
import xiaozhi.modules.security.password.PasswordUtils;
|
||||
import xiaozhi.modules.security.service.CaptchaService;
|
||||
import xiaozhi.modules.security.service.SysUserTokenService;
|
||||
import xiaozhi.modules.sys.service.SysParamsService;
|
||||
import xiaozhi.modules.sys.dto.SysUserDTO;
|
||||
import xiaozhi.modules.sys.service.SysUserService;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 登录
|
||||
* 登录控制层
|
||||
*/
|
||||
@Tag(name = "登录管理")
|
||||
@AllArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
@Tag(name = "登录管理")
|
||||
public class LoginController {
|
||||
private final SysUserService sysUserService;
|
||||
private final SysUserTokenService sysUserTokenService;
|
||||
private final CaptchaService captchaService;
|
||||
private final RedisUtils redisUtils;
|
||||
private final SysParamsService sysParamsService;
|
||||
private final PropertiesUtils propertiesUtils;
|
||||
|
||||
@GetMapping("captcha")
|
||||
|
||||
@GetMapping("/captcha")
|
||||
@Operation(summary = "验证码")
|
||||
public void captcha(HttpServletResponse response, String uuid) throws IOException {
|
||||
//uuid不能为空
|
||||
@@ -46,10 +42,45 @@ public class LoginController {
|
||||
captchaService.create(response, uuid);
|
||||
}
|
||||
|
||||
@PostMapping("login")
|
||||
@PostMapping("/login")
|
||||
@Operation(summary = "登录")
|
||||
public Result login(HttpServletRequest request, @RequestBody LoginDTO login) {
|
||||
return sysUserTokenService.createToken(1L);
|
||||
public Result login( @RequestBody LoginDTO login) {
|
||||
// 验证是否正确输入验证码
|
||||
boolean validate = captchaService.validate(login.getCaptchaId(), login.getCaptcha());
|
||||
if (!validate) {
|
||||
throw new RenException("验证码错误,请重新获取");
|
||||
}
|
||||
// 按照用户名获取用户
|
||||
SysUserDTO userDTO = sysUserService.getByUsername(login.getMobile());
|
||||
// 判断用户是否存在
|
||||
if (userDTO == null) {
|
||||
throw new RenException("请检测用户和密码是否输入错误");
|
||||
}
|
||||
// 判断密码是否正确,不一样则进入if
|
||||
if (!PasswordUtils.matches(login.getPassword(), userDTO.getPassword())) {
|
||||
throw new RenException("请检测用户和密码是否输入错误");
|
||||
}
|
||||
return sysUserTokenService.createToken(userDTO.getId());
|
||||
}
|
||||
|
||||
@PostMapping("/register")
|
||||
@Operation(summary = "注册")
|
||||
public Result<Void> register(@RequestBody LoginDTO login) {
|
||||
// 验证是否正确输入验证码
|
||||
boolean validate = captchaService.validate(login.getCaptchaId(), login.getCaptcha());
|
||||
if (!validate) {
|
||||
throw new RenException("验证码错误,请重新获取");
|
||||
}
|
||||
// 按照用户名获取用户
|
||||
SysUserDTO userDTO = sysUserService.getByUsername(login.getMobile());
|
||||
if (userDTO != null){
|
||||
throw new RenException("此手机号码已经注册过");
|
||||
}
|
||||
userDTO = new SysUserDTO();
|
||||
userDTO.setUsername(login.getMobile());
|
||||
userDTO.setPassword(login.getPassword());
|
||||
sysUserService.save(userDTO);
|
||||
return new Result<Void>();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,9 +13,9 @@ import java.io.Serializable;
|
||||
@Schema(description = "登录表单")
|
||||
public class LoginDTO implements Serializable {
|
||||
|
||||
@Schema(description = "用户名", required = true)
|
||||
@Schema(description = "手机号码")
|
||||
@NotBlank(message = "{sysuser.username.require}")
|
||||
private String username;
|
||||
private String mobile;
|
||||
|
||||
@Schema(description = "密码")
|
||||
@NotBlank(message = "{sysuser.password.require}")
|
||||
@@ -27,6 +27,6 @@ public class LoginDTO implements Serializable {
|
||||
|
||||
@Schema(description = "唯一标识")
|
||||
@NotBlank(message = "{sysuser.uuid.require}")
|
||||
private String uuid;
|
||||
private String captchaId;
|
||||
|
||||
}
|
||||
@@ -7,7 +7,7 @@ server:
|
||||
min-spare: 30
|
||||
port: 8002
|
||||
servlet:
|
||||
context-path: /xiaozhi-esp32-api
|
||||
context-path: /xiaozhi-esp32-api/api/v1
|
||||
session:
|
||||
cookie:
|
||||
http-only: true
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
-- 给用户表添加一个创建者
|
||||
ALTER TABLE sys_user ADD COLUMN creator BIGINT COMMENT '创建者';
|
||||
@@ -8,4 +8,11 @@ databaseChangeLog:
|
||||
changes:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/001create_sys.sql
|
||||
path: classpath:db/changelog/001create_sys.sql
|
||||
- changeSet:
|
||||
id: 202503101631
|
||||
author: zjy
|
||||
changes:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202503101631.sql
|
||||
|
||||
Reference in New Issue
Block a user