接口优化
This commit is contained in:
@@ -9,6 +9,7 @@ import com.emotion.dto.response.CaptchaResponse;
|
||||
import com.emotion.dto.response.UserInfoResponse;
|
||||
import com.emotion.service.AuthService;
|
||||
import com.emotion.service.TokenService;
|
||||
import com.emotion.util.UserContextUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@@ -52,10 +53,9 @@ public class AuthController {
|
||||
/**
|
||||
* 获取当前用户信息
|
||||
*/
|
||||
@GetMapping("/user/info")
|
||||
@GetMapping("/userInfo")
|
||||
public Result<UserInfoResponse> getCurrentUserInfo(HttpServletRequest request) {
|
||||
String token = extractToken(request);
|
||||
UserInfoResponse userInfo = tokenService.getUserInfoByToken(token);
|
||||
UserInfoResponse userInfo = tokenService.getUserInfoByToken(request);
|
||||
return Result.success(userInfo);
|
||||
}
|
||||
|
||||
@@ -73,15 +73,14 @@ public class AuthController {
|
||||
*/
|
||||
@PostMapping("/logout")
|
||||
public Result<Void> logout(HttpServletRequest request) {
|
||||
String token = extractToken(request);
|
||||
authService.logoutByToken(token);
|
||||
authService.logoutByToken(request);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新访问令牌
|
||||
*/
|
||||
@PostMapping("/refresh")
|
||||
@PostMapping("/refreshToken")
|
||||
public Result<AuthResponse> refreshToken(@Valid @RequestBody RefreshTokenRequest request) {
|
||||
AuthResponse response = authService.refreshToken(request.getRefreshToken());
|
||||
return Result.success("令牌刷新成功", response);
|
||||
@@ -90,14 +89,9 @@ public class AuthController {
|
||||
/**
|
||||
* 验证访问令牌
|
||||
*/
|
||||
@GetMapping("/validate")
|
||||
@GetMapping("/validateToken")
|
||||
public Result<Boolean> validateToken(HttpServletRequest request) {
|
||||
String token = extractToken(request);
|
||||
if (token == null) {
|
||||
return Result.success(false);
|
||||
}
|
||||
|
||||
boolean isValid = authService.validateToken(token);
|
||||
boolean isValid = authService.validateToken(request);
|
||||
return Result.success(isValid);
|
||||
}
|
||||
|
||||
@@ -106,15 +100,14 @@ public class AuthController {
|
||||
*/
|
||||
@GetMapping("/username")
|
||||
public Result<String> getUsernameFromToken(HttpServletRequest request) {
|
||||
String token = extractToken(request);
|
||||
String username = tokenService.getUsernameByToken(token);
|
||||
String username = tokenService.getUsernameByToken(request);
|
||||
return Result.success(username);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查账号是否存在
|
||||
*/
|
||||
@GetMapping("/check-account")
|
||||
@GetMapping("/checkAccount")
|
||||
public Result<Boolean> checkAccount(@RequestParam String account) {
|
||||
boolean exists = authService.existsByAccount(account);
|
||||
return Result.success(exists);
|
||||
@@ -123,7 +116,7 @@ public class AuthController {
|
||||
/**
|
||||
* 检查邮箱是否存在
|
||||
*/
|
||||
@GetMapping("/check-email")
|
||||
@GetMapping("/checkEmail")
|
||||
public Result<Boolean> checkEmail(@RequestParam String email) {
|
||||
boolean exists = authService.existsByEmail(email);
|
||||
return Result.success(exists);
|
||||
@@ -132,27 +125,9 @@ public class AuthController {
|
||||
/**
|
||||
* 检查手机号是否存在
|
||||
*/
|
||||
@GetMapping("/check-phone")
|
||||
@GetMapping("/checkPhone")
|
||||
public Result<Boolean> checkPhone(@RequestParam String phone) {
|
||||
boolean exists = authService.existsByPhone(phone);
|
||||
return Result.success(exists);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从请求中提取访问令牌
|
||||
*/
|
||||
private String extractToken(HttpServletRequest request) {
|
||||
String authHeader = request.getHeader("Authorization");
|
||||
if (authHeader != null && authHeader.startsWith("Bearer ")) {
|
||||
return authHeader.substring(7);
|
||||
}
|
||||
|
||||
// 也可以从请求参数中获取
|
||||
String tokenParam = request.getParameter("token");
|
||||
if (tokenParam != null && !tokenParam.trim().isEmpty()) {
|
||||
return tokenParam.trim();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user