174 lines
6.2 KiB
Java
174 lines
6.2 KiB
Java
package com.rj.controller.sys;
|
||
|
||
import com.rj.common.Result;
|
||
import com.rj.entity.sys.User;
|
||
import com.rj.pojo.sys.LoginRequest;
|
||
import com.rj.pojo.sys.LoginResponse;
|
||
import com.rj.service.sys.IUserService;
|
||
import io.swagger.v3.oas.annotations.Operation;
|
||
import io.swagger.v3.oas.annotations.Parameter;
|
||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.http.ResponseEntity;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.web.bind.annotation.*;
|
||
|
||
import jakarta.validation.Valid;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
/**
|
||
* <p>
|
||
* 认证控制器
|
||
* </p>
|
||
*
|
||
* @author 系统生成
|
||
* @since 2025-08-07
|
||
*/
|
||
@RestController
|
||
@RequestMapping("/api/sys/auth")
|
||
@Tag(name = "认证管理", description = "用户认证相关接口")
|
||
@Slf4j
|
||
public class AuthController {
|
||
|
||
@Autowired
|
||
private IUserService userService;
|
||
|
||
/**
|
||
* 用户登录
|
||
*/
|
||
@PostMapping("/login")
|
||
@Operation(summary = "用户登录", description = "根据用户名和密码进行登录验证")
|
||
public ResponseEntity<Map<String, Object>> login(
|
||
@Parameter(description = "登录信息", required = true)
|
||
@Valid @RequestBody LoginRequest loginRequest) {
|
||
log.info("用户登录:{}", loginRequest);
|
||
Map<String, Object> result = new HashMap<>();
|
||
try {
|
||
LoginResponse loginResponse = userService.login(loginRequest);
|
||
result.put("success", true);
|
||
result.put("message", "登录成功");
|
||
result.put("data", loginResponse);
|
||
return ResponseEntity.ok(result);
|
||
} catch (Exception e) {
|
||
result.put("success", false);
|
||
result.put("message", "登录失败:" + e.getMessage());
|
||
return ResponseEntity.badRequest().body(result);
|
||
}
|
||
}
|
||
|
||
@Operation(summary = "通过token获取用户信息")
|
||
@GetMapping(value = "/getUserInfoByToken")
|
||
public ResponseEntity getUserInfoByToken(String token){
|
||
log.info("通过token获取用户信息:{}", token);
|
||
Map<String, Object> result = new HashMap<>();
|
||
// 用户信息/角色信息
|
||
User user = userService.getUserInfoByToken(token);
|
||
user.setPassword(null);
|
||
user.setOriginalPassword(null);
|
||
result.put("success", true);
|
||
result.put("data", user);
|
||
result.put("message", "token验证成功");
|
||
return ResponseEntity.ok(result);
|
||
}
|
||
|
||
/**
|
||
* 用户登出
|
||
*/
|
||
@PostMapping("/logout")
|
||
@Operation(summary = "用户登出", description = "清除用户token,实现登出功能")
|
||
public ResponseEntity<Map<String, Object>> logout(
|
||
@Parameter(description = "用户ID", required = true)
|
||
@RequestParam Integer userId) {
|
||
log.info("用户登出:{}", userId);
|
||
Map<String, Object> result = new HashMap<>();
|
||
try {
|
||
User user = userService.getById(userId);
|
||
if (user != null) {
|
||
// 清除token
|
||
user.setToken(null);
|
||
userService.updateById(user);
|
||
result.put("success", true);
|
||
result.put("message", "登出成功");
|
||
return ResponseEntity.ok(result);
|
||
} else {
|
||
result.put("success", false);
|
||
result.put("message", "用户不存在");
|
||
return ResponseEntity.badRequest().body(result);
|
||
}
|
||
} catch (Exception e) {
|
||
result.put("success", false);
|
||
result.put("message", "登出失败:" + e.getMessage());
|
||
return ResponseEntity.internalServerError().body(result);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 验证token
|
||
*/
|
||
@GetMapping("/verify")
|
||
@Operation(summary = "验证token", description = "验证用户token是否有效")
|
||
public ResponseEntity<Map<String, Object>> verifyToken(
|
||
@Parameter(description = "token", required = true)
|
||
@RequestParam String token) {
|
||
log.info("验证token:{}", token);
|
||
Map<String, Object> result = new HashMap<>();
|
||
try {
|
||
// 根据token查询用户
|
||
User user = userService.getUserByToken(token);
|
||
if (user != null) {
|
||
result.put("success", true);
|
||
result.put("message", "token有效");
|
||
result.put("data", user);
|
||
return ResponseEntity.ok(result);
|
||
} else {
|
||
result.put("success", false);
|
||
result.put("message", "token无效或已过期");
|
||
return ResponseEntity.badRequest().body(result);
|
||
}
|
||
} catch (Exception e) {
|
||
result.put("success", false);
|
||
result.put("message", "验证失败:" + e.getMessage());
|
||
return ResponseEntity.internalServerError().body(result);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取当前用户信息
|
||
*/
|
||
@GetMapping("/current")
|
||
@Operation(summary = "获取当前用户信息", description = "根据token获取当前登录用户信息")
|
||
public ResponseEntity<Map<String, Object>> getCurrentUser(
|
||
@Parameter(description = "token", required = true)
|
||
@RequestParam String token) {
|
||
log.info("获取当前用户信息:{}", token);
|
||
Map<String, Object> result = new HashMap<>();
|
||
try {
|
||
User user = userService.getUserByToken(token);
|
||
if (user != null) {
|
||
// 不返回密码等敏感信息
|
||
user.setPassword(null);
|
||
result.put("success", true);
|
||
result.put("message", "获取成功");
|
||
result.put("data", user);
|
||
return ResponseEntity.ok(result);
|
||
} else {
|
||
result.put("success", false);
|
||
result.put("message", "用户未登录或token已过期");
|
||
return ResponseEntity.badRequest().body(result);
|
||
}
|
||
} catch (Exception e) {
|
||
result.put("success", false);
|
||
result.put("message", "获取用户信息失败:" + e.getMessage());
|
||
return ResponseEntity.internalServerError().body(result);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|