-
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 branch 'dev' of https://github.com/YAPP-Github/25th-Web-Team-2-BE…
… into feat/YS-30
- Loading branch information
Showing
11 changed files
with
199 additions
and
5 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
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/com/dobby/backend/domain/exception/AuthorizationException.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,7 @@ | ||
package com.dobby.backend.domain.exception | ||
|
||
open class AuthorizationException( | ||
errorCode: ErrorCode, | ||
) : DomainException(errorCode) | ||
|
||
class PermissionDeniedException : AuthorizationException(ErrorCode.PERMISSION_DENIED) |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/com/dobby/backend/domain/exception/DefaultException.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,10 @@ | ||
package com.dobby.backend.domain.exception | ||
|
||
open class DefaultException( | ||
errorCode: ErrorCode, | ||
) : DomainException(errorCode) | ||
|
||
class UnknownErrorException : DefaultException(ErrorCode.UNKNOWN_SERVER_ERROR) | ||
class UnauthorizedException : DefaultException(ErrorCode.UNAUTHORIZED) | ||
class InvalidInputException : DefaultException(ErrorCode.INVALID_INPUT) | ||
class UnknownResourceException : DefaultException(ErrorCode.UNKNOWN_RESOURCE) |
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
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/com/dobby/backend/presentation/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,17 @@ | ||
package com.dobby.backend.presentation.api.config | ||
|
||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer | ||
|
||
@Configuration | ||
class WebConfig : WebMvcConfigurer { | ||
override fun addCorsMappings(registry: CorsRegistry) { | ||
registry.addMapping("/**") | ||
.allowedMethods("*") | ||
.allowedOrigins( | ||
"http://localhost:3000", | ||
"http://localhost:3300", | ||
) | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/kotlin/com/dobby/backend/presentation/api/config/WebExceptionHandler.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,56 @@ | ||
package com.dobby.backend.presentation.api.config | ||
|
||
import com.dobby.backend.domain.exception.AuthenticationException | ||
import com.dobby.backend.domain.exception.AuthorizationException | ||
import com.dobby.backend.domain.exception.DomainException | ||
import com.dobby.backend.domain.exception.PermissionDeniedException | ||
import com.dobby.backend.presentation.api.dto.payload.ApiResponse | ||
import jakarta.servlet.http.HttpServletRequest | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.security.access.AccessDeniedException | ||
import org.springframework.web.bind.annotation.ExceptionHandler | ||
import org.springframework.web.bind.annotation.RestControllerAdvice | ||
|
||
@RestControllerAdvice | ||
class WebExceptionHandler { | ||
|
||
@ExceptionHandler(value = [DomainException::class]) | ||
fun handleDomainException( | ||
e: DomainException, | ||
request: HttpServletRequest, | ||
): ResponseEntity<ApiResponse<Unit>> { | ||
return ResponseEntity | ||
.badRequest() | ||
.body(e.toErrorResponse()) | ||
} | ||
|
||
@ExceptionHandler(value = [AuthenticationException::class]) | ||
fun handleAuthenticationException( | ||
exception: AuthenticationException, | ||
request: HttpServletRequest, | ||
): ResponseEntity<ApiResponse<Unit>> { | ||
return ResponseEntity | ||
.status(HttpStatus.UNAUTHORIZED) | ||
.body(exception.toErrorResponse()) | ||
} | ||
|
||
@ExceptionHandler(value = [AuthorizationException::class]) | ||
fun handleAuthorizationException( | ||
exception: AuthorizationException, | ||
): ResponseEntity<ApiResponse<Unit>> { | ||
return ResponseEntity | ||
.status(HttpStatus.FORBIDDEN) | ||
.body(exception.toErrorResponse()) | ||
} | ||
|
||
@ExceptionHandler(value = [AccessDeniedException::class]) | ||
fun handleAccessDeniedException() = handleAuthorizationException(PermissionDeniedException()) | ||
|
||
private fun DomainException.toErrorResponse(): ApiResponse<Unit> { | ||
return ApiResponse.onFailure( | ||
code = this.code, | ||
message = this.errorMessage | ||
) | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/kotlin/com/dobby/backend/presentation/api/config/WebSecurityConfig.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,63 @@ | ||
package com.dobby.backend.presentation.api.config | ||
|
||
import com.dobby.backend.domain.exception.PermissionDeniedException | ||
import com.dobby.backend.domain.exception.UnauthorizedException | ||
import com.dobby.backend.infrastructure.token.JwtTokenProvider | ||
import com.dobby.backend.presentation.api.config.filter.JwtAuthenticationFilter | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.core.annotation.Order | ||
import org.springframework.security.config.Customizer | ||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity | ||
import org.springframework.security.config.http.SessionCreationPolicy | ||
import org.springframework.security.web.SecurityFilterChain | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter | ||
import org.springframework.web.servlet.HandlerExceptionResolver | ||
|
||
@Configuration | ||
@EnableMethodSecurity | ||
class WebSecurityConfig { | ||
@Bean | ||
@Order(0) | ||
fun securityFilterChain( | ||
httpSecurity: HttpSecurity, | ||
jwtTokenProvider: JwtTokenProvider, | ||
handlerExceptionResolver: HandlerExceptionResolver, | ||
): SecurityFilterChain = httpSecurity | ||
.securityMatcher( "/v1/members/**") | ||
.csrf { it.disable() } | ||
.cors(Customizer.withDefaults()) | ||
.sessionManagement { | ||
it.sessionCreationPolicy(SessionCreationPolicy.STATELESS) | ||
} | ||
.authorizeHttpRequests { | ||
it.anyRequest().authenticated() | ||
} | ||
.addFilterBefore( | ||
JwtAuthenticationFilter(jwtTokenProvider, handlerExceptionResolver), | ||
UsernamePasswordAuthenticationFilter::class.java | ||
) | ||
.exceptionHandling { | ||
it.accessDeniedHandler { request, response, exception -> | ||
handlerExceptionResolver.resolveException(request, response, null, PermissionDeniedException()) | ||
}.authenticationEntryPoint { request, response, authException -> | ||
handlerExceptionResolver.resolveException(request, response, null, UnauthorizedException()) | ||
} | ||
} | ||
.build() | ||
|
||
@Bean | ||
@Order(1) | ||
fun authSecurityFilterChain(httpSecurity: HttpSecurity): SecurityFilterChain = httpSecurity | ||
.securityMatcher("/v1/auth/**") | ||
.csrf { it.disable() } | ||
.cors(Customizer.withDefaults()) | ||
.sessionManagement { | ||
it.sessionCreationPolicy(SessionCreationPolicy.STATELESS) | ||
} | ||
.authorizeHttpRequests { | ||
it.anyRequest().permitAll() | ||
} | ||
.build() | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/com/dobby/backend/presentation/api/config/filter/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,36 @@ | ||
package com.dobby.backend.presentation.api.config.filter | ||
|
||
import com.dobby.backend.domain.exception.AuthenticationTokenNotFoundException | ||
import com.dobby.backend.domain.exception.AuthenticationTokenNotValidException | ||
import com.dobby.backend.infrastructure.token.JwtTokenProvider | ||
import jakarta.servlet.FilterChain | ||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import org.springframework.web.filter.OncePerRequestFilter | ||
import org.springframework.web.servlet.HandlerExceptionResolver | ||
|
||
class JwtAuthenticationFilter( | ||
private val jwtTokenProvider: JwtTokenProvider, | ||
private val handlerExceptionResolver: HandlerExceptionResolver, | ||
) : OncePerRequestFilter() { | ||
override fun doFilterInternal( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
filterChain: FilterChain, | ||
) { | ||
try { | ||
val authenticationHeader = | ||
request.getHeader("Authorization") ?: throw AuthenticationTokenNotFoundException() | ||
val accessToken = if (authenticationHeader.startsWith("Bearer ")) | ||
authenticationHeader.substring(7) | ||
else throw AuthenticationTokenNotValidException() | ||
|
||
val authentication = jwtTokenProvider.parseAuthentication(accessToken) | ||
SecurityContextHolder.getContext().authentication = authentication | ||
return filterChain.doFilter(request, response) | ||
} catch (e: Exception) { | ||
handlerExceptionResolver.resolveException(request, response, null, e) | ||
} | ||
} | ||
} |
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