package space.luminic.budgerapp.controllers import com.fasterxml.jackson.databind.ObjectMapper import org.springframework.http.HttpStatus import org.springframework.http.MediaType import org.springframework.security.core.AuthenticationException import org.springframework.security.web.server.ServerAuthenticationEntryPoint import org.springframework.stereotype.Component import org.springframework.web.server.ServerWebExchange import reactor.core.publisher.Mono import java.util.* @Component class CustomAuthenticationEntryPoint : ServerAuthenticationEntryPoint { override fun commence( exchange: ServerWebExchange, ex: AuthenticationException ): Mono { val response = exchange.response response.statusCode = HttpStatus.UNAUTHORIZED response.headers.contentType = MediaType.APPLICATION_JSON val body = mapOf( "timestamp" to Date(), "status" to HttpStatus.UNAUTHORIZED.value(), "error" to "Unauthorized", "message" to ex.message, "path" to exchange.request.path.value() ) val buffer = response.bufferFactory().wrap(ObjectMapper().writeValueAsBytes(body)) return response.writeWith(Mono.just(buffer)) } }