-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 전역 예외 핸들러 추가 * feat: ErrorResponse 추가 * feat: ErrorCode 추가 * feat: CustomException 추가 * feat: JwtFilter의 예외 처리 필터 추가 * refactor: 토큰 파싱 과정의 예외를 CustomException으로 변경 * remove: CustomException 던지므로 throws 제거 * refactor: parseAccessToken 내부에서 CustomException 던지므로 try-catch 제거 * typo: 오타 수정 * feat: Jwt 관련 ErrorCode 추가 * feat: JwtFilter의 예외 처리 필터 추가 * fix: 재발급 메서드에서 잡을 수 있도록 수정 * fix: try-catch로 만료 토큰의 경우 null을 반환 * fix: 토큰 조회 메서드 수정 기존 메서드는 데이터의 유무와 무관하게 항상 비어있는 Optional을 반환함. 사용하지 않는 Repository의 메서드는 제거함. * fix: refreshToken을 accessToken으로 수정 accessToken을 받아야 하는 메서드인데 refreshToken을 받고 있었음.
- Loading branch information
1 parent
827bd86
commit 8dcf2e1
Showing
10 changed files
with
114 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 1 addition & 4 deletions
5
src/main/java/com/gdschongik/gdsc/domain/auth/dao/RefreshTokenRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
package com.gdschongik.gdsc.domain.auth.dao; | ||
|
||
import com.gdschongik.gdsc.domain.auth.domain.RefreshToken; | ||
import java.util.Optional; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
public interface RefreshTokenRepository extends CrudRepository<RefreshToken, Long> { | ||
Optional<RefreshToken> findByMemberId(Long aLong); | ||
} | ||
public interface RefreshTokenRepository extends CrudRepository<RefreshToken, Long> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/gdschongik/gdsc/global/exception/CustomException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.gdschongik.gdsc.global.exception; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CustomException extends RuntimeException { | ||
|
||
private final ErrorCode errorCode; | ||
|
||
public CustomException(ErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.errorCode = errorCode; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/gdschongik/gdsc/global/exception/ErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.gdschongik.gdsc.global.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ErrorCode { | ||
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 에러입니다."), | ||
|
||
// Jwt | ||
INVALID_JWT_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 JWT 토큰입니다."), | ||
EXPIRED_JWT_TOKEN(HttpStatus.UNAUTHORIZED, "만료된 JWT 토큰입니다."); | ||
|
||
private final HttpStatus status; | ||
private final String message; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/gdschongik/gdsc/global/exception/ErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.gdschongik.gdsc.global.exception; | ||
|
||
public record ErrorResponse(String errorCodeName, String errorMessage) { | ||
public static ErrorResponse of(ErrorCode errorCode) { | ||
return new ErrorResponse(errorCode.name(), errorCode.getMessage()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/gdschongik/gdsc/global/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.gdschongik.gdsc.global.exception; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
@ExceptionHandler(CustomException.class) | ||
public ResponseEntity<ErrorResponse> handleCustomException(CustomException e) { | ||
log.error("CustomException : {}", e.getMessage(), e); | ||
return ResponseEntity.status(e.getErrorCode().getStatus()).body(ErrorResponse.of(e.getErrorCode())); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ErrorResponse> handleException(Exception e) { | ||
log.error("INTERNAL_SERVER_ERROR : {}", e.getMessage(), e); | ||
return ResponseEntity.status(ErrorCode.INTERNAL_SERVER_ERROR.getStatus()) | ||
.body(ErrorResponse.of(ErrorCode.INTERNAL_SERVER_ERROR)); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/gdschongik/gdsc/global/security/JwtExceptionFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.gdschongik.gdsc.global.security; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.gdschongik.gdsc.global.exception.CustomException; | ||
import com.gdschongik.gdsc.global.exception.ErrorResponse; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class JwtExceptionFilter extends OncePerRequestFilter { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
try { | ||
filterChain.doFilter(request, response); | ||
} catch (CustomException e) { | ||
log.error("JWTException : {}", e.getMessage(), e); | ||
response.setCharacterEncoding("UTF-8"); | ||
response.setContentType("application/json"); | ||
response.setStatus(e.getErrorCode().getStatus().value()); | ||
response.getWriter().write(objectMapper.writeValueAsString(ErrorResponse.of(e.getErrorCode()))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters