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
This commit is contained in:
@@ -40,5 +40,6 @@ class SecurityConfiguration {
|
|||||||
"/actuator/**",
|
"/actuator/**",
|
||||||
"/login",
|
"/login",
|
||||||
"/logout",
|
"/logout",
|
||||||
|
"/",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,24 @@
|
|||||||
package ltd.hlaeja.controller
|
package ltd.hlaeja.controller
|
||||||
|
|
||||||
|
import ltd.hlaeja.security.RemoteUserDetail
|
||||||
|
import org.springframework.security.core.context.ReactiveSecurityContextHolder
|
||||||
import org.springframework.stereotype.Controller
|
import org.springframework.stereotype.Controller
|
||||||
import org.springframework.ui.Model
|
import org.springframework.ui.Model
|
||||||
import org.springframework.web.bind.annotation.GetMapping
|
import org.springframework.web.bind.annotation.GetMapping
|
||||||
|
import reactor.core.publisher.Mono
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
class HomeController {
|
class HomeController {
|
||||||
|
|
||||||
@Suppress("UnusedParameter", "FunctionOnlyReturningConstant")
|
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
fun home(model: Model): String = "home/index"
|
fun home(model: Model): Mono<String> = 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")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,4 +24,13 @@ data class RemoteAuthentication(
|
|||||||
override fun setAuthenticated(isAuthenticated: Boolean) {
|
override fun setAuthenticated(isAuthenticated: Boolean) {
|
||||||
authenticated = isAuthenticated
|
authenticated = isAuthenticated
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun hasRole(role: String): Boolean {
|
||||||
|
authorities.forEach {
|
||||||
|
if (it.authority.equals("role_$role", true)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
<h1>Test</h1>
|
<h1>Test</h1>
|
||||||
<hr>
|
<hr>
|
||||||
<p>This is a index page!</p>
|
<p>This is a index page!</p>
|
||||||
|
<a href="/login">login</a>
|
||||||
</main>
|
</main>
|
||||||
<!--/*/<th:block th:replace="~{layout.html :: script}"/>/*/-->
|
<!--/*/<th:block th:replace="~{layout.html :: script}"/>/*/-->
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
23
src/main/resources/templates/home/welcome.html
Normal file
23
src/main/resources/templates/home/welcome.html
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<!DOCTYPE HTML>
|
||||||
|
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<title>Home Pages</title>
|
||||||
|
<!--/*/<th:block th:insert="~{layout.html :: documentHead}"/>/*/-->
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<main>
|
||||||
|
<h1>Welcome</h1>
|
||||||
|
<hr>
|
||||||
|
<!--/*@thymesVar id="remoteUser" type="ltd.hlaeja.security.RemoteAuthentication"*/-->
|
||||||
|
<div th:if="${remoteUser.hasRole('admin')}">
|
||||||
|
You are an admin!
|
||||||
|
</div>
|
||||||
|
<div th:if="${remoteUser.hasRole('user')}">
|
||||||
|
You are a user!
|
||||||
|
</div>
|
||||||
|
<p>This is welcome pages and you're a user!</p>
|
||||||
|
<a href="/logout">Logout</a>
|
||||||
|
</main>
|
||||||
|
<!--/*/<th:block th:replace="~{layout.html :: script}"/>/*/-->
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user