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:
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
16
src/main/kotlin/ltd/hlaeja/security/user/GuestUser.kt
Normal file
16
src/main/kotlin/ltd/hlaeja/security/user/GuestUser.kt
Normal 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(),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user