-
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
1 parent
20ed3ad
commit 4bd1ad6
Showing
11 changed files
with
207 additions
and
4 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
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/com/ohayo/moyamoya/api/user/value/RefreshReq.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.ohayo.moyamoya.api.user.value | ||
|
||
data class RefreshReq( | ||
val refreshToken: String, | ||
) |
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,9 +1,16 @@ | ||
package com.ohayo.moyamoya.core | ||
|
||
import com.ohayo.moyamoya.global.CustomException | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
interface UserRepository: JpaRepository<UserEntity, Int> { | ||
interface UserRepository : JpaRepository<UserEntity, Int> { | ||
fun existsByTel(tel: String): Boolean | ||
} | ||
|
||
fun findByTel(tel: String): UserEntity? | ||
} | ||
|
||
fun UserRepository.findByTelSafety(tel: String) = | ||
findByTel(tel) ?: throw CustomException(HttpStatus.NOT_FOUND, "유저를 찾을 수 없습니다") |
46 changes: 46 additions & 0 deletions
46
src/main/kotlin/com/ohayo/moyamoya/global/LogInterceptor.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,46 @@ | ||
package com.ohayo.moyamoya.global | ||
|
||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import mu.KLogger | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.servlet.HandlerInterceptor | ||
import org.springframework.web.servlet.ModelAndView | ||
|
||
|
||
@Component | ||
class LogInterceptor( | ||
private val logger: KLogger | ||
) : HandlerInterceptor { | ||
@Throws(Exception::class) | ||
override fun preHandle( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
handler: Any | ||
): Boolean { | ||
logger.info("✅ request url - ${request.requestURI}") | ||
logger.info("✅ request method - ${request.method}") | ||
return super.preHandle(request, response, handler) | ||
} | ||
|
||
@Throws(Exception::class) | ||
override fun postHandle( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
handler: Any, | ||
modelAndView: ModelAndView? | ||
) { | ||
logger.info("✅ response status - ${response.status}") | ||
super.postHandle(request, response, handler, modelAndView) | ||
} | ||
|
||
@Throws(Exception::class) | ||
override fun afterCompletion( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
handler: Any, | ||
ex: Exception? | ||
) { | ||
super.afterCompletion(request, response, handler, ex) | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
src/main/kotlin/com/ohayo/moyamoya/global/jwt/JwtAuthenticationFilter.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,40 @@ | ||
package com.ohayo.moyamoya.global.jwt | ||
|
||
import com.ohayo.moyamoya.infra.token.JwtClient | ||
import com.ohayo.moyamoya.infra.token.JwtPayloadKey | ||
import jakarta.servlet.FilterChain | ||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import org.springframework.security.core.userdetails.UserDetailsService | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.filter.OncePerRequestFilter | ||
|
||
@Component | ||
class JwtAuthenticationFilter( | ||
private val jwtUtils: JwtClient, | ||
private val userDetailsService: UserDetailsService | ||
) : OncePerRequestFilter() { | ||
|
||
override fun doFilterInternal( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
filterChain: FilterChain | ||
) { | ||
val token = TokenExtractor.extract(request) | ||
if (token != null) { | ||
jwtUtils.parseToken(token) | ||
setAuthentication(token) | ||
} | ||
|
||
doFilter(request, response, filterChain) | ||
} | ||
|
||
private fun setAuthentication(token: String) { | ||
val tel = jwtUtils.payload(JwtPayloadKey.TEL, token) | ||
val details = userDetailsService.loadUserByUsername(tel) | ||
SecurityContextHolder.getContext().authentication = | ||
UsernamePasswordAuthenticationToken(details, null, details.authorities) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/com/ohayo/moyamoya/global/jwt/JwtExceptionFilter.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,29 @@ | ||
package com.ohayo.moyamoya.global.jwt | ||
import com.ohayo.moyamoya.global.CustomException | ||
import com.ohayo.moyamoya.global.ErrorResponseSender | ||
import jakarta.servlet.FilterChain | ||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.filter.OncePerRequestFilter | ||
|
||
@Component | ||
class JwtExceptionFilter( | ||
private val sender: ErrorResponseSender | ||
) : OncePerRequestFilter() { | ||
|
||
override fun doFilterInternal( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
filterChain: FilterChain | ||
) { | ||
try { | ||
filterChain.doFilter(request, response) | ||
} catch (exception: CustomException) { | ||
sender.send(response, exception) | ||
} catch (exception: Exception) { | ||
sender.send(response, status = HttpStatus.INTERNAL_SERVER_ERROR) | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/com/ohayo/moyamoya/global/jwt/JwtUserDetails.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,17 @@ | ||
package com.ohayo.moyamoya.global.jwt | ||
|
||
import com.ohayo.moyamoya.core.UserEntity | ||
import org.springframework.security.core.GrantedAuthority | ||
import org.springframework.security.core.userdetails.UserDetails | ||
|
||
class JwtUserDetails( | ||
val user: UserEntity | ||
) : UserDetails { | ||
override fun getAuthorities() = listOf(GrantedAuthority { user.userRole.name }) | ||
override fun getPassword() = user.password | ||
override fun getUsername() = user.tel | ||
override fun isAccountNonExpired() = true // 계정이 만료되지 않았는지 | ||
override fun isAccountNonLocked() = true // 계정이 잠기지 않았는지 | ||
override fun isCredentialsNonExpired() = true // 비밀번호가 만료되지 않았는지 | ||
override fun isEnabled() = true | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/ohayo/moyamoya/global/jwt/JwtUserDetailsService.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,13 @@ | ||
package com.ohayo.moyamoya.global.jwt | ||
import com.ohayo.moyamoya.core.UserRepository | ||
import com.ohayo.moyamoya.core.findByTelSafety | ||
import org.springframework.security.core.userdetails.UserDetailsService | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class JwtUserDetailsService( | ||
private val userRepository: UserRepository, | ||
) : UserDetailsService { | ||
override fun loadUserByUsername(username: String) = | ||
JwtUserDetails(userRepository.findByTelSafety(username)) | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/com/ohayo/moyamoya/global/jwt/TokenExtractor.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,18 @@ | ||
package com.ohayo.moyamoya.global.jwt | ||
import com.ohayo.moyamoya.global.CustomException | ||
import jakarta.servlet.http.HttpServletRequest | ||
import org.springframework.http.HttpStatus | ||
|
||
object TokenExtractor { | ||
fun extract(request: HttpServletRequest): String? { | ||
val authorization = request.getHeader("Authorization") ?: return null | ||
return token(authorization) | ||
} | ||
|
||
private fun token(authorization: String): String { | ||
if (!authorization.startsWith("Bearer ")) { | ||
throw CustomException(HttpStatus.UNAUTHORIZED, "token does not start with Bearer") | ||
} | ||
return authorization.removePrefix("Bearer ") | ||
} | ||
} |