From 3bd6f3b5afe61ce4490348a61fd8412b8c15f3a6 Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Wed, 15 Jan 2025 12:41:32 +0100 Subject: [PATCH] Added basic login and logout functionality - update SecurityConfiguration - add logout - add login - add AuthenticationController - add goodbye.html - add logout.html - add login.html --- .../configuration/SecurityConfiguration.kt | 15 ++++++++++++ .../controller/AuthenticationController.kt | 22 ++++++++++++++++++ .../templates/authentication/goodbye.html | 16 +++++++++++++ .../templates/authentication/login.html | 23 +++++++++++++++++++ .../templates/authentication/logout.html | 17 ++++++++++++++ 5 files changed, 93 insertions(+) create mode 100644 src/main/kotlin/ltd/hlaeja/controller/AuthenticationController.kt create mode 100644 src/main/resources/templates/authentication/goodbye.html create mode 100644 src/main/resources/templates/authentication/login.html create mode 100644 src/main/resources/templates/authentication/logout.html diff --git a/src/main/kotlin/ltd/hlaeja/configuration/SecurityConfiguration.kt b/src/main/kotlin/ltd/hlaeja/configuration/SecurityConfiguration.kt index 55aec74..08ebb14 100644 --- a/src/main/kotlin/ltd/hlaeja/configuration/SecurityConfiguration.kt +++ b/src/main/kotlin/ltd/hlaeja/configuration/SecurityConfiguration.kt @@ -2,9 +2,11 @@ package ltd.hlaeja.configuration import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration +import org.springframework.http.HttpStatus.FOUND import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity import org.springframework.security.config.web.server.ServerHttpSecurity import org.springframework.security.config.web.server.ServerHttpSecurity.AuthorizeExchangeSpec +import org.springframework.security.config.web.server.ServerHttpSecurity.FormLoginSpec import org.springframework.security.web.server.SecurityWebFilterChain @Configuration @@ -14,8 +16,19 @@ class SecurityConfiguration { @Bean fun securityWebFilterChain(serverHttpSecurity: ServerHttpSecurity): SecurityWebFilterChain = serverHttpSecurity .authorizeExchange(::authorizeExchange) + .formLogin(::formLogin) + .logout(::logout) .build() + private fun logout(logout: ServerHttpSecurity.LogoutSpec) = logout.logoutUrl("/logout") + .logoutSuccessHandler { webFilter, _ -> + webFilter.exchange.response.headers.add("Location", "/logout") + webFilter.exchange.response.statusCode = FOUND + webFilter.exchange.response.setComplete() + } + + private fun formLogin(login: FormLoginSpec) = login.loginPage("/login") + private fun authorizeExchange(authorizeExchange: AuthorizeExchangeSpec) = authorizeExchange .publicPaths().permitAll() .anyExchange().authenticated() @@ -25,5 +38,7 @@ class SecurityConfiguration { "/js/**", "/img/**", "/actuator/**", + "/login", + "/logout", ) } diff --git a/src/main/kotlin/ltd/hlaeja/controller/AuthenticationController.kt b/src/main/kotlin/ltd/hlaeja/controller/AuthenticationController.kt new file mode 100644 index 0000000..70f413e --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/controller/AuthenticationController.kt @@ -0,0 +1,22 @@ +package ltd.hlaeja.controller + +import org.springframework.http.HttpStatus.UNAUTHORIZED +import org.springframework.security.core.context.ReactiveSecurityContextHolder +import org.springframework.stereotype.Controller +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.ResponseStatus +import reactor.core.publisher.Mono + +@Controller +class AuthenticationController { + + @GetMapping("/login") + @ResponseStatus(UNAUTHORIZED) + fun login(): Mono = Mono.just("authentication/login") + + @GetMapping("/logout") + fun logout(): Mono = ReactiveSecurityContextHolder.getContext() + .filter { it.authentication?.isAuthenticated == true } + .map { "authentication/logout" } + .defaultIfEmpty("authentication/goodbye") +} diff --git a/src/main/resources/templates/authentication/goodbye.html b/src/main/resources/templates/authentication/goodbye.html new file mode 100644 index 0000000..fda425c --- /dev/null +++ b/src/main/resources/templates/authentication/goodbye.html @@ -0,0 +1,16 @@ + + + + Goodbye + + + +
+

You are logged out

+
+

We hope to see you again soon!

+ Login Again +
+ + + diff --git a/src/main/resources/templates/authentication/login.html b/src/main/resources/templates/authentication/login.html new file mode 100644 index 0000000..a310c14 --- /dev/null +++ b/src/main/resources/templates/authentication/login.html @@ -0,0 +1,23 @@ + + + + Login + + + +
+

Login

+
+
+ + +
+ + +
+ +
+
+ + + diff --git a/src/main/resources/templates/authentication/logout.html b/src/main/resources/templates/authentication/logout.html new file mode 100644 index 0000000..20c9bf1 --- /dev/null +++ b/src/main/resources/templates/authentication/logout.html @@ -0,0 +1,17 @@ + + + + Logout + + + +
+

Logout

+
+

Are you sure you want to logout?

+
+ +
+ + +