Skip to content

Commit

Permalink
feat: 예외 처리로직 수정
Browse files Browse the repository at this point in the history
`JwtAuthenticationEntryPoint` 에서 공통적으로 처리하게 함
  • Loading branch information
GGHDMS committed Dec 19, 2023
1 parent 40fbed1 commit 40ae31d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class CommonExceptionHandler {
}

@ExceptionHandler(RefreshTokenNotFoundException::class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseStatus(HttpStatus.NOT_FOUND)
fun handleRefreshTokenNotFoundException(
exception: RefreshTokenNotFoundException,
request: HttpServletRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.yourssu.ssudateserver.jwt.filter

import com.yourssu.ssudateserver.jwt.component.JwtProvider
import com.yourssu.ssudateserver.jwt.exception.AuthenticateException
import com.yourssu.ssudateserver.service.BlackTokenService
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.web.filter.OncePerRequestFilter
import javax.servlet.FilterChain
Expand All @@ -28,9 +29,13 @@ class JwtFilter(
return
}

val token = extractAccessTokenFromHeader(authorizationHeader)
val authentication = jwtProvider.authenticate(token)
SecurityContextHolder.getContext().authentication = authentication
try {
val token = extractAccessTokenFromHeader(authorizationHeader)
val authentication = jwtProvider.authenticate(token)
SecurityContextHolder.getContext().authentication = authentication
} catch (exception: AuthenticateException) {
request.setAttribute("AuthenticateException", exception)
}

filterChain.doFilter(request, response)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import com.yourssu.ssudateserver.exception.ErrorResponse
import com.yourssu.ssudateserver.jwt.exception.AuthenticateException
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.security.core.AuthenticationException
Expand All @@ -20,10 +21,12 @@ class JwtAuthenticationEntryPoint : AuthenticationEntryPoint {
response: HttpServletResponse,
ex: AuthenticationException,
) {
val exception = request.getAttribute("AuthenticateException") as? AuthenticateException

val errorResponse = ErrorResponse(
time = LocalDateTime.now(),
status = HttpStatus.UNAUTHORIZED,
message = ex.message!!,
message = exception?.message ?: ex.message!!,
requestURI = request.requestURI.toString(),
)

Expand Down

0 comments on commit 40ae31d

Please sign in to comment.