-
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.
- Loading branch information
Showing
13 changed files
with
303 additions
and
88 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,14 +1,5 @@ | ||
# Use a base image for running the application | ||
FROM openjdk:17-jdk-slim | ||
|
||
# Set the working directory in the container | ||
WORKDIR /app | ||
|
||
# Copy the pre-built JAR file into the container | ||
COPY /app/build/libs/app-0.0.1-SNAPSHOT.jar app.jar | ||
|
||
# Expose port 8080 for the Spring application | ||
FROM openjdk:17 | ||
ARG JAR_FILE=build/libs/karrot-0.0.1-SNAPSHOT.jar | ||
COPY ${JAR_FILE} /app.jar | ||
EXPOSE 8080 | ||
|
||
# Specify the entry point for the application | ||
ENTRYPOINT ["java", "-jar", "app.jar"] | ||
ENTRYPOINT ["java", "-jar", "/app.jar"] |
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,17 @@ | ||
package com.toyProject7.karrot | ||
|
||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.HttpStatusCode | ||
|
||
open class DomainException( | ||
// client 와 약속된 Application Error 에 대한 코드 필요 시 Enum 으로 관리하자. | ||
val errorCode: Int, | ||
// HTTP Status Code, 비어있다면 500 이다. | ||
val httpErrorCode: HttpStatusCode = HttpStatus.INTERNAL_SERVER_ERROR, | ||
val msg: String, | ||
cause: Throwable? = null, | ||
) : RuntimeException(msg, cause) { | ||
override fun toString(): String { | ||
return "DomainException(msg='$msg', errorCode=$errorCode, httpErrorCode=$httpErrorCode)" | ||
} | ||
} |
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,4 +1,5 @@ | ||
package com.toyProject7.karrot.user | ||
|
||
class AuthUser { | ||
} | ||
@Target(AnnotationTarget.VALUE_PARAMETER) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class AuthUser |
38 changes: 37 additions & 1 deletion
38
src/main/kotlin/com/toyProject7/karrot/user/UserAccessTokenUtil.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 |
---|---|---|
@@ -1,4 +1,40 @@ | ||
package com.toyProject7.karrot.user | ||
|
||
class UserAccessTokenUtil { | ||
import io.jsonwebtoken.Jwts | ||
import io.jsonwebtoken.security.Keys | ||
import java.nio.charset.StandardCharsets | ||
import java.util.Date | ||
|
||
object UserAccessTokenUtil { | ||
private val SECRET_KEY = System.getenv("JWT_SECRET_KEY") | ||
?.let { Keys.hmacShaKeyFor(it.toByteArray(StandardCharsets.UTF_8)) } | ||
?: throw IllegalStateException("JWT_SECRET_KEY is not set!") | ||
|
||
private const val JWT_EXPIRATION_TIME = 1000 * 60 * 60 * 2 // 2 hours | ||
|
||
fun generateAccessToken(username: String): String { | ||
val now = Date() | ||
val expiryDate = Date(now.time + JWT_EXPIRATION_TIME) | ||
return Jwts.builder() | ||
.signWith(SECRET_KEY) | ||
.setSubject(username) | ||
.setIssuedAt(now) | ||
.setExpiration(expiryDate) | ||
.compact() | ||
} | ||
|
||
fun validateAccessTokenGetUserId(accessToken: String): String? { | ||
return try { | ||
val claims = | ||
Jwts.parserBuilder() | ||
.setSigningKey(SECRET_KEY) | ||
.build() | ||
.parseClaimsJws(accessToken) | ||
.body | ||
if (claims.expiration.before(Date())) null else claims.subject | ||
} catch (e: Exception) { | ||
println("Token validation failed. Please try again.") | ||
null | ||
} | ||
} | ||
} |
40 changes: 39 additions & 1 deletion
40
src/main/kotlin/com/toyProject7/karrot/user/UserArgumentResolver.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 |
---|---|---|
@@ -1,4 +1,42 @@ | ||
package com.toyProject7.karrot.user | ||
|
||
class UserArgumentResolver { | ||
import com.toyProject7.karrot.user.controller.User | ||
import com.toyProject7.karrot.user.service.UserService | ||
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 UserArgumentResolver( | ||
private val userService: UserService, | ||
) : HandlerMethodArgumentResolver { | ||
override fun supportsParameter(parameter: MethodParameter): Boolean { | ||
return parameter.parameterType == User::class.java | ||
} | ||
|
||
override fun resolveArgument( | ||
parameter: MethodParameter, | ||
mavContainer: ModelAndViewContainer?, | ||
webRequest: NativeWebRequest, | ||
binderFactory: WebDataBinderFactory?, | ||
): User? { | ||
return runCatching { | ||
val accessToken = | ||
requireNotNull( | ||
webRequest.getHeader("Authorization")?.split(" ")?.let { | ||
if (it.getOrNull(0) == "Bearer") it.getOrNull(1) else null | ||
}, | ||
) | ||
userService.authenticate(accessToken) | ||
}.getOrElse { | ||
if (parameter.hasParameterAnnotation(AuthUser::class.java)) { | ||
throw AuthenticateException() | ||
} else { | ||
null | ||
} | ||
} | ||
} | ||
} |
54 changes: 52 additions & 2 deletions
54
src/main/kotlin/com/toyProject7/karrot/user/UserException.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 |
---|---|---|
@@ -1,4 +1,54 @@ | ||
package com.toyProject7.karrot.user | ||
|
||
class UserException { | ||
} | ||
import com.toyProject7.karrot.DomainException | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.HttpStatusCode | ||
|
||
sealed class UserException( | ||
errorCode: Int, | ||
httpStatusCode: HttpStatusCode, | ||
msg: String, | ||
cause: Throwable? = null, | ||
) : DomainException(errorCode, httpStatusCode, msg, cause) | ||
|
||
class SignUpUserIdConflictException : UserException( | ||
errorCode = 0, | ||
httpStatusCode = HttpStatus.CONFLICT, | ||
msg = "Username conflict", | ||
) | ||
|
||
class SignUpNicknameConflictException : UserException( | ||
errorCode = 0, | ||
httpStatusCode = HttpStatus.CONFLICT, | ||
msg = "Nickname conflict", | ||
) | ||
|
||
class SignUpBadUserIdException : UserException( | ||
errorCode = 0, | ||
httpStatusCode = HttpStatus.BAD_REQUEST, | ||
msg = "Bad userId, User ID must be 5-20 characters", | ||
) | ||
|
||
class SignUpBadPasswordException : UserException( | ||
errorCode = 0, | ||
httpStatusCode = HttpStatus.BAD_REQUEST, | ||
msg = "Bad password, password must be 8-16 characters", | ||
) | ||
|
||
class SignInUserNotFoundException : UserException( | ||
errorCode = 0, | ||
httpStatusCode = HttpStatus.UNAUTHORIZED, | ||
msg = "User not found", | ||
) | ||
|
||
class SignInInvalidPasswordException : UserException( | ||
errorCode = 0, | ||
httpStatusCode = HttpStatus.UNAUTHORIZED, | ||
msg = "Invalid password", | ||
) | ||
|
||
class AuthenticateException : UserException( | ||
errorCode = 0, | ||
httpStatusCode = HttpStatus.UNAUTHORIZED, | ||
msg = "Authenticate failed", | ||
) |
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
Oops, something went wrong.