From b5edcc0128e347bcb10c155d8e353dbbf0db06ed Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:13:25 +0900 Subject: [PATCH 01/17] =?UTF-8?q?:memo:=20UserNotFoundException=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../miso/domain/user/exception/UserNotFoundException.kt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/kotlin/andreas311/miso/domain/user/exception/UserNotFoundException.kt diff --git a/src/main/kotlin/andreas311/miso/domain/user/exception/UserNotFoundException.kt b/src/main/kotlin/andreas311/miso/domain/user/exception/UserNotFoundException.kt new file mode 100644 index 00000000..e74518be --- /dev/null +++ b/src/main/kotlin/andreas311/miso/domain/user/exception/UserNotFoundException.kt @@ -0,0 +1,7 @@ +package andreas311.miso.domain.user.exception + +import andreas311.miso.global.error.exception.ErrorCode +import andreas311.miso.global.error.exception.MisoException + +class UserNotFoundException : MisoException(ErrorCode.USER_NOT_FOUND) { +} \ No newline at end of file From 700c2e29ab211e7f8e999d4a23fcc875fb8ad481 Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:13:32 +0900 Subject: [PATCH 02/17] =?UTF-8?q?:memo:=20TokenTimeProperties=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../security/jwt/properties/TokenTimeProperties.kt | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/main/kotlin/andreas311/miso/global/security/jwt/properties/TokenTimeProperties.kt diff --git a/src/main/kotlin/andreas311/miso/global/security/jwt/properties/TokenTimeProperties.kt b/src/main/kotlin/andreas311/miso/global/security/jwt/properties/TokenTimeProperties.kt new file mode 100644 index 00000000..1a347ab0 --- /dev/null +++ b/src/main/kotlin/andreas311/miso/global/security/jwt/properties/TokenTimeProperties.kt @@ -0,0 +1,11 @@ +package andreas311.miso.global.security.jwt.properties + +import org.springframework.boot.context.properties.ConfigurationProperties +import org.springframework.boot.context.properties.ConstructorBinding + +@ConstructorBinding +@ConfigurationProperties(prefix = "jwt.time") +class TokenTimeProperties ( + val accessTime: Long, + val refreshTime: Long +) \ No newline at end of file From 34113bd06bb62302236a2982ceb0ae87203fb487 Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:13:44 +0900 Subject: [PATCH 03/17] =?UTF-8?q?:memo:=20TokenProvider=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../miso/global/security/jwt/TokenProvider.kt | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/main/kotlin/andreas311/miso/global/security/jwt/TokenProvider.kt diff --git a/src/main/kotlin/andreas311/miso/global/security/jwt/TokenProvider.kt b/src/main/kotlin/andreas311/miso/global/security/jwt/TokenProvider.kt new file mode 100644 index 00000000..203dcf6c --- /dev/null +++ b/src/main/kotlin/andreas311/miso/global/security/jwt/TokenProvider.kt @@ -0,0 +1,111 @@ +package andreas311.miso.global.security.jwt + +import andreas311.miso.domain.auth.exception.RoleNotExistException +import andreas311.miso.domain.user.enums.Role +import andreas311.miso.global.security.auth.AuthDetailService +import andreas311.miso.global.security.exception.TokenExpiredException +import andreas311.miso.global.security.exception.TokenInvalidException +import andreas311.miso.global.security.jwt.properties.JwtProperties +import andreas311.miso.global.security.jwt.properties.TokenTimeProperties +import io.jsonwebtoken.Claims +import io.jsonwebtoken.ExpiredJwtException +import io.jsonwebtoken.Jwts +import io.jsonwebtoken.SignatureAlgorithm +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken +import org.springframework.security.core.Authentication +import org.springframework.stereotype.Component +import java.security.Key +import java.time.ZonedDateTime +import java.util.* +import javax.servlet.http.HttpServletRequest + +@Component +class TokenProvider( + private val jwtProperties: JwtProperties, + private val tokenTimeProperties: TokenTimeProperties, + private val authDetailService: AuthDetailService, +) { + companion object { + const val ACCESS_TYPE = "access" + const val REFRESH_TYPE = "refresh" + const val TOKEN_PREFIX = "Bearer " + const val AUTHORITY = "authority" + } + + val accessExpiredTime: ZonedDateTime + get() = ZonedDateTime.now().plusSeconds(tokenTimeProperties.accessTime) + + val refreshExpiredTime: ZonedDateTime + get() = ZonedDateTime.now().plusSeconds(tokenTimeProperties.refreshTime) + + fun generateAccessToken(email: String, role: Role): String = + generateToken(email, ACCESS_TYPE, jwtProperties.accessSecret, tokenTimeProperties.accessTime, role) + + fun generateRefreshToken(email: String, role: Role): String = + generateToken(email, REFRESH_TYPE, jwtProperties.refreshSecret, tokenTimeProperties.refreshTime, role) + + fun resolveToken(req: HttpServletRequest): String? { + val token = req.getHeader("Authorization") ?: return null + return parseToken(token) + } + + fun exactEmailFromRefreshToken(refresh: String): String { + return getTokenSubject(refresh, jwtProperties.refreshSecret) + } + + fun exactRoleFromRefreshToken(refresh: String): Role { + val authority = getTokenBody(refresh, jwtProperties.refreshSecret) + .get(AUTHORITY, String::class.java) + + return when (authority) { + "ROLE_USER" -> Role.ROLE_USER + "ROLE_ADMIN" -> Role.ROLE_ADMIN + else -> throw RoleNotExistException() + } + + } + + fun exactTypeFromRefreshToken(refresh: String): String = + getTokenSubject(refresh, jwtProperties.refreshSecret) + + fun authentication(token: String): Authentication { + val userDetails = authDetailService.loadUserByUsername(getTokenSubject(token, jwtProperties.accessSecret)) + return UsernamePasswordAuthenticationToken(userDetails, "", userDetails.authorities) + } + + fun parseToken(token: String): String? = + if (token.startsWith(TOKEN_PREFIX)) + token.replace(TOKEN_PREFIX, "") + else + null + + fun generateToken(email: String, type: String, secret: Key, exp: Long, role: Role): String { + val claims = Jwts.claims().setSubject(email) + claims["type"] = type + claims[AUTHORITY] = role + return Jwts.builder() + .setHeaderParam("typ", "JWT") + .signWith(secret, SignatureAlgorithm.HS256) + .setClaims(claims) + .setIssuedAt(Date()) + .setExpiration(Date(System.currentTimeMillis() + exp * 1000)) + .compact() + } + + private fun getTokenBody(token: String, secret: Key): Claims { + return try { + Jwts.parserBuilder() + .setSigningKey(secret) + .build() + .parseClaimsJws(token) + .body + } catch (e: ExpiredJwtException) { + throw TokenExpiredException() + } catch (e: Exception) { + throw TokenInvalidException() + } + } + + private fun getTokenSubject(token: String, secret: Key): String = + getTokenBody(token, secret).subject +} \ No newline at end of file From 422813eae4d6aaa943043ccaef2a387f4c21c4ca Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:13:50 +0900 Subject: [PATCH 04/17] =?UTF-8?q?:memo:=20TokenInvalidException=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/security/exception/TokenInvalidException.kt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/kotlin/andreas311/miso/global/security/exception/TokenInvalidException.kt diff --git a/src/main/kotlin/andreas311/miso/global/security/exception/TokenInvalidException.kt b/src/main/kotlin/andreas311/miso/global/security/exception/TokenInvalidException.kt new file mode 100644 index 00000000..39da6f1a --- /dev/null +++ b/src/main/kotlin/andreas311/miso/global/security/exception/TokenInvalidException.kt @@ -0,0 +1,7 @@ +package andreas311.miso.global.security.exception + +import andreas311.miso.global.error.exception.ErrorCode +import andreas311.miso.global.error.exception.MisoException + +class TokenInvalidException : MisoException(ErrorCode.TOKEN_NOT_VALID) { +} \ No newline at end of file From 9072aff08f0c0aa5e007f2aab20035e7116904ea Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:13:59 +0900 Subject: [PATCH 05/17] =?UTF-8?q?:memo:=20TokenExpiredException=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/security/exception/TokenExpiredException.kt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/kotlin/andreas311/miso/global/security/exception/TokenExpiredException.kt diff --git a/src/main/kotlin/andreas311/miso/global/security/exception/TokenExpiredException.kt b/src/main/kotlin/andreas311/miso/global/security/exception/TokenExpiredException.kt new file mode 100644 index 00000000..bf7bd74e --- /dev/null +++ b/src/main/kotlin/andreas311/miso/global/security/exception/TokenExpiredException.kt @@ -0,0 +1,7 @@ +package andreas311.miso.global.security.exception + +import andreas311.miso.global.error.exception.ErrorCode +import andreas311.miso.global.error.exception.MisoException + +class TokenExpiredException : MisoException(ErrorCode.TOKEN_IS_EXPIRED) { +} \ No newline at end of file From ed9928a60bb20a6df61a4de9fdd0f78b376c73ea Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:14:07 +0900 Subject: [PATCH 06/17] =?UTF-8?q?:memo:=20SecurityConfig=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../miso/global/security/SecurityConfig.kt | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/main/kotlin/andreas311/miso/global/security/SecurityConfig.kt diff --git a/src/main/kotlin/andreas311/miso/global/security/SecurityConfig.kt b/src/main/kotlin/andreas311/miso/global/security/SecurityConfig.kt new file mode 100644 index 00000000..67bd9787 --- /dev/null +++ b/src/main/kotlin/andreas311/miso/global/security/SecurityConfig.kt @@ -0,0 +1,50 @@ +package andreas311.miso.global.security + +import andreas311.miso.global.security.filter.JwtExceptionFilter +import andreas311.miso.global.security.filter.JwtRequestFilter +import andreas311.miso.global.security.handler.CustomAccessDeniedHandler +import andreas311.miso.global.security.handler.CustomAuthenticationEntryPointHandler +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.security.config.annotation.web.builders.HttpSecurity +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity +import org.springframework.security.config.http.SessionCreationPolicy +import org.springframework.security.web.SecurityFilterChain +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter +import org.springframework.security.web.util.matcher.RequestMatcher +import org.springframework.web.cors.CorsUtils + +@Configuration +@EnableWebSecurity +class SecurityConfig( + private val jwtExceptionFilter: JwtExceptionFilter, + private val jwtRequestFilter: JwtRequestFilter +) { + + @Bean + fun filterChain(http: HttpSecurity) : SecurityFilterChain { + return http + .cors().and() + .csrf().disable() + .formLogin().disable() + .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) + .and() + + .authorizeRequests() + .requestMatchers(RequestMatcher { request -> + CorsUtils.isPreFlightRequest(request) + }).permitAll() + + .anyRequest().denyAll() + .and() + .exceptionHandling() + .accessDeniedHandler(CustomAccessDeniedHandler()) + .authenticationEntryPoint(CustomAuthenticationEntryPointHandler()) + + .and() + .addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter::class.java) + .addFilterBefore(jwtExceptionFilter, JwtRequestFilter::class.java) + + .build() + } +} \ No newline at end of file From ddb1e36e6eaba2493ee4f85fa389b93e21be3ef4 Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:14:12 +0900 Subject: [PATCH 07/17] =?UTF-8?q?:memo:=20RoleNotExistException=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../miso/domain/auth/exception/RoleNotExistException.kt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/kotlin/andreas311/miso/domain/auth/exception/RoleNotExistException.kt diff --git a/src/main/kotlin/andreas311/miso/domain/auth/exception/RoleNotExistException.kt b/src/main/kotlin/andreas311/miso/domain/auth/exception/RoleNotExistException.kt new file mode 100644 index 00000000..cb2b68c0 --- /dev/null +++ b/src/main/kotlin/andreas311/miso/domain/auth/exception/RoleNotExistException.kt @@ -0,0 +1,7 @@ +package andreas311.miso.domain.auth.exception + +import andreas311.miso.global.error.exception.ErrorCode +import andreas311.miso.global.error.exception.MisoException + +class RoleNotExistException : MisoException(ErrorCode.ROLE_NOT_EXIST) { +} \ No newline at end of file From 9b86193f72a4d643814c091509ee6874beeddcdd Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:14:21 +0900 Subject: [PATCH 08/17] =?UTF-8?q?:memo:=20JwtRequestFilter=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../security/filter/JwtRequestFilter.kt | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/main/kotlin/andreas311/miso/global/security/filter/JwtRequestFilter.kt diff --git a/src/main/kotlin/andreas311/miso/global/security/filter/JwtRequestFilter.kt b/src/main/kotlin/andreas311/miso/global/security/filter/JwtRequestFilter.kt new file mode 100644 index 00000000..66014dc0 --- /dev/null +++ b/src/main/kotlin/andreas311/miso/global/security/filter/JwtRequestFilter.kt @@ -0,0 +1,34 @@ +package andreas311.miso.global.security.filter + +import andreas311.miso.global.security.jwt.TokenProvider +import org.slf4j.LoggerFactory +import org.springframework.security.core.context.SecurityContextHolder +import org.springframework.stereotype.Component +import org.springframework.web.filter.OncePerRequestFilter +import javax.servlet.FilterChain +import javax.servlet.http.HttpServletRequest +import javax.servlet.http.HttpServletResponse + +@Component +class JwtRequestFilter( + private val tokenProvider: TokenProvider +) : OncePerRequestFilter() { + + private val log = LoggerFactory.getLogger(this::class.simpleName) + + override fun doFilterInternal(request: HttpServletRequest, response: HttpServletResponse, filterChain: FilterChain) { + + val accessToken = tokenProvider.resolveToken(request) + + if(!accessToken.isNullOrBlank()) { + + val authentication = tokenProvider.authentication(accessToken) + + SecurityContextHolder.getContext().authentication = authentication + + log.info("current user email = ${authentication.name}") + } + + filterChain.doFilter(request, response) + } +} \ No newline at end of file From 1f2f5d0ab5fbf32e2a130a206e812536067df496 Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:14:28 +0900 Subject: [PATCH 09/17] =?UTF-8?q?:memo:=20JwtProperties=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../security/jwt/properties/JwtProperties.kt | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/kotlin/andreas311/miso/global/security/jwt/properties/JwtProperties.kt diff --git a/src/main/kotlin/andreas311/miso/global/security/jwt/properties/JwtProperties.kt b/src/main/kotlin/andreas311/miso/global/security/jwt/properties/JwtProperties.kt new file mode 100644 index 00000000..a52b07f8 --- /dev/null +++ b/src/main/kotlin/andreas311/miso/global/security/jwt/properties/JwtProperties.kt @@ -0,0 +1,22 @@ +package andreas311.miso.global.security.jwt.properties + +import io.jsonwebtoken.security.Keys +import org.springframework.boot.context.properties.ConfigurationProperties +import org.springframework.boot.context.properties.ConstructorBinding +import java.security.Key + +@ConstructorBinding +@ConfigurationProperties(prefix = "jwt") +class JwtProperties( + accessSecret: String, + refreshSecret: String +) { + + val accessSecret: Key + val refreshSecret: Key + + init { + this.accessSecret = Keys.hmacShaKeyFor(accessSecret.toByteArray()) + this.refreshSecret = Keys.hmacShaKeyFor(refreshSecret.toByteArray()) + } +} \ No newline at end of file From a6ea4063ac590b5df6768abc3f8e7be1b9dd32a6 Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:14:37 +0900 Subject: [PATCH 10/17] =?UTF-8?q?:memo:=20CustomAuthenticationEntryPointHa?= =?UTF-8?q?ndler=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomAuthenticationEntryPointHandler.kt | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/kotlin/andreas311/miso/global/security/handler/CustomAuthenticationEntryPointHandler.kt diff --git a/src/main/kotlin/andreas311/miso/global/security/handler/CustomAuthenticationEntryPointHandler.kt b/src/main/kotlin/andreas311/miso/global/security/handler/CustomAuthenticationEntryPointHandler.kt new file mode 100644 index 00000000..cfa7314a --- /dev/null +++ b/src/main/kotlin/andreas311/miso/global/security/handler/CustomAuthenticationEntryPointHandler.kt @@ -0,0 +1,23 @@ +package andreas311.miso.global.security.handler + +import org.slf4j.LoggerFactory +import org.springframework.security.core.AuthenticationException +import org.springframework.security.web.AuthenticationEntryPoint +import org.springframework.stereotype.Component +import java.io.IOException +import javax.servlet.ServletException +import javax.servlet.http.HttpServletRequest +import javax.servlet.http.HttpServletResponse + +@Component +class CustomAuthenticationEntryPointHandler : AuthenticationEntryPoint { + private val log = LoggerFactory.getLogger(this::class.simpleName) + + @Throws(IOException::class, ServletException::class) + override fun commence(request: HttpServletRequest, response: HttpServletResponse, authException: AuthenticationException) { + + log.info("=== AuthenticationEntryPoint ===") + + response.sendError(HttpServletResponse.SC_UNAUTHORIZED) + } +} \ No newline at end of file From 926becc2905c400360e007d155771bcff11d855f Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:14:44 +0900 Subject: [PATCH 11/17] =?UTF-8?q?:memo:=20CustomAccessDeniedHandler=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/CustomAccessDeniedHandler.kt | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/kotlin/andreas311/miso/global/security/handler/CustomAccessDeniedHandler.kt diff --git a/src/main/kotlin/andreas311/miso/global/security/handler/CustomAccessDeniedHandler.kt b/src/main/kotlin/andreas311/miso/global/security/handler/CustomAccessDeniedHandler.kt new file mode 100644 index 00000000..b0ba1c03 --- /dev/null +++ b/src/main/kotlin/andreas311/miso/global/security/handler/CustomAccessDeniedHandler.kt @@ -0,0 +1,23 @@ +package andreas311.miso.global.security.handler + +import org.slf4j.LoggerFactory +import org.springframework.security.access.AccessDeniedException +import org.springframework.security.web.access.AccessDeniedHandler +import org.springframework.stereotype.Component +import java.io.IOException +import javax.servlet.ServletException +import javax.servlet.http.HttpServletRequest +import javax.servlet.http.HttpServletResponse + +@Component +class CustomAccessDeniedHandler : AccessDeniedHandler { + private val log = LoggerFactory.getLogger(this::class.simpleName) + + @Throws(IOException::class, ServletException::class) + override fun handle(request: HttpServletRequest, response: HttpServletResponse, accessDeniedException: AccessDeniedException) { + + log.info("=== Access Denied ===") + + response.sendError(HttpServletResponse.SC_FORBIDDEN) + } +} \ No newline at end of file From 2367e807dddc636759f7514ef3359be852000f5e Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:14:50 +0900 Subject: [PATCH 12/17] =?UTF-8?q?:memo:=20AuthDetailService=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/security/auth/AuthDetailService.kt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/kotlin/andreas311/miso/global/security/auth/AuthDetailService.kt diff --git a/src/main/kotlin/andreas311/miso/global/security/auth/AuthDetailService.kt b/src/main/kotlin/andreas311/miso/global/security/auth/AuthDetailService.kt new file mode 100644 index 00000000..e9c2f773 --- /dev/null +++ b/src/main/kotlin/andreas311/miso/global/security/auth/AuthDetailService.kt @@ -0,0 +1,19 @@ +package andreas311.miso.global.security.auth + +import andreas311.miso.domain.user.exception.UserNotFoundException +import andreas311.miso.domain.user.repository.UserRepository +import org.springframework.security.core.userdetails.UserDetails +import org.springframework.security.core.userdetails.UserDetailsService +import org.springframework.stereotype.Service + +@Service +class AuthDetailService( + private val userRepository: UserRepository +) : UserDetailsService { + override fun loadUserByUsername(username: String): UserDetails { + + val user = userRepository.findByEmail(username) ?: throw UserNotFoundException() + + return AuthDetails(user) + } +} \ No newline at end of file From c133f3a412d8da457b70528c5eab1985174c05f6 Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:14:59 +0900 Subject: [PATCH 13/17] =?UTF-8?q?:memo:=20AuthDetails=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../miso/global/security/auth/AuthDetails.kt | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/kotlin/andreas311/miso/global/security/auth/AuthDetails.kt diff --git a/src/main/kotlin/andreas311/miso/global/security/auth/AuthDetails.kt b/src/main/kotlin/andreas311/miso/global/security/auth/AuthDetails.kt new file mode 100644 index 00000000..811c6d77 --- /dev/null +++ b/src/main/kotlin/andreas311/miso/global/security/auth/AuthDetails.kt @@ -0,0 +1,23 @@ +package andreas311.miso.global.security.auth + +import andreas311.miso.domain.user.entity.User +import org.springframework.security.core.GrantedAuthority +import org.springframework.security.core.userdetails.UserDetails + +class AuthDetails( + private val user: User +) : UserDetails { + override fun getAuthorities(): MutableCollection? = user.role + + override fun getPassword(): String? = null + + override fun getUsername(): String = user.email + + override fun isAccountNonExpired(): Boolean = true + + override fun isAccountNonLocked(): Boolean = true + + override fun isCredentialsNonExpired(): Boolean = true + + override fun isEnabled(): Boolean = true +} \ No newline at end of file From f728517d6d4ca6ed2f88643d39072f85c17b27ff Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:15:29 +0900 Subject: [PATCH 14/17] =?UTF-8?q?:memo:=20ErrorCode=20=EC=97=90=20User=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EC=98=88=EC=99=B8=20enum=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../andreas311/miso/global/error/exception/ErrorCode.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/kotlin/andreas311/miso/global/error/exception/ErrorCode.kt b/src/main/kotlin/andreas311/miso/global/error/exception/ErrorCode.kt index fc594baa..7c8309d7 100644 --- a/src/main/kotlin/andreas311/miso/global/error/exception/ErrorCode.kt +++ b/src/main/kotlin/andreas311/miso/global/error/exception/ErrorCode.kt @@ -8,6 +8,10 @@ enum class ErrorCode( // SERVER ERROR UNKNOWN_ERROR(500, "알 수 없는 에러입니다."), + // USER + USER_NOT_FOUND(404, "사용자를 찾을 수 없습니다."), + ROLE_NOT_EXIST(404, "역할이 존재하지 않습니다"), + // TOKEN TOKEN_IS_EXPIRED(401, "토큰이 만료 되었습니다."), TOKEN_NOT_VALID(401, "토큰이 유효 하지 않습니다."), From 3012b83e8d9592e9aed628ccdef30df803c74a22 Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:15:50 +0900 Subject: [PATCH 15/17] =?UTF-8?q?:memo:=20ExceptionFilter=20=EC=97=90?= =?UTF-8?q?=EC=84=9C=20JwtExceptionFilter=20=EC=9C=BC=EB=A1=9C=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../filter/{ExceptionFilter.kt => JwtExceptionFilter.kt} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/kotlin/andreas311/miso/global/security/filter/{ExceptionFilter.kt => JwtExceptionFilter.kt} (98%) diff --git a/src/main/kotlin/andreas311/miso/global/security/filter/ExceptionFilter.kt b/src/main/kotlin/andreas311/miso/global/security/filter/JwtExceptionFilter.kt similarity index 98% rename from src/main/kotlin/andreas311/miso/global/security/filter/ExceptionFilter.kt rename to src/main/kotlin/andreas311/miso/global/security/filter/JwtExceptionFilter.kt index fb8b3632..5f2de7ce 100644 --- a/src/main/kotlin/andreas311/miso/global/security/filter/ExceptionFilter.kt +++ b/src/main/kotlin/andreas311/miso/global/security/filter/JwtExceptionFilter.kt @@ -16,7 +16,7 @@ import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse @Component -class ExceptionFilter( +class JwtExceptionFilter( private val objectMapper: ObjectMapper ) : OncePerRequestFilter() { From 817fb1ee009220ce3ed64aecdd5e4084699d6a3b Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:16:11 +0900 Subject: [PATCH 16/17] =?UTF-8?q?:memo:=20MisoApplication=20=EC=97=90=20Co?= =?UTF-8?q?nfigurationPropertiesScan=20=EC=96=B4=EB=85=B8=ED=85=8C?= =?UTF-8?q?=EC=9D=B4=EC=85=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/andreas311/miso/MisoApplication.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/kotlin/andreas311/miso/MisoApplication.kt b/src/main/kotlin/andreas311/miso/MisoApplication.kt index c9f0bb0d..8fad2e12 100644 --- a/src/main/kotlin/andreas311/miso/MisoApplication.kt +++ b/src/main/kotlin/andreas311/miso/MisoApplication.kt @@ -1,9 +1,11 @@ package andreas311.miso import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.context.properties.ConfigurationPropertiesScan import org.springframework.boot.runApplication @SpringBootApplication +@ConfigurationPropertiesScan class MisoApplication fun main(args: Array) { From f1ac6481b9a1124d062ad12e847cba7dd358d4de Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Tue, 10 Oct 2023 11:19:19 +0900 Subject: [PATCH 17/17] =?UTF-8?q?:memo:=20UserRepository=20=EC=97=90=20fin?= =?UTF-8?q?dByEmail=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../andreas311/miso/domain/user/repository/UserRepository.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/kotlin/andreas311/miso/domain/user/repository/UserRepository.kt b/src/main/kotlin/andreas311/miso/domain/user/repository/UserRepository.kt index dd9934f9..0f4e448f 100644 --- a/src/main/kotlin/andreas311/miso/domain/user/repository/UserRepository.kt +++ b/src/main/kotlin/andreas311/miso/domain/user/repository/UserRepository.kt @@ -5,4 +5,6 @@ import org.springframework.data.repository.CrudRepository import java.util.UUID interface UserRepository : CrudRepository { + + fun findByEmail(email: String): User? } \ No newline at end of file