-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from Katchup-dev/task#22
[KS-12] API 요청 토큰 검증 Argument Resolver 구현
- Loading branch information
Showing
8 changed files
with
91 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package site.katchup.springboot.auth | ||
|
||
@Target(AnnotationTarget.VALUE_PARAMETER) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class Auth() |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/site/katchup/springboot/auth/AuthConfig.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,15 @@ | ||
package site.katchup.springboot.auth | ||
|
||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer | ||
|
||
@Configuration | ||
class AuthConfig( | ||
private val authorizationArgumentResolver: AuthorizationArgumentResolver, | ||
) : WebMvcConfigurer { | ||
|
||
override fun addArgumentResolvers(resolvers: MutableList<HandlerMethodArgumentResolver>) { | ||
resolvers.add(authorizationArgumentResolver) | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/kotlin/site/katchup/springboot/auth/AuthorizationArgumentResolver.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,49 @@ | ||
package site.katchup.springboot.auth | ||
|
||
import jakarta.servlet.http.HttpServletRequest | ||
import org.springframework.core.MethodParameter | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.bind.support.WebDataBinderFactory | ||
import org.springframework.web.context.request.NativeWebRequest | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver | ||
import org.springframework.web.method.support.ModelAndViewContainer | ||
import site.katchup.springboot.exception.auth.InvalidTokenException | ||
import site.katchup.springboot.global.message.FailMessage | ||
import site.katchup.springboot.repository.MemberRepository | ||
|
||
@Component | ||
class AuthorizationArgumentResolver( | ||
private val tokenValidator: TokenValidator, | ||
private val memberRepository: MemberRepository, | ||
) : HandlerMethodArgumentResolver { | ||
|
||
companion object { | ||
private const val AUTHORIZATION = "Authorization" | ||
} | ||
override fun supportsParameter(parameter: MethodParameter): Boolean { | ||
return parameter.hasParameterAnnotation(Auth::class.java) | ||
} | ||
|
||
override fun resolveArgument( | ||
parameter: MethodParameter, | ||
mavContainer: ModelAndViewContainer?, | ||
webRequest: NativeWebRequest, | ||
binderFactory: WebDataBinderFactory?, | ||
): Any? { | ||
val request = webRequest.getNativeRequest(HttpServletRequest::class.java) | ||
val jwt = request?.let { extractToken(it) } ?: throw InvalidTokenException(FailMessage.INVALID_TOKEN) | ||
val memberId = tokenValidator.validate(jwt) | ||
if (!memberRepository.existsById(memberId)) { | ||
throw InvalidTokenException(FailMessage.INVALID_TOKEN) | ||
} | ||
return memberId | ||
} | ||
|
||
private fun extractToken(request: HttpServletRequest): String { | ||
val token = request.getHeader(AUTHORIZATION) | ||
if (token == null || !token.startsWith("Bearer ")) { | ||
throw InvalidTokenException(FailMessage.INVALID_TOKEN) | ||
} | ||
return token.substring(7) | ||
} | ||
} |
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
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
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