Add access denied resolver (#42)

This commit is contained in:
TonCherAmi 2021-08-09 12:29:11 +03:00 committed by GitHub
parent 91d4dd66da
commit 153aaf7608
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 0 deletions

View File

@ -12,6 +12,7 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-security")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin")

View File

@ -11,6 +11,7 @@ data class ExceptionResolverResult(
) {
companion object {
fun createInternalError(errorMessage: String?): ExceptionResolverResult {
return ExceptionResolverResult(
apiError = DefaultApiError.createFailure(errorMessage),
@ -37,6 +38,15 @@ data class ExceptionResolverResult(
exception = exception
)
}
fun createAccessDeniedError(exception: Exception?): ExceptionResolverResult {
return ExceptionResolverResult(
apiError = DefaultApiError.createFailure(exception?.message),
status = HttpStatus.FORBIDDEN,
exception = exception,
)
}
}
}

View File

@ -0,0 +1,22 @@
package ru.touchin.exception.handler.spring.resolvers
import org.springframework.core.annotation.Order
import org.springframework.security.access.AccessDeniedException
import org.springframework.stereotype.Component
import ru.touchin.common.exceptions.CommonNotFoundException
import ru.touchin.common.spring.Ordered
import ru.touchin.exception.handler.dto.ExceptionResolverResult
@Order(Ordered.LOW)
@Component
class AccessDeniedExceptionResolver : ExceptionResolver {
override fun invoke(exception: Exception): ExceptionResolverResult? {
if (exception is AccessDeniedException) {
return ExceptionResolverResult.createAccessDeniedError(exception)
}
return null
}
}