add default error

- add error setting in application.yml
- update tailwind.css
- add error.html
- add information.html
- update UserAttribute with GuestUser
- add ErrorAttributes
- add GuestUser
This commit is contained in:
2025-07-31 09:20:23 +02:00
parent 07af820970
commit a9cdf3592d
7 changed files with 469 additions and 14 deletions

View File

@@ -0,0 +1,29 @@
package ltd.hlaeja.controller.advice
import kotlinx.coroutines.runBlocking
import ltd.hlaeja.security.RemoteAuthentication
import ltd.hlaeja.security.user.GuestUser
import org.springframework.boot.web.error.ErrorAttributeOptions
import org.springframework.boot.web.reactive.error.DefaultErrorAttributes
import org.springframework.security.core.context.SecurityContextImpl
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.function.server.awaitSession
@Component
class ErrorAttributes : DefaultErrorAttributes(), GuestUser {
override fun getErrorAttributes(
serverRequest: ServerRequest,
errorAttributeOptions: ErrorAttributeOptions,
): MutableMap<String, Any> =
super.getErrorAttributes(serverRequest, errorAttributeOptions)
.also { attribute ->
attribute["remoteUser"] = runBlocking {
serverRequest.awaitSession()
.attributes["SPRING_SECURITY_CONTEXT"]
?.let { context -> (context as SecurityContextImpl).authentication }
?: guestUser()
} as RemoteAuthentication
}
}

View File

@@ -1,29 +1,24 @@
package ltd.hlaeja.controller.advice
import java.util.UUID
import kotlinx.coroutines.reactive.awaitFirstOrNull
import ltd.hlaeja.security.RemoteAuthentication
import ltd.hlaeja.security.RemoteUserDetail
import ltd.hlaeja.security.user.GuestUser
import org.springframework.security.core.context.ReactiveSecurityContextHolder
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ModelAttribute
@ControllerAdvice
class UserAttribute {
class UserAttribute : GuestUser {
@ModelAttribute
suspend fun remoteUser(model: Model) {
val remoteAuthentication: RemoteAuthentication = ReactiveSecurityContextHolder.getContext()
.awaitFirstOrNull()
?.let { it.authentication as RemoteAuthentication }
?: RemoteAuthentication(
RemoteUserDetail(
UUID.fromString("00000000-0000-0000-0000-000000000000"),
"n/a",
),
mutableListOf(),
)
model.addAttribute("remoteUser", remoteAuthentication)
model.addAttribute(
"remoteUser",
ReactiveSecurityContextHolder.getContext()
.awaitFirstOrNull()
?.let { it.authentication as RemoteAuthentication }
?: guestUser(),
)
}
}

View File

@@ -0,0 +1,16 @@
package ltd.hlaeja.security.user
import java.util.UUID
import ltd.hlaeja.security.RemoteAuthentication
import ltd.hlaeja.security.RemoteUserDetail
interface GuestUser {
fun guestUser(): RemoteAuthentication = RemoteAuthentication(
RemoteUserDetail(
UUID.fromString("00000000-0000-0000-0000-000000000000"),
"n/a",
),
mutableListOf(),
)
}