-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
56 changed files
with
1,064 additions
and
231 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
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,26 +1,38 @@ | ||
plugins { | ||
kotlin("jvm") | ||
kotlin("jvm") | ||
} | ||
tasks.jar { | ||
enabled = false | ||
enabled = false | ||
} | ||
|
||
dependencies { | ||
implementation(project(":plu-domain")) | ||
implementation(project(":plu-external")) | ||
implementation(project(":plu-common")) | ||
implementation(project(":plu-domain")) | ||
implementation(project(":plu-external")) | ||
implementation(project(":plu-common")) | ||
|
||
// web | ||
implementation("org.springframework.boot:spring-boot-starter-web") | ||
// web | ||
implementation("org.springframework.boot:spring-boot-starter-web") | ||
|
||
// Redis | ||
implementation("org.springframework.boot:spring-boot-starter-data-redis") | ||
implementation("org.springframework.session:spring-session-data-redis") | ||
implementation(kotlin("stdlib-jdk8")) | ||
// validation | ||
implementation("org.springframework.boot:spring-boot-starter-validation") | ||
implementation("jakarta.validation:jakarta.validation-api:3.0.2") | ||
|
||
// swagger | ||
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0") | ||
|
||
// Redis | ||
implementation("org.springframework.boot:spring-boot-starter-data-redis") | ||
implementation("org.springframework.session:spring-session-data-redis") | ||
implementation(kotlin("stdlib-jdk8")) | ||
|
||
//jwt | ||
implementation("io.jsonwebtoken:jjwt-api:0.11.2") | ||
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.11.2") | ||
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.11.2") | ||
} | ||
repositories { | ||
mavenCentral() | ||
mavenCentral() | ||
} | ||
kotlin { | ||
jvmToolchain(17) | ||
jvmToolchain(17) | ||
} |
2 changes: 1 addition & 1 deletion
2
...in/kotlin/com/th/plu/PluApiApplication.kt → ...otlin/com/th/plu/api/PluApiApplication.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
24 changes: 24 additions & 0 deletions
24
plu-api/src/main/kotlin/com/th/plu/api/config/WebConfig.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.th.plu.api.config | ||
|
||
import com.th.plu.api.config.interceptor.AuthInterceptor | ||
import com.th.plu.api.config.resolver.MemberIdResolver | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer | ||
|
||
@Configuration | ||
class WebConfig( | ||
private val authInterceptor: AuthInterceptor, | ||
private val memberIdResolver: MemberIdResolver | ||
) : WebMvcConfigurer { | ||
|
||
override fun addInterceptors(registry: InterceptorRegistry) { | ||
registry.addInterceptor(authInterceptor) | ||
} | ||
|
||
override fun addArgumentResolvers(resolvers: MutableList<HandlerMethodArgumentResolver>) { | ||
resolvers.add(memberIdResolver) | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
plu-api/src/main/kotlin/com/th/plu/api/config/interceptor/Auth.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,5 @@ | ||
package com.th.plu.api.config.interceptor | ||
|
||
@Target(AnnotationTarget.FUNCTION) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class Auth |
24 changes: 24 additions & 0 deletions
24
plu-api/src/main/kotlin/com/th/plu/api/config/interceptor/AuthInterceptor.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.th.plu.api.config.interceptor | ||
|
||
import com.th.plu.common.constant.JwtKey | ||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.method.HandlerMethod | ||
import org.springframework.web.servlet.HandlerInterceptor | ||
|
||
@Component | ||
class AuthInterceptor( | ||
private val loginCheckHandler: LoginCheckHandler | ||
) : HandlerInterceptor { | ||
|
||
override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean { | ||
if (handler !is HandlerMethod) { | ||
return true | ||
} | ||
handler.getMethodAnnotation(Auth::class.java) ?: return true | ||
val memberId = loginCheckHandler.getMemberId(request) | ||
request.setAttribute(JwtKey.MEMBER_ID, memberId) | ||
return true | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
plu-api/src/main/kotlin/com/th/plu/api/config/interceptor/LoginCheckHandler.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,23 @@ | ||
package com.th.plu.api.config.interceptor | ||
|
||
import com.th.plu.api.service.auth.jwt.JwtHandler | ||
import com.th.plu.common.exception.code.ErrorCode | ||
import com.th.plu.common.exception.model.UnauthorizedException | ||
import jakarta.servlet.http.HttpServletRequest | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class LoginCheckHandler( | ||
private val jwtHandler: JwtHandler | ||
) { | ||
fun getMemberId(request: HttpServletRequest): Long { | ||
val bearerToken: String? = request.getHeader("Authorization") | ||
if (!bearerToken.isNullOrBlank() && bearerToken.startsWith("Bearer ")) { | ||
val accessToken = bearerToken.substring("Bearer ".length) | ||
if (jwtHandler.validateToken(accessToken)) { | ||
return jwtHandler.getMemberIdFromJwt(accessToken) | ||
} | ||
} | ||
throw UnauthorizedException(ErrorCode.UNAUTHORIZED_EXCEPTION, "잘못된 JWT $bearerToken 입니다.") | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
plu-api/src/main/kotlin/com/th/plu/api/config/resolver/MemberId.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,5 @@ | ||
package com.th.plu.api.config.resolver | ||
|
||
@Target(AnnotationTarget.VALUE_PARAMETER) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class MemberId |
37 changes: 37 additions & 0 deletions
37
plu-api/src/main/kotlin/com/th/plu/api/config/resolver/MemberIdResolver.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,37 @@ | ||
package com.th.plu.api.config.resolver | ||
|
||
import com.th.plu.api.config.interceptor.Auth | ||
import com.th.plu.common.constant.JwtKey | ||
import com.th.plu.common.exception.code.ErrorCode | ||
import com.th.plu.common.exception.model.InternalServerException | ||
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 | ||
|
||
@Component | ||
class MemberIdResolver : HandlerMethodArgumentResolver { | ||
|
||
override fun supportsParameter(parameter: MethodParameter): Boolean { | ||
return parameter.hasParameterAnnotation(MemberId::class.java) && Long::class.java == parameter.parameterType | ||
} | ||
|
||
override fun resolveArgument( | ||
parameter: MethodParameter, mavContainer: ModelAndViewContainer?, | ||
webRequest: NativeWebRequest, binderFactory: WebDataBinderFactory? | ||
): Any? { | ||
parameter.getMethodAnnotation(Auth::class.java) | ||
?: throw InternalServerException( | ||
ErrorCode.INTERNAL_SERVER_EXCEPTION, | ||
"인증이 필요한 컨트롤러 입니다. @Auth 어노테이션을 붙여주세요." | ||
) | ||
|
||
return webRequest.getAttribute(JwtKey.MEMBER_ID, 0) | ||
?: throw InternalServerException( | ||
ErrorCode.INTERNAL_SERVER_EXCEPTION, | ||
"MEMBER_ID 를 가져오지 못했습니다. ($parameter.javaClass - $parameter.method)" | ||
) | ||
} | ||
} |
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
122 changes: 0 additions & 122 deletions
122
plu-api/src/main/kotlin/com/th/plu/api/controller/ExceptionController.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.