代码优化
This commit is contained in:
@@ -6,10 +6,14 @@ import com.emotion.dto.request.RegisterRequest;
|
||||
import com.emotion.dto.request.RefreshTokenRequest;
|
||||
import com.emotion.dto.response.AuthResponse;
|
||||
import com.emotion.dto.response.CaptchaResponse;
|
||||
import com.emotion.dto.response.SmsCodeResponse;
|
||||
import com.emotion.dto.response.UserInfoResponse;
|
||||
import com.emotion.service.AuthService;
|
||||
import com.emotion.service.TokenService;
|
||||
import com.emotion.util.UserContextUtils;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@@ -24,6 +28,7 @@ import javax.validation.Valid;
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/auth")
|
||||
@Tag(name = "认证管理", description = "用户注册、登录、验证码等认证相关接口")
|
||||
public class AuthController {
|
||||
|
||||
@Autowired
|
||||
@@ -33,18 +38,20 @@ public class AuthController {
|
||||
private TokenService tokenService;
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
* 用户登录(简化版:手机号+验证码,不存在则自动注册)
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
@Operation(summary = "用户登录", description = "使用手机号和短信验证码登录,若用户不存在则自动注册")
|
||||
public Result<AuthResponse> login(@Valid @RequestBody LoginRequest request) {
|
||||
AuthResponse response = authService.login(request);
|
||||
return Result.success("登录成功", response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
* 用户注册(简化版:仅需手机号、密码和短信验证码)
|
||||
*/
|
||||
@PostMapping("/register")
|
||||
@Operation(summary = "用户注册", description = "使用手机号、密码和短信验证码进行注册")
|
||||
public Result<AuthResponse> register(@Valid @RequestBody RegisterRequest request) {
|
||||
AuthResponse response = authService.register(request);
|
||||
return Result.success("注册成功", response);
|
||||
@@ -60,14 +67,27 @@ public class AuthController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成验证码
|
||||
* 生成验证码(图形验证码,用于登录)
|
||||
*/
|
||||
@GetMapping("/captcha")
|
||||
@Operation(summary = "获取图形验证码", description = "用于登录时的图形验证码")
|
||||
public Result<CaptchaResponse> generateCaptcha() {
|
||||
CaptchaResponse response = authService.generateCaptcha();
|
||||
return Result.success(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取短信验证码(用于注册)
|
||||
*/
|
||||
@GetMapping("/sms-code")
|
||||
@Operation(summary = "获取短信验证码", description = "用于注册时的短信验证码")
|
||||
public Result<SmsCodeResponse> getSmsCode(
|
||||
@Parameter(description = "手机号", required = true)
|
||||
@RequestParam String phone) {
|
||||
SmsCodeResponse response = authService.sendSmsCode(phone);
|
||||
return Result.success("验证码已发送", response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登出
|
||||
*/
|
||||
|
||||
@@ -1,44 +1,60 @@
|
||||
package com.emotion.controller;
|
||||
|
||||
import com.emotion.common.Result;
|
||||
import com.emotion.dto.request.TokenRequest;
|
||||
import com.emotion.dto.response.UserInfoResponse;
|
||||
import com.emotion.service.TokenService;
|
||||
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.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* Token控制器
|
||||
* 提供基于请求头Authorization的token验证和用户信息获取功能
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/token")
|
||||
@Tag(name = "Token管理", description = "Token验证和用户信息获取")
|
||||
public class TokenController {
|
||||
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
|
||||
/**
|
||||
* 通过token获取用户信息
|
||||
* 通过请求头中的token获取用户信息
|
||||
* Token应该在请求头中以 "Authorization: Bearer {token}" 的形式传递
|
||||
*/
|
||||
@PostMapping("/user-info")
|
||||
public Result<UserInfoResponse> getUserInfoByToken(@RequestBody @Validated TokenRequest request) {
|
||||
UserInfoResponse userInfo = tokenService.getUserInfoByToken(request.getToken());
|
||||
@GetMapping("/user-info")
|
||||
@Operation(summary = "获取用户信息", description = "通过请求头中的token获取当前用户信息")
|
||||
public Result<UserInfoResponse> getUserInfoByToken(HttpServletRequest request) {
|
||||
UserInfoResponse userInfo = tokenService.getUserInfoByToken(request);
|
||||
return Result.success(userInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过token获取用户名
|
||||
* 通过请求头中的token获取用户名
|
||||
* Token应该在请求头中以 "Authorization: Bearer {token}" 的形式传递
|
||||
*/
|
||||
@PostMapping("/username")
|
||||
public Result<String> getUsernameByToken(@RequestBody @Validated TokenRequest request) {
|
||||
String username = tokenService.getUsernameByToken(request.getToken());
|
||||
@GetMapping("/username")
|
||||
@Operation(summary = "获取用户名", description = "通过请求头中的token获取当前用户名")
|
||||
public Result<String> getUsernameByToken(HttpServletRequest request) {
|
||||
String username = tokenService.getUsernameByToken(request);
|
||||
return Result.success(username);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证token并返回用户ID
|
||||
* 验证请求头中的token并返回用户ID
|
||||
* Token应该在请求头中以 "Authorization: Bearer {token}" 的形式传递
|
||||
*/
|
||||
@PostMapping("/validate")
|
||||
public Result<String> validateTokenAndGetUserId(@RequestBody @Validated TokenRequest request) {
|
||||
String userId = tokenService.validateTokenAndGetUserId(request.getToken());
|
||||
@GetMapping("/validate")
|
||||
@Operation(summary = "验证Token", description = "验证请求头中的token并返回用户ID")
|
||||
public Result<String> validateTokenAndGetUserId(HttpServletRequest request) {
|
||||
String userId = tokenService.validateTokenAndGetUserId(request);
|
||||
return Result.success(userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user