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
+
+ Welcome
+
+
+
+ You are an admin!
+
+
+ You are a user!
+
+ This is welcome pages and you're a user!
+ Logout
+
+
+
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 @@
+
+
+