AuthController.java
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.aigeo.auth.controller;
import com.aigeo.auth.dto.LoginRequest;
import com.aigeo.auth.dto.LoginResponse;
import com.aigeo.auth.dto.RegisterRequest;
import com.aigeo.auth.dto.RegisterResponse;
import com.aigeo.auth.service.AuthService;
import com.aigeo.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 认证控制器
*/
@Slf4j
@RestController
@RequestMapping("/auth")
@Tag(name = "认证管理", description = "用户登录、注册等认证相关接口")
public class AuthController {
private final AuthService authService;
public AuthController(AuthService authService) {
this.authService = authService;
}
@PostMapping("/login")
@Operation(summary = "用户登录", description = "用户登录接口")
public Result<LoginResponse> login(@Valid @RequestBody LoginRequest loginRequest) {
try {
LoginResponse response = authService.login(loginRequest);
return Result.success("登录成功", response);
} catch (Exception e) {
log.error("用户登录失败: {}", loginRequest.getUsername(), e);
return Result.error(500, "登录失败:" + e.getMessage());
}
}
@PostMapping("/register")
@Operation(summary = "用户注册", description = "用户注册接口")
public Result<RegisterResponse> register(@Valid @RequestBody RegisterRequest registerRequest) {
try {
RegisterResponse response = authService.register(registerRequest);
return Result.success("注册成功", response);
} catch (Exception e) {
log.error("用户注册失败: {}", registerRequest.getUsername(), e);
return Result.error(500, "注册失败:" + e.getMessage());
}
}
}