- 로그인/비밀번호 재인증에 실패 기반 레이트리밋(Redis 슬라이딩 윈도우) · 로그인 IP당 10회/10분, 비밀번호 게이트 회원당 5회/10분, 초과 시 429 · 성공 시 실패 카운터 초기화, Redis 장애 시 통과(가용성 우선) · 회원가입 레이트리밋도 공통 enforceRateLimit 으로 일원화 - GlobalExceptionHandler: 미처리 500 로그에 메서드+경로 기록(추적성), 잘못된 JSON은 400 - 테스트: 레이트리밋 429 케이스 2개 추가 (백엔드 총 34) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
96 lines
3.7 KiB
Java
96 lines
3.7 KiB
Java
package com.sb.web.auth.controller;
|
|
|
|
import com.sb.web.auth.dto.LoginRequest;
|
|
import com.sb.web.auth.dto.LoginResponse;
|
|
import com.sb.web.auth.dto.MemberResponse;
|
|
import com.sb.web.auth.dto.PasswordChangeRequest;
|
|
import com.sb.web.auth.dto.SessionUser;
|
|
import com.sb.web.auth.dto.SignupRequest;
|
|
import com.sb.web.auth.service.AuthService;
|
|
import com.sb.web.auth.web.AuthInterceptor;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.validation.Valid;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
/**
|
|
* 인증 API.
|
|
* POST /api/auth/signup 회원가입 (비보호)
|
|
* POST /api/auth/login 로그인 (비보호)
|
|
* POST /api/auth/logout 로그아웃 (보호)
|
|
* GET /api/auth/me 내 정보 (보호)
|
|
*/
|
|
@RestController
|
|
@RequestMapping("/api/auth")
|
|
@RequiredArgsConstructor
|
|
public class AuthController {
|
|
|
|
private final AuthService authService;
|
|
private final com.sb.web.admin.service.AppSettingService appSettingService;
|
|
|
|
/** 회원가입 허용 여부 (비보호) — 프론트가 가입 진입 전에 차단 표시용 */
|
|
@GetMapping("/signup-enabled")
|
|
public java.util.Map<String, Boolean> signupEnabled() {
|
|
return java.util.Map.of("enabled", appSettingService.isSignupEnabled());
|
|
}
|
|
|
|
@PostMapping("/signup")
|
|
public ResponseEntity<MemberResponse> signup(@Valid @RequestBody SignupRequest req,
|
|
HttpServletRequest request) {
|
|
return ResponseEntity.status(HttpStatus.CREATED).body(authService.signup(req, clientIp(request)));
|
|
}
|
|
|
|
/** 클라이언트 IP (nginx 프록시 뒤 → X-Forwarded-For 우선) */
|
|
private String clientIp(HttpServletRequest request) {
|
|
String xff = request.getHeader("X-Forwarded-For");
|
|
if (xff != null && !xff.isBlank()) {
|
|
return xff.split(",")[0].trim();
|
|
}
|
|
return request.getRemoteAddr();
|
|
}
|
|
|
|
@PostMapping("/login")
|
|
public LoginResponse login(@Valid @RequestBody LoginRequest req, HttpServletRequest request) {
|
|
return authService.login(req, clientIp(request));
|
|
}
|
|
|
|
@PostMapping("/logout")
|
|
public ResponseEntity<Void> logout(HttpServletRequest request) {
|
|
authService.logout(AuthInterceptor.resolveToken(request));
|
|
return ResponseEntity.noContent().build();
|
|
}
|
|
|
|
@GetMapping("/me")
|
|
public SessionUser me(@RequestAttribute(AuthInterceptor.CURRENT_MEMBER) SessionUser current) {
|
|
return current;
|
|
}
|
|
|
|
@PutMapping("/password")
|
|
public ResponseEntity<Void> changePassword(
|
|
@Valid @RequestBody PasswordChangeRequest req,
|
|
@RequestAttribute(AuthInterceptor.CURRENT_MEMBER) SessionUser current) {
|
|
authService.changePassword(current.getId(), req);
|
|
return ResponseEntity.noContent().build();
|
|
}
|
|
|
|
/** 가입정보 변경 진입 전 비밀번호 재인증 */
|
|
@PostMapping("/verify-password")
|
|
public ResponseEntity<Void> verifyPassword(
|
|
@Valid @RequestBody com.sb.web.auth.dto.PasswordVerifyRequest req,
|
|
@RequestAttribute(AuthInterceptor.CURRENT_MEMBER) SessionUser current) {
|
|
authService.verifyPassword(current.getId(), req.getPassword());
|
|
return ResponseEntity.noContent().build();
|
|
}
|
|
|
|
/** 가입정보(이름/이메일) 변경 */
|
|
@PutMapping("/profile")
|
|
public MemberResponse updateProfile(
|
|
@Valid @RequestBody com.sb.web.auth.dto.ProfileUpdateRequest req,
|
|
@RequestAttribute(AuthInterceptor.CURRENT_MEMBER) SessionUser current,
|
|
HttpServletRequest request) {
|
|
return authService.updateProfile(current.getId(), AuthInterceptor.resolveToken(request), req);
|
|
}
|
|
}
|