-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: (BRD-74) 액세스토큰 인증 기능 추가 #53
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
cf8761f
Feature: (BRD-74) 스프링 시큐리티 필터체인 기본 설정
ttasjwi c431959
Feature: (BRD-74) AccessTokenManager - 액세스토큰 현재유효성 체크기능 추가
ttasjwi 73b703d
Feature: (BRD-74) BearerTokenResolver 정의(Authorization 헤더에서 토큰값 분리)
ttasjwi 791bfda
Feature: (BRD-74) AccessTokenManagerFixture 구현 변경
ttasjwi ea2c6f5
Feature: (BRD-74) 인증회원에 대응하는 Authentication(Spring Security) 구현
ttasjwi 9e047ae
Feature: (BRD-74) 액세스토큰 인증 필터 구현
ttasjwi c83517b
Feature: (BRD-74) 액세스토큰 인증필터를 필터체인에 포함
ttasjwi 100401a
Chore: (BRD-74) external-security 모듈 내 패키지 구조 수정
ttasjwi d03a28e
Feature: (BRD-74) 커스텀 AuthenticationEntryPoint, AccessDeniedHandler 설정
ttasjwi 230ad52
Feature: (BRD-74) NoResourceFoundException 처리
ttasjwi edcd3ab
Feature: (BRD-74) 인증필요/인가실패 예외 정의
ttasjwi 435a828
Feature: (BRD-74) 인증필요/인가실패 예외처리 개선
ttasjwi 1602a65
Feature: (BRD-74) 메시지/국제화 파일 추가 작성
ttasjwi 324ba8a
Fix: (BRD-74) AccessToken.checkCurrentlyValid 접근제어자 internal 로 수정
ttasjwi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
...tlin/com/ttasjwi/board/system/auth/domain/external/spring/security/BearerTokenResolver.kt
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,20 @@ | ||
package com.ttasjwi.board.system.auth.domain.external.spring.security | ||
|
||
import com.ttasjwi.board.system.auth.domain.external.spring.security.exception.InvalidAuthorizationHeaderFormatException | ||
import jakarta.servlet.http.HttpServletRequest | ||
import org.springframework.http.HttpHeaders | ||
|
||
class BearerTokenResolver { | ||
|
||
private val bearerTokenHeaderName = HttpHeaders.AUTHORIZATION | ||
|
||
fun resolve(request: HttpServletRequest): String? { | ||
val authorizationHeader = request.getHeader(this.bearerTokenHeaderName) ?: return null | ||
|
||
if (!authorizationHeader.startsWith("Bearer ", ignoreCase = true)) { | ||
throw InvalidAuthorizationHeaderFormatException() | ||
} | ||
return authorizationHeader.substring(7) | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...th/domain/external/spring/security/exception/InvalidAuthorizationHeaderFormatException.kt
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,12 @@ | ||
package com.ttasjwi.board.system.auth.domain.external.spring.security.exception | ||
|
||
import com.ttasjwi.board.system.core.exception.CustomException | ||
import com.ttasjwi.board.system.core.exception.ErrorStatus | ||
|
||
class InvalidAuthorizationHeaderFormatException : CustomException( | ||
status = ErrorStatus.UNAUTHENTICATED, | ||
code = "Error.InvalidAuthorizationHeaderFormat", | ||
args = emptyList(), | ||
source = "authorizationHeader", | ||
debugMessage = "잘못된 Authorization 헤더 형식입니다. 토큰값을 Authorization 헤더에 'Bearer [토큰값]' 형식으로 보내주세요." | ||
) |
66 changes: 66 additions & 0 deletions
66
.../com/ttasjwi/board/system/auth/domain/external/spring/security/BearerTokenResolverTest.kt
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,66 @@ | ||
package com.ttasjwi.board.system.auth.domain.external.spring.security | ||
|
||
import com.ttasjwi.board.system.auth.domain.external.spring.security.exception.InvalidAuthorizationHeaderFormatException | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import jakarta.servlet.http.HttpServletRequest | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
import org.springframework.http.HttpHeaders | ||
|
||
@DisplayName("BearerTokenResolver: Authorization 헤더의 Bearer 뒤에 위치한 토큰값을 분리한다.") | ||
class BearerTokenResolverTest { | ||
private lateinit var bearerTokenResolver: BearerTokenResolver | ||
|
||
@BeforeEach | ||
fun setup() { | ||
bearerTokenResolver = BearerTokenResolver() | ||
} | ||
|
||
@Test | ||
@DisplayName("헤더값이 유효하다면, 토큰값이 성공적으로 분리된다.") | ||
fun testSuccess() { | ||
// given | ||
val request = mockk<HttpServletRequest>() | ||
every { request.getHeader(HttpHeaders.AUTHORIZATION) } returns "Bearer validToken123" | ||
|
||
|
||
// when | ||
val token = bearerTokenResolver.resolve(request) | ||
|
||
// then | ||
assertThat(token).isEqualTo("validToken123") | ||
verify { request.getHeader(HttpHeaders.AUTHORIZATION) } | ||
} | ||
|
||
@Test | ||
@DisplayName("Authorization 헤더값이 없을 경우 null 이 반환된다.") | ||
fun authorizationHeaderNull() { | ||
// given | ||
val request = mockk<HttpServletRequest>() | ||
every { request.getHeader(HttpHeaders.AUTHORIZATION) } returns null | ||
|
||
// when | ||
val token = bearerTokenResolver.resolve(request) | ||
|
||
// then | ||
assertThat(token).isNull() | ||
} | ||
|
||
@Test | ||
@DisplayName("Authorization 헤더값이 Bearer 로 시작하지 않으면 예외가 발생한다.") | ||
fun testBadAuthorizationHeader() { | ||
// given | ||
val request = mockk<HttpServletRequest>() | ||
every { request.getHeader(HttpHeaders.AUTHORIZATION) } returns "Basic abc123" | ||
|
||
// when | ||
// then | ||
assertThrows<InvalidAuthorizationHeaderFormatException> { bearerTokenResolver.resolve(request) } | ||
verify { request.getHeader(HttpHeaders.AUTHORIZATION) } | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...omain/external/spring/security/exception/InvalidAuthorizationHeaderFormatExceptionTest.kt
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,24 @@ | ||
package com.ttasjwi.board.system.auth.domain.external.spring.security.exception | ||
|
||
import com.ttasjwi.board.system.core.exception.ErrorStatus | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
|
||
@DisplayName("InvalidAuthorizationHeaderFormatException: Authorization 헤더값 포맷이 잘못된 형식일 때 발생하는 예외") | ||
class InvalidAuthorizationHeaderFormatExceptionTest { | ||
|
||
@Test | ||
@DisplayName("예외 기본값 테스트") | ||
fun test() { | ||
val exception = InvalidAuthorizationHeaderFormatException() | ||
|
||
assertThat(exception.status).isEqualTo(ErrorStatus.UNAUTHENTICATED) | ||
assertThat(exception.code).isEqualTo("Error.InvalidAuthorizationHeaderFormat") | ||
assertThat(exception.source).isEqualTo("authorizationHeader") | ||
assertThat(exception.args).isEmpty() | ||
assertThat(exception.message).isEqualTo(exception.debugMessage) | ||
assertThat(exception.cause).isNull() | ||
assertThat(exception.debugMessage).isEqualTo("잘못된 Authorization 헤더 형식입니다. 토큰값을 Authorization 헤더에 'Bearer [토큰값]' 형식으로 보내주세요.") | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.