|
|
@@ -6,6 +6,7 @@ import com.etotem.cfc.dto.*;
|
|
|
import com.etotem.cfc.dto.RegisterWithIdCardDTO;
|
|
|
import com.etotem.cfc.dto.RegisterWithInviteCodeDTO;
|
|
|
import com.etotem.cfc.entity.User;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
import com.etotem.cfc.service.UserService;
|
|
|
import com.etotem.cfc.service.VerificationCodeService;
|
|
|
import io.jsonwebtoken.Claims;
|
|
|
@@ -13,6 +14,7 @@ import io.swagger.v3.oas.annotations.Operation;
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
@@ -21,6 +23,7 @@ import java.util.Map;
|
|
|
|
|
|
@Tag(name = "用户认证", description = "用户登录、验证码、密码设置等认证接口")
|
|
|
@RestController
|
|
|
+@Slf4j
|
|
|
@RequestMapping("/api/auth")
|
|
|
public class AuthController {
|
|
|
|
|
|
@@ -33,6 +36,9 @@ public class AuthController {
|
|
|
@Resource
|
|
|
private JwtConfig jwtConfig;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
/**
|
|
|
* 发送验证码
|
|
|
*/
|
|
|
@@ -191,7 +197,7 @@ public class AuthController {
|
|
|
* JWT Token 验证
|
|
|
* 从 Authorization header 提取 Bearer token,验证并返回用户信息
|
|
|
*/
|
|
|
- @Operation(summary = "验证JWT Token有效性")
|
|
|
+ @Operation(summary = "验证JWT Token有效性(含 user 存在性)")
|
|
|
@PostMapping("/verify")
|
|
|
public Result<Map<String, Object>> verifyToken(HttpServletRequest request) {
|
|
|
String authHeader = request.getHeader("Authorization");
|
|
|
@@ -204,6 +210,13 @@ public class AuthController {
|
|
|
Long userId = Long.parseLong(claims.getSubject());
|
|
|
String role = (String) claims.get("role");
|
|
|
|
|
|
+ // 校验 user 是否仍存在(账号被清理/禁用时 JWT 仍可能合法)
|
|
|
+ // 不存在则视为登录态失效,返回 401 触发前端 _clearAuthAndRedirect
|
|
|
+ if (userMapper.selectById(userId) == null) {
|
|
|
+ log.warn("/api/auth/verify: userId={} 在数据库不存在,返回 401", userId);
|
|
|
+ return Result.error(401, "用户不存在");
|
|
|
+ }
|
|
|
+
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
|
data.put("userId", userId);
|
|
|
data.put("role", role);
|