From c40f1a0036482357743268ca32ff443d320dd281 Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Thu, 23 Jan 2025 14:02:59 +0100 Subject: [PATCH] Basic accounts - add account link in welcome.html - add AuthorizeExchangeSpec adminPaths in SecurityConfiguration - add AccountController - add users.html - add getAccounts in AccountRegistryService - add WebClient accountRegistryAccounts in webClient --- .../configuration/SecurityConfiguration.kt | 5 +++ .../hlaeja/controller/AccountController.kt | 30 +++++++++++++++++ .../hlaeja/service/AccountRegistryService.kt | 11 +++++++ .../kotlin/ltd/hlaeja/util/WebClientCalls.kt | 11 +++++++ .../resources/templates/account/users.html | 32 +++++++++++++++++++ .../resources/templates/home/welcome.html | 1 + 6 files changed, 90 insertions(+) create mode 100644 src/main/kotlin/ltd/hlaeja/controller/AccountController.kt create mode 100644 src/main/resources/templates/account/users.html diff --git a/src/main/kotlin/ltd/hlaeja/configuration/SecurityConfiguration.kt b/src/main/kotlin/ltd/hlaeja/configuration/SecurityConfiguration.kt index 415a37e..07baa5a 100644 --- a/src/main/kotlin/ltd/hlaeja/configuration/SecurityConfiguration.kt +++ b/src/main/kotlin/ltd/hlaeja/configuration/SecurityConfiguration.kt @@ -31,8 +31,13 @@ class SecurityConfiguration { private fun authorizeExchange(authorizeExchange: AuthorizeExchangeSpec) = authorizeExchange .publicPaths().permitAll() + .adminPaths().hasRole("ADMIN") .anyExchange().authenticated() + private fun AuthorizeExchangeSpec.adminPaths(): AuthorizeExchangeSpec.Access = pathMatchers( + "/account/**" + ) + private fun AuthorizeExchangeSpec.publicPaths(): AuthorizeExchangeSpec.Access = pathMatchers( "/css/**", "/js/**", diff --git a/src/main/kotlin/ltd/hlaeja/controller/AccountController.kt b/src/main/kotlin/ltd/hlaeja/controller/AccountController.kt new file mode 100644 index 0000000..f698201 --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/controller/AccountController.kt @@ -0,0 +1,30 @@ +package ltd.hlaeja.controller + +import ltd.hlaeja.service.AccountRegistryService +import org.springframework.stereotype.Controller +import org.springframework.ui.Model +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RequestParam +import reactor.core.publisher.Mono + +@Controller +@RequestMapping("/account") +class AccountController( + private val accountRegistryService: AccountRegistryService, +) { + + @GetMapping + fun getAccounts( + @RequestParam(defaultValue = "1") page: Int, + @RequestParam(defaultValue = "2") size: Int, + model: Model, + ): Mono = accountRegistryService.getAccounts(page, size) + .collectList() + .doOnNext { items -> + model.addAttribute("items", items) + model.addAttribute("page", page) + model.addAttribute("size", size) + } + .then(Mono.just("account/users")) +} diff --git a/src/main/kotlin/ltd/hlaeja/service/AccountRegistryService.kt b/src/main/kotlin/ltd/hlaeja/service/AccountRegistryService.kt index 0840a76..87fb4b8 100644 --- a/src/main/kotlin/ltd/hlaeja/service/AccountRegistryService.kt +++ b/src/main/kotlin/ltd/hlaeja/service/AccountRegistryService.kt @@ -1,15 +1,20 @@ package ltd.hlaeja.service import io.github.oshai.kotlinlogging.KotlinLogging +import ltd.hlaeja.library.accountRegistry.Account import ltd.hlaeja.library.accountRegistry.Authentication import ltd.hlaeja.property.AccountRegistryProperty +import ltd.hlaeja.util.accountRegistryAccounts import ltd.hlaeja.util.accountRegistryAuthenticate +import org.springframework.http.HttpStatus.BAD_REQUEST import org.springframework.security.authentication.AuthenticationServiceException import org.springframework.security.core.AuthenticationException import org.springframework.stereotype.Service import org.springframework.web.reactive.function.client.WebClient import org.springframework.web.reactive.function.client.WebClientRequestException import org.springframework.web.reactive.function.client.WebClientResponseException +import org.springframework.web.server.ResponseStatusException +import reactor.core.publisher.Flux import reactor.core.publisher.Mono private val log = KotlinLogging.logger {} @@ -43,4 +48,10 @@ class AccountRegistryService( } } } + + fun getAccounts( + page: Int, + size: Int, + ): Flux = webClient.accountRegistryAccounts(page, size, property) + .onErrorResume { error -> Flux.error(ResponseStatusException(BAD_REQUEST, error.message, error)) } } diff --git a/src/main/kotlin/ltd/hlaeja/util/WebClientCalls.kt b/src/main/kotlin/ltd/hlaeja/util/WebClientCalls.kt index f6c2963..23c60b6 100644 --- a/src/main/kotlin/ltd/hlaeja/util/WebClientCalls.kt +++ b/src/main/kotlin/ltd/hlaeja/util/WebClientCalls.kt @@ -1,5 +1,6 @@ package ltd.hlaeja.util +import ltd.hlaeja.library.accountRegistry.Account import ltd.hlaeja.library.accountRegistry.Authentication import ltd.hlaeja.property.AccountRegistryProperty import org.springframework.http.HttpStatus.LOCKED @@ -9,6 +10,7 @@ import org.springframework.security.authentication.BadCredentialsException import org.springframework.security.authentication.LockedException import org.springframework.security.core.userdetails.UsernameNotFoundException import org.springframework.web.reactive.function.client.WebClient +import reactor.core.publisher.Flux import reactor.core.publisher.Mono fun WebClient.accountRegistryAuthenticate( @@ -22,3 +24,12 @@ fun WebClient.accountRegistryAuthenticate( .onStatus(UNAUTHORIZED::equals) { throw BadCredentialsException("Invalid credentials") } .onStatus(NOT_FOUND::equals) { throw UsernameNotFoundException("User not found") } .bodyToMono(Authentication.Response::class.java) + +fun WebClient.accountRegistryAccounts( + page: Int, + size: Int, + property: AccountRegistryProperty +): Flux = get() + .uri("${property.url}/accounts?page=$page&size=$size".also(::logCall)) + .retrieve() + .bodyToFlux(Account.Response::class.java) diff --git a/src/main/resources/templates/account/users.html b/src/main/resources/templates/account/users.html new file mode 100644 index 0000000..a52cc4c --- /dev/null +++ b/src/main/resources/templates/account/users.html @@ -0,0 +1,32 @@ + + + + Home Pages + + + +
+

Test

+
+ + + + + + + + + + + +
IdNameDescription
IDtimestampusername
+ + Next + Previous + +
+ Logout +
+ + + diff --git a/src/main/resources/templates/home/welcome.html b/src/main/resources/templates/home/welcome.html index 2be861f..af271bf 100644 --- a/src/main/resources/templates/home/welcome.html +++ b/src/main/resources/templates/home/welcome.html @@ -11,6 +11,7 @@
You are an admin! + Account
You are a user!