From 32122268538a501dd54ca4c72283cc50cec632d4 Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Mon, 20 Jan 2025 21:43:36 +0100 Subject: [PATCH] Update so there is an open landing page and a login page - make / public - add login on index.html - add UserAttribute - add hasRole to RemoteAuthentication - update HomeController to give welcome instead of index if login. - add welcome.html --- .../configuration/SecurityConfiguration.kt | 1 + .../ltd/hlaeja/controller/HomeController.kt | 15 ++++++++-- .../hlaeja/controller/advice/UserAttribute.kt | 29 +++++++++++++++++++ .../hlaeja/security/RemoteAuthentication.kt | 9 ++++++ src/main/resources/templates/home/index.html | 1 + .../resources/templates/home/welcome.html | 23 +++++++++++++++ 6 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/ltd/hlaeja/controller/advice/UserAttribute.kt create mode 100644 src/main/resources/templates/home/welcome.html diff --git a/src/main/kotlin/ltd/hlaeja/configuration/SecurityConfiguration.kt b/src/main/kotlin/ltd/hlaeja/configuration/SecurityConfiguration.kt index 08ebb14..415a37e 100644 --- a/src/main/kotlin/ltd/hlaeja/configuration/SecurityConfiguration.kt +++ b/src/main/kotlin/ltd/hlaeja/configuration/SecurityConfiguration.kt @@ -40,5 +40,6 @@ class SecurityConfiguration { "/actuator/**", "/login", "/logout", + "/", ) } diff --git a/src/main/kotlin/ltd/hlaeja/controller/HomeController.kt b/src/main/kotlin/ltd/hlaeja/controller/HomeController.kt index 676026b..b565861 100644 --- a/src/main/kotlin/ltd/hlaeja/controller/HomeController.kt +++ b/src/main/kotlin/ltd/hlaeja/controller/HomeController.kt @@ -1,13 +1,24 @@ package ltd.hlaeja.controller +import ltd.hlaeja.security.RemoteUserDetail +import org.springframework.security.core.context.ReactiveSecurityContextHolder import org.springframework.stereotype.Controller import org.springframework.ui.Model import org.springframework.web.bind.annotation.GetMapping +import reactor.core.publisher.Mono @Controller class HomeController { - @Suppress("UnusedParameter", "FunctionOnlyReturningConstant") @GetMapping("/") - fun home(model: Model): String = "home/index" + fun home(model: Model): Mono = ReactiveSecurityContextHolder.getContext() + .filter { it.authentication?.isAuthenticated == true } + .map { + (it.authentication.principal as RemoteUserDetail).let { user -> + model.addAttribute("id", user.id) + model.addAttribute("username", user.username) + } + "home/welcome" + } + .defaultIfEmpty("home/index") } diff --git a/src/main/kotlin/ltd/hlaeja/controller/advice/UserAttribute.kt b/src/main/kotlin/ltd/hlaeja/controller/advice/UserAttribute.kt new file mode 100644 index 0000000..d595d7b --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/controller/advice/UserAttribute.kt @@ -0,0 +1,29 @@ +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 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 { + + @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) + } +} diff --git a/src/main/kotlin/ltd/hlaeja/security/RemoteAuthentication.kt b/src/main/kotlin/ltd/hlaeja/security/RemoteAuthentication.kt index b37438f..a4af478 100644 --- a/src/main/kotlin/ltd/hlaeja/security/RemoteAuthentication.kt +++ b/src/main/kotlin/ltd/hlaeja/security/RemoteAuthentication.kt @@ -24,4 +24,13 @@ data class RemoteAuthentication( override fun setAuthenticated(isAuthenticated: Boolean) { authenticated = isAuthenticated } + + fun hasRole(role: String): Boolean { + authorities.forEach { + if (it.authority.equals("role_$role", true)) { + return true + } + } + return false + } } diff --git a/src/main/resources/templates/home/index.html b/src/main/resources/templates/home/index.html index 55922f2..8df7f1d 100644 --- a/src/main/resources/templates/home/index.html +++ b/src/main/resources/templates/home/index.html @@ -9,6 +9,7 @@

Test


This is a index page!

+ login diff --git a/src/main/resources/templates/home/welcome.html b/src/main/resources/templates/home/welcome.html new file mode 100644 index 0000000..2be861f --- /dev/null +++ b/src/main/resources/templates/home/welcome.html @@ -0,0 +1,23 @@ + + + + Home Pages + + + +
+

Welcome

+
+ +
+ You are an admin! +
+
+ You are a user! +
+

This is welcome pages and you're a user!

+ Logout +
+ + +