AuthController.java
Springboot/bookstore-api/src/main/java/com/example/bookstore/controller/AuthController.java
package com.example.bookstore.controller;
import com.example.bookstore.dto.AuthResponse;
import com.example.bookstore.dto.LoginRequest;
import com.example.bookstore.dto.RegisterRequest;
import com.example.bookstore.dto.UserResponse;
import com.example.bookstore.service.AuthService;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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;
/**
* 認證 Controller:提供「註冊」與「登入」兩個公開 API。
* (SecurityConfig 中已設定 /api/auth/** 為 permitAll,不需要 token)
*
* @RestController = @Controller + @ResponseBody:
* 方法的回傳值會自動被 Jackson 序列化成 JSON 寫入回應本文。
* @RequestMapping("/api/auth"):
* 此類別所有 API 的共同路徑前綴。
*/
@RestController
@RequestMapping("/api/auth")
public class AuthController {
private final AuthService authService;
public AuthController(AuthService authService) {
this.authService = authService;
}
/**
* 註冊新使用者。
*
* POST /api/auth/register
* Body: { "username": "student01", "password": "Passw0rd!", "email": "s01@example.com" }
*
* @Valid :觸發 RegisterRequest 上的驗證註解,失敗回 400
* @RequestBody :把請求本文的 JSON 轉成 RegisterRequest 物件
* @return 201 Created + 新使用者資料(不含密碼)
*/
@PostMapping("/register")
public ResponseEntity<UserResponse> register(@Valid @RequestBody RegisterRequest request) {
UserResponse created = authService.register(request);
// 建立資源成功,回 201 Created(而不是籠統的 200)
return ResponseEntity.status(HttpStatus.CREATED).body(created);
}
/**
* 登入並取得 JWT。
*
* POST /api/auth/login
* Body: { "username": "user", "password": "User@123" }
*
* @return 200 OK + token(客戶端之後要放在 Authorization: Bearer <token>)
* 帳密錯誤則回 401(由 GlobalExceptionHandler 處理)
*/
@PostMapping("/login")
public ResponseEntity<AuthResponse> login(@Valid @RequestBody LoginRequest request) {
return ResponseEntity.ok(authService.login(request));
}
}
Related articles
BookstoreApiApplication.java
BookstoreApiApplication.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/BookstoreApiApplication.java).
Read article →DataInitializer.java
DataInitializer.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/config/DataInitializer.java).
Read article →SecurityConfig.java
SecurityConfig.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/config/SecurityConfig.java).
Read article →BookController.java
BookController.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/controller/BookController.java).
Read article →ApiErrorResponse.java
ApiErrorResponse.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/dto/ApiErrorResponse.java).
Read article →AuthResponse.java
AuthResponse.java — java source code from the Springboot learning materials (Springboot/bookstore-api/src/main/java/com/example/bookstore/dto/AuthResponse.java).
Read article →