Skip to content

Commit

Permalink
feat: 로그인 에러 핸들링 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
70825 committed Aug 3, 2023
1 parent 108d055 commit 8232f2b
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.funeat.auth.exception;

public class LoginException extends RuntimeException {

public LoginException(final String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.funeat.auth.util;

import com.funeat.auth.exception.LoginException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

Expand All @@ -12,12 +12,9 @@ public class AuthHandlerInterceptor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
final HttpMethod httpMethod = HttpMethod.valueOf(request.getMethod());
if (httpMethod.equals(HttpMethod.POST) || httpMethod.equals(HttpMethod.PATCH)) {
final HttpSession session = request.getSession();
if (session.getAttribute("member") == null) {
throw new IllegalArgumentException("login error");
}
final HttpSession session = request.getSession();
if (session.getAttribute("member") == null) {
throw new LoginException("login error");
}
return true;
}
Expand Down

This file was deleted.

9 changes: 2 additions & 7 deletions backend/src/main/java/com/funeat/common/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.funeat.auth.util.AuthArgumentResolver;
import com.funeat.auth.util.AuthHandlerInterceptor;
import com.funeat.auth.util.MemberHandlerInterceptor;
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
Expand All @@ -16,23 +15,19 @@ public class WebConfig implements WebMvcConfigurer {
private final CustomPageableHandlerMethodArgumentResolver customPageableHandlerMethodArgumentResolver;
private final AuthArgumentResolver authArgumentResolver;
private final AuthHandlerInterceptor authHandlerInterceptor;
private final MemberHandlerInterceptor memberHandlerInterceptor;

public WebConfig(final CustomPageableHandlerMethodArgumentResolver customPageableHandlerMethodArgumentResolver,
final AuthArgumentResolver authArgumentResolver,
final AuthHandlerInterceptor authHandlerInterceptor,
final MemberHandlerInterceptor memberHandlerInterceptor) {
final AuthHandlerInterceptor authHandlerInterceptor) {
this.customPageableHandlerMethodArgumentResolver = customPageableHandlerMethodArgumentResolver;
this.authArgumentResolver = authArgumentResolver;
this.authHandlerInterceptor = authHandlerInterceptor;
this.memberHandlerInterceptor = memberHandlerInterceptor;
}

@Override
public void addInterceptors(final InterceptorRegistry registry) {
registry.addInterceptor(authHandlerInterceptor)
.addPathPatterns("/api/products/**/reviews/**");
registry.addInterceptor(memberHandlerInterceptor)
.addPathPatterns("/api/products/**/reviews/**")
.addPathPatterns("/api/members");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.funeat.exception.presentation;

import com.funeat.auth.exception.LoginException;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalControllerAdvice {

private final Logger log = LoggerFactory.getLogger(this.getClass());

@ExceptionHandler(LoginException.class)
public ResponseEntity<?> loginExceptionHandler(final LoginException e, final HttpServletRequest request) {

log.warn("URI: {}, 쿠키값: {}, 저장된 JSESSIONID 값: {}", request.getRequestURI(), request.getHeader("Cookie"),
request.getSession().getId());

return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public ResponseEntity<Void> toggleLikeReview(@PathVariable Long reviewId,
}

@GetMapping("/api/products/{productId}/reviews")
public ResponseEntity<SortingReviewsResponse> getSortingReviews(@PathVariable Long productId,
public ResponseEntity<SortingReviewsResponse> getSortingReviews(@AuthenticationPrincipal LoginInfo loginInfo,
@PathVariable Long productId,
@PageableDefault Pageable pageable) {
final SortingReviewsResponse response = reviewService.sortingReviews(productId, pageable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ ResponseEntity<Void> toggleLikeReview(@PathVariable Long reviewId, @Authenticati
description = "리뷰 정렬후 조회 성공."
)
@GetMapping
ResponseEntity<SortingReviewsResponse> getSortingReviews(@PathVariable Long productId,
ResponseEntity<SortingReviewsResponse> getSortingReviews(@AuthenticationPrincipal LoginInfo loginInfo,
@PathVariable Long productId,
@PageableDefault Pageable pageable);

@Operation(summary = "리뷰 랭킹 Top3 조회", description = "리뷰 랭킹 Top3 조회한다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,10 @@ class 좋아요_기준_내림차순으로_리뷰_목록_조회 {

final var sortingReviews = List.of(review2, review3, review1);
final var pageDto = new SortingReviewsPageDto(3L, 1L, true, true, 0L, 10L);
final var loginCookie = 로그인_쿠키를_얻는다();

// when
final var response = 정렬된_리뷰_목록_조회_요청(productId, "favoriteCount,desc", 0);
final var response = 정렬된_리뷰_목록_조회_요청(loginCookie, productId, "favoriteCount,desc", 0);

// then
STATUS_CODE_검증한다(response, 정상_처리);
Expand Down Expand Up @@ -188,9 +189,10 @@ class 좋아요_기준_내림차순으로_리뷰_목록_조회 {

final var sortingReviews = List.of(review4, review3, review2, review1);
final var pageDto = new SortingReviewsPageDto(4L, 1L, true, true, 0L, 10L);
final var loginCookie = 로그인_쿠키를_얻는다();

// when
final var response = 정렬된_리뷰_목록_조회_요청(productId, "favoriteCount,desc", 0);
final var response = 정렬된_리뷰_목록_조회_요청(loginCookie, productId, "favoriteCount,desc", 0);

// then
STATUS_CODE_검증한다(response, 정상_처리);
Expand Down Expand Up @@ -223,9 +225,10 @@ class 평점_기준_오름차순으로_리뷰_목록을_조회 {
복수_리뷰_(reviews);

final var sortingReviews = List.of(review1, review3, review2);
final var loginCookie = 로그인_쿠키를_얻는다();

// when
final var response = 정렬된_리뷰_목록_조회_요청(productId, "rating,asc", 0);
final var response = 정렬된_리뷰_목록_조회_요청(loginCookie, productId, "rating,asc", 0);
final var page = new SortingReviewsPageDto(3L, 1L, true, true, 0L, 10L);

// then
Expand Down Expand Up @@ -258,9 +261,10 @@ class 평점_기준_오름차순으로_리뷰_목록을_조회 {

final var sortingReviews = List.of(review4, review3, review2, review1);
final var page = new SortingReviewsPageDto(4L, 1L, true, true, 0L, 10L);
final var loginCookie = 로그인_쿠키를_얻는다();

// when
final var response = 정렬된_리뷰_목록_조회_요청(productId, "rating,asc", 0);
final var response = 정렬된_리뷰_목록_조회_요청(loginCookie, productId, "rating,asc", 0);

// then
STATUS_CODE_검증한다(response, 정상_처리);
Expand Down Expand Up @@ -293,9 +297,10 @@ class 최신순으로_리뷰_목록을_조회 {
복수_리뷰_(reviews);

final var sortingReviews = List.of(review3, review2, review1);
final var loginCookie = 로그인_쿠키를_얻는다();

// when
final var response = 정렬된_리뷰_목록_조회_요청(productId, "createdAt,desc", 0);
final var response = 정렬된_리뷰_목록_조회_요청(loginCookie, productId, "createdAt,desc", 0);
final var page = new SortingReviewsPageDto(3L, 1L, true, true, 0L, 10L);

// then
Expand Down Expand Up @@ -330,9 +335,10 @@ class 평점_기준_내림차순으로_리뷰_목록_조회 {

final var sortingReviews = List.of(review2, review3, review1);
final var page = new SortingReviewsPageDto(3L, 1L, true, true, 0L, 10L);
final var loginCookie = 로그인_쿠키를_얻는다();

// when
final var response = 정렬된_리뷰_목록_조회_요청(productId, "rating,desc", 0);
final var response = 정렬된_리뷰_목록_조회_요청(loginCookie, productId, "rating,desc", 0);

// then
STATUS_CODE_검증한다(response, 정상_처리);
Expand Down Expand Up @@ -364,9 +370,10 @@ class 평점_기준_내림차순으로_리뷰_목록_조회 {

final var sortingReviews = List.of(review4, review3, review2, review1);
final var page = new SortingReviewsPageDto(4L, 1L, true, true, 0L, 10L);
final var loginCookie = 로그인_쿠키를_얻는다();

// when
final var response = 정렬된_리뷰_목록_조회_요청(productId, "rating,desc", 0);
final var response = 정렬된_리뷰_목록_조회_요청(loginCookie, productId, "rating,desc", 0);

// then
STATUS_CODE_검증한다(response, 정상_처리);
Expand Down Expand Up @@ -429,10 +436,12 @@ class 평점_기준_내림차순으로_리뷰_목록_조회 {
reviewRepository.saveAll(reviews);
}

private ExtractableResponse<Response> 정렬된_리뷰_목록_조회_요청(final Long productId,
private ExtractableResponse<Response> 정렬된_리뷰_목록_조회_요청(final String loginCookie,
final Long productId,
final String sort,
final Integer page) {
return given()
.cookie("JSESSIONID", loginCookie)
.queryParam("sort", sort)
.queryParam("page", page)
.when()
Expand Down

0 comments on commit 8232f2b

Please sign in to comment.