不再使用的代码清理
This commit is contained in:
@@ -23,15 +23,17 @@ public class WebMvcConfig implements WebMvcConfigurer {
|
||||
registry.addInterceptor(jwtAuthInterceptor)
|
||||
.addPathPatterns("/api/**") // 拦截所有API请求
|
||||
.excludePathPatterns(
|
||||
"/api/auth/login", // 登录接口
|
||||
"/api/auth/register", // 注册接口
|
||||
"/api/auth/captcha", // 验证码接口
|
||||
"/api/auth/refresh-token", // 刷新token接口
|
||||
"/api/health", // 健康检查接口
|
||||
"/api/ws/**", // WebSocket接口
|
||||
"/swagger-ui/**", // Swagger UI
|
||||
"/v3/api-docs/**", // API文档
|
||||
"/actuator/**" // 监控端点
|
||||
"/api/auth/login", // 登录接口
|
||||
"/api/auth/register", // 注册接口
|
||||
"/api/auth/captcha", // 图形验证码接口
|
||||
"/api/auth/sms-code", // 短信验证码接口(免登录)
|
||||
"/api/auth/refresh-token", // 刷新token接口
|
||||
"/api/auth/resetPassword", // 重置密码接口(免登录)
|
||||
"/api/health", // 健康检查接口
|
||||
"/api/ws/**", // WebSocket接口
|
||||
"/swagger-ui/**", // Swagger UI
|
||||
"/v3/api-docs/**", // API文档
|
||||
"/actuator/**" // 监控端点
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@ import com.emotion.common.Result;
|
||||
import com.emotion.dto.request.LoginRequest;
|
||||
import com.emotion.dto.request.RegisterRequest;
|
||||
import com.emotion.dto.request.RefreshTokenRequest;
|
||||
import com.emotion.dto.request.ResetPasswordRequest;
|
||||
import com.emotion.dto.response.ResetPasswordResponse;
|
||||
|
||||
import com.emotion.dto.response.AuthResponse;
|
||||
import com.emotion.dto.response.CaptchaResponse;
|
||||
import com.emotion.dto.response.SmsCodeResponse;
|
||||
@@ -50,13 +53,23 @@ public class AuthController {
|
||||
/**
|
||||
* 用户注册(简化版:仅需手机号、密码和短信验证码)
|
||||
*/
|
||||
@PostMapping("/register")
|
||||
@PostMapping(value = "/register")
|
||||
@Operation(summary = "用户注册", description = "使用手机号、密码和短信验证码进行注册")
|
||||
public Result<AuthResponse> register(@Valid @RequestBody RegisterRequest request) {
|
||||
AuthResponse response = authService.register(request);
|
||||
return Result.success("注册成功", response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置密码(手机号 + 验证码)
|
||||
*/
|
||||
@PostMapping(value = "/resetPassword")
|
||||
@Operation(summary = "重置密码", description = "通过手机号和验证码重置密码,验证码本期固定为123456")
|
||||
public Result<ResetPasswordResponse> resetPassword(@Valid @RequestBody ResetPasswordRequest request) {
|
||||
ResetPasswordResponse response = authService.resetPassword(request);
|
||||
return Result.success("重置密码成功", response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户信息
|
||||
*/
|
||||
|
||||
@@ -2,6 +2,9 @@ package com.emotion.service;
|
||||
|
||||
import com.emotion.dto.request.LoginRequest;
|
||||
import com.emotion.dto.request.RegisterRequest;
|
||||
import com.emotion.dto.request.ResetPasswordRequest;
|
||||
import com.emotion.dto.response.ResetPasswordResponse;
|
||||
|
||||
import com.emotion.dto.response.AuthResponse;
|
||||
import com.emotion.dto.response.CaptchaResponse;
|
||||
import com.emotion.dto.response.SmsCodeResponse;
|
||||
@@ -11,52 +14,60 @@ import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 认证服务接口
|
||||
*
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-23
|
||||
*/
|
||||
public interface AuthService {
|
||||
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*
|
||||
*
|
||||
* @param request 登录请求
|
||||
* @return 认证响应
|
||||
*/
|
||||
AuthResponse login(LoginRequest request);
|
||||
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
*
|
||||
*
|
||||
* @param request 注册请求
|
||||
* @return 认证响应
|
||||
*/
|
||||
/**
|
||||
* 重置密码(手机号 + 验证码)
|
||||
*
|
||||
* @param request 重置密码请求
|
||||
* @return 重置密码响应
|
||||
*/
|
||||
ResetPasswordResponse resetPassword(ResetPasswordRequest request);
|
||||
|
||||
AuthResponse register(RegisterRequest request);
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前用户信息
|
||||
*
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 用户信息响应
|
||||
*/
|
||||
UserInfoResponse getCurrentUserInfo(String userId);
|
||||
|
||||
|
||||
/**
|
||||
* 生成验证码
|
||||
*
|
||||
*
|
||||
* @return 验证码响应
|
||||
*/
|
||||
CaptchaResponse generateCaptcha();
|
||||
|
||||
|
||||
/**
|
||||
* 验证验证码
|
||||
*
|
||||
*
|
||||
* @param captchaKey 验证码key
|
||||
* @param captcha 验证码
|
||||
* @param captcha 验证码
|
||||
* @return 是否验证成功
|
||||
*/
|
||||
boolean validateCaptcha(String captchaKey, String captcha);
|
||||
|
||||
|
||||
/**
|
||||
* 用户登出
|
||||
*
|
||||
@@ -73,18 +84,18 @@ public interface AuthService {
|
||||
* @return 是否登出成功
|
||||
*/
|
||||
boolean logoutByToken(HttpServletRequest request);
|
||||
|
||||
|
||||
/**
|
||||
* 刷新访问令牌
|
||||
*
|
||||
*
|
||||
* @param refreshToken 刷新令牌
|
||||
* @return 新的认证响应
|
||||
*/
|
||||
AuthResponse refreshToken(String refreshToken);
|
||||
|
||||
|
||||
/**
|
||||
* 验证访问令牌
|
||||
*
|
||||
*
|
||||
* @param request HTTP请求
|
||||
* @return 是否有效
|
||||
*/
|
||||
@@ -92,20 +103,20 @@ public interface AuthService {
|
||||
|
||||
/**
|
||||
* 验证访问令牌
|
||||
*
|
||||
*
|
||||
* @param token 访问令牌
|
||||
* @return 是否有效
|
||||
*/
|
||||
boolean validateToken(String token);
|
||||
|
||||
|
||||
/**
|
||||
* 从令牌中获取用户ID
|
||||
*
|
||||
*
|
||||
* @param token 访问令牌
|
||||
* @return 用户ID
|
||||
*/
|
||||
String getUserIdFromToken(String token);
|
||||
|
||||
|
||||
/**
|
||||
* 从令牌中获取用户名
|
||||
*
|
||||
|
||||
@@ -2,8 +2,10 @@ package com.emotion.service.impl;
|
||||
|
||||
import com.emotion.dto.request.LoginRequest;
|
||||
import com.emotion.dto.request.RegisterRequest;
|
||||
import com.emotion.dto.request.ResetPasswordRequest;
|
||||
import com.emotion.dto.response.AuthResponse;
|
||||
import com.emotion.dto.response.CaptchaResponse;
|
||||
import com.emotion.dto.response.ResetPasswordResponse;
|
||||
import com.emotion.dto.response.SmsCodeResponse;
|
||||
import com.emotion.dto.response.UserInfoResponse;
|
||||
import com.emotion.entity.User;
|
||||
@@ -155,6 +157,30 @@ public class AuthServiceImpl implements AuthService {
|
||||
return response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResetPasswordResponse resetPassword(ResetPasswordRequest request) {
|
||||
// 验证码本期固定为123456
|
||||
if (request.getCaptcha() == null || !"123456".equals(request.getCaptcha().trim())) {
|
||||
throw new CaptchaException("验证码错误或已过期");
|
||||
}
|
||||
|
||||
// 根据手机号查询用户
|
||||
User user = userService.getByPhone(request.getPhone());
|
||||
if (user == null) {
|
||||
throw new BusinessException("用户不存在");
|
||||
}
|
||||
|
||||
// 使用统一的 PasswordEncoder 进行加密,保持与登录/注册一致
|
||||
String encoded = passwordEncoder.encode(request.getNewPassword());
|
||||
user.setPassword(encoded);
|
||||
userService.updateById(user);
|
||||
|
||||
ResetPasswordResponse resp = new ResetPasswordResponse();
|
||||
resp.setSuccess(true);
|
||||
resp.setMessage("重置密码成功");
|
||||
return resp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserInfoResponse getCurrentUserInfo(String userId) {
|
||||
User user = userService.getById(userId);
|
||||
@@ -168,15 +194,15 @@ public class AuthServiceImpl implements AuthService {
|
||||
public CaptchaResponse generateCaptcha() {
|
||||
String captchaKey = UUID.randomUUID().toString();
|
||||
String captchaCode = generateCaptchaCode();
|
||||
|
||||
|
||||
// 生成验证码图片
|
||||
String captchaImage = generateCaptchaImage(captchaCode);
|
||||
|
||||
|
||||
// 存储验证码到Redis
|
||||
redisTemplate.opsForValue().set(
|
||||
CAPTCHA_PREFIX + captchaKey,
|
||||
captchaCode.toLowerCase(),
|
||||
CAPTCHA_EXPIRE_MINUTES,
|
||||
CAPTCHA_PREFIX + captchaKey,
|
||||
captchaCode.toLowerCase(),
|
||||
CAPTCHA_EXPIRE_MINUTES,
|
||||
TimeUnit.MINUTES
|
||||
);
|
||||
|
||||
@@ -201,7 +227,7 @@ public class AuthServiceImpl implements AuthService {
|
||||
|
||||
// 验证成功后删除验证码
|
||||
redisTemplate.delete(CAPTCHA_PREFIX + captchaKey);
|
||||
|
||||
|
||||
return storedCaptcha.equalsIgnoreCase(captcha.trim());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user