Add access denied resolver (#42)
This commit is contained in:
parent
91d4dd66da
commit
153aaf7608
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue