add CsrfAccessDeniedHandler make 400

This commit is contained in:
2025-07-31 22:41:54 +02:00
parent 61f215c3c7
commit e2b3448f97
2 changed files with 17 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
package ltd.hlaeja.configuration package ltd.hlaeja.configuration
import ltd.hlaeja.security.handler.CsrfAccessDeniedHandler
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
@@ -15,6 +16,7 @@ class SecurityConfiguration {
@Bean @Bean
fun securityWebFilterChain(serverHttpSecurity: ServerHttpSecurity): SecurityWebFilterChain = serverHttpSecurity fun securityWebFilterChain(serverHttpSecurity: ServerHttpSecurity): SecurityWebFilterChain = serverHttpSecurity
.csrf { it.accessDeniedHandler(CsrfAccessDeniedHandler()) }
.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.BAD_REQUEST
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 CsrfAccessDeniedHandler : ServerAccessDeniedHandler {
override fun handle(
exchange: ServerWebExchange,
denied: AccessDeniedException,
): Mono<Void> = Mono.error(ResponseStatusException(BAD_REQUEST, "Access denied ${exchange.request.path}", denied))
}