add UserAccessDeniedHandler make 404 on 401

This commit is contained in:
2025-07-31 22:46:45 +02:00
parent e2b3448f97
commit 9321a7eadd
2 changed files with 17 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package ltd.hlaeja.configuration package ltd.hlaeja.configuration
import ltd.hlaeja.security.handler.CsrfAccessDeniedHandler import ltd.hlaeja.security.handler.CsrfAccessDeniedHandler
import ltd.hlaeja.security.handler.UserAccessDeniedHandler
import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpStatus.FOUND import org.springframework.http.HttpStatus.FOUND
@@ -17,6 +18,7 @@ class SecurityConfiguration {
@Bean @Bean
fun securityWebFilterChain(serverHttpSecurity: ServerHttpSecurity): SecurityWebFilterChain = serverHttpSecurity fun securityWebFilterChain(serverHttpSecurity: ServerHttpSecurity): SecurityWebFilterChain = serverHttpSecurity
.csrf { it.accessDeniedHandler(CsrfAccessDeniedHandler()) } .csrf { it.accessDeniedHandler(CsrfAccessDeniedHandler()) }
.exceptionHandling { it.accessDeniedHandler(UserAccessDeniedHandler()) }
.authorizeExchange(::authorizeExchange) .authorizeExchange(::authorizeExchange)
.formLogin(::formLogin) .formLogin(::formLogin)
.logout(::logout) .logout(::logout)

View File

@@ -0,0 +1,15 @@
package ltd.hlaeja.security.handler
import org.springframework.http.HttpStatus.NOT_FOUND
import org.springframework.security.access.AccessDeniedException
import org.springframework.security.web.server.authorization.ServerAccessDeniedHandler
import org.springframework.web.server.ResponseStatusException
import org.springframework.web.server.ServerWebExchange
import reactor.core.publisher.Mono
class UserAccessDeniedHandler : ServerAccessDeniedHandler {
override fun handle(
exchange: ServerWebExchange,
denied: AccessDeniedException,
): Mono<Void> = Mono.error(ResponseStatusException(NOT_FOUND, "Access denied ${exchange.request.path}", denied))
}