Skip to content

Commit a6e1887

Browse files
committed
refactor: MyBarController에 SecurityUser 기반 로그인 처리 로직 도입
- MyBarController의 모든 엔드포인트에서 `@AuthenticationPrincipal`을 활용하여 사용자 인증 정보를 처리하는 방식을 리팩토링
1 parent 9ffd623 commit a6e1887

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/main/java/com/back/domain/mybar/controller/MyBarController.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
import com.back.domain.mybar.dto.MyBarListResponseDto;
44
import com.back.domain.mybar.service.MyBarService;
5+
import com.back.global.exception.ServiceException;
56
import com.back.global.rsData.RsData;
7+
import com.back.global.security.SecurityUser;
68
import io.swagger.v3.oas.annotations.Operation;
79
import jakarta.validation.constraints.Max;
810
import jakarta.validation.constraints.Min;
911
import lombok.RequiredArgsConstructor;
12+
import org.springframework.format.annotation.DateTimeFormat;
1013
import org.springframework.security.core.annotation.AuthenticationPrincipal;
1114
import org.springframework.validation.annotation.Validated;
1215
import org.springframework.web.bind.annotation.*;
13-
import org.springframework.format.annotation.DateTimeFormat;
16+
1417
import java.time.LocalDateTime;
1518

1619
@RestController
@@ -35,16 +38,18 @@ public class MyBarController {
3538
* @return 킵 아이템 목록과 다음 페이지 커서
3639
*/
3740
@GetMapping
38-
@Operation(summary = "내 바 목록", description = "내가 킵한 칵테일 목록 조회. 무한스크롤 파라미터 지원")
41+
@Operation(summary = "내 바 목록", description = "내가 킵한 칵테일 목록 조회. 무한 스크롤 커서 지원")
3942
public RsData<MyBarListResponseDto> getMyBarList(
40-
@AuthenticationPrincipal(expression = "id") Long userId,
43+
@AuthenticationPrincipal(errorOnInvalidType = false) SecurityUser principal,
4144
@RequestParam(required = false)
4245
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime lastKeptAt,
4346
@RequestParam(required = false) Long lastId,
4447
@RequestParam(defaultValue = "20") @Min(1) @Max(100) int limit
4548
) {
49+
Long userId = principal != null ? principal.getId() : null;
50+
if (userId == null) throw new ServiceException(401, "로그인이 필요합니다.");
4651
MyBarListResponseDto body = myBarService.getMyBar(userId, lastKeptAt, lastId, limit);
47-
return RsData.successOf(body); // code=200, message="success"
52+
return RsData.successOf(body);
4853
}
4954

5055
/**
@@ -54,11 +59,13 @@ public RsData<MyBarListResponseDto> getMyBarList(
5459
* @return 201 kept
5560
*/
5661
@PostMapping("/{cocktailId}/keep")
57-
@Operation(summary = "킵 추가/복원", description = "해당 칵테일을 내 바에 킵합니다. 이미 삭제된 경우 복원")
62+
@Operation(summary = "킵 추가/복원", description = "해당 칵테일을 내 바에 킵합니다. 이미 삭제 상태면 복원")
5863
public RsData<Void> keep(
59-
@AuthenticationPrincipal(expression = "id") Long userId,
64+
@AuthenticationPrincipal(errorOnInvalidType = false) SecurityUser principal,
6065
@PathVariable Long cocktailId
6166
) {
67+
Long userId = principal != null ? principal.getId() : null;
68+
if (userId == null) throw new ServiceException(401, "로그인이 필요합니다.");
6269
myBarService.keep(userId, cocktailId);
6370
return RsData.of(201, "kept"); // Aspect가 HTTP 201로 설정
6471
}
@@ -70,12 +77,15 @@ public RsData<Void> keep(
7077
* @return 200 deleted
7178
*/
7279
@DeleteMapping("/{cocktailId}/keep")
73-
@Operation(summary = "킵 해제", description = "내 바에서 해당 칵테일 킵을 해제합니다(소프트 삭제, 멱등)")
80+
@Operation(summary = "킵 해제", description = "내 바에서 해당 칵테일을 삭제(소프트 삭제, 멱등)")
7481
public RsData<Void> unkeep(
75-
@AuthenticationPrincipal(expression = "id") Long userId,
82+
@AuthenticationPrincipal(errorOnInvalidType = false) SecurityUser principal,
7683
@PathVariable Long cocktailId
7784
) {
85+
Long userId = principal != null ? principal.getId() : null;
86+
if (userId == null) throw new ServiceException(401, "로그인이 필요합니다.");
7887
myBarService.unkeep(userId, cocktailId);
7988
return RsData.of(200, "deleted");
8089
}
8190
}
91+

0 commit comments

Comments
 (0)