From 8ca7424686bc3a16fb1de3b2dd5b8de60cd826ae Mon Sep 17 00:00:00 2001 From: Paul Heimer Date: Mon, 6 Nov 2023 19:48:33 +0100 Subject: [PATCH] Add prototype of login/register --- src/main/java/catering/Application.java | 15 +++- .../catering/customer/CustomerController.java | 35 ++++++++++ .../catering/customer/RegistrationForm.java | 32 +++++++++ src/main/resources/templates/login.html | 69 +++++++++++++++++++ src/main/resources/templates/register.html | 40 +++++++++++ 5 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 src/main/java/catering/customer/CustomerController.java create mode 100644 src/main/java/catering/customer/RegistrationForm.java create mode 100644 src/main/resources/templates/login.html create mode 100644 src/main/resources/templates/register.html diff --git a/src/main/java/catering/Application.java b/src/main/java/catering/Application.java index 160ed73..bdc6763 100644 --- a/src/main/java/catering/Application.java +++ b/src/main/java/catering/Application.java @@ -22,6 +22,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer.FrameOptionsConfig; import org.springframework.security.web.SecurityFilterChain; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * The main application class. @@ -42,14 +44,23 @@ public class Application { static class WebSecurityConfiguration { @Bean - SecurityFilterChain videoShopSecurity(HttpSecurity http) throws Exception { + SecurityFilterChain WebSecurity(HttpSecurity http) throws Exception { return http .headers(headers -> headers.frameOptions(FrameOptionsConfig::sameOrigin)) .csrf(csrf -> csrf.disable()) - .formLogin(login -> login.loginProcessingUrl("/login")) + .formLogin(login -> login.loginPage("/login").loginProcessingUrl("/login")) .logout(logout -> logout.logoutUrl("/logout").logoutSuccessUrl("/")) .build(); } } + + @Configuration + static class WebConfiguration implements WebMvcConfigurer { + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/login").setViewName("login"); + registry.addViewController("/").setViewName("index"); + } + } } diff --git a/src/main/java/catering/customer/CustomerController.java b/src/main/java/catering/customer/CustomerController.java new file mode 100644 index 0000000..4241a6f --- /dev/null +++ b/src/main/java/catering/customer/CustomerController.java @@ -0,0 +1,35 @@ +package catering.customer; + +import org.salespointframework.useraccount.Password; +import org.salespointframework.useraccount.UserAccountManagement; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@Controller +public class CustomerController { + + UserAccountManagement userAccountManagement; + CustomerController(UserAccountManagement userAccountManagement) { + + this.userAccountManagement = userAccountManagement; + } + + @GetMapping("/register") + String register() { + return "register"; + } + + @GetMapping("/login") + public String loginPage(){ + + return "login"; + } + + @PostMapping("/register") + String register(@RequestParam String username, @RequestParam String password) { + userAccountManagement.create(username, Password.UnencryptedPassword.of(password)); + return "redirect:/"; + } +} diff --git a/src/main/java/catering/customer/RegistrationForm.java b/src/main/java/catering/customer/RegistrationForm.java new file mode 100644 index 0000000..53a3e07 --- /dev/null +++ b/src/main/java/catering/customer/RegistrationForm.java @@ -0,0 +1,32 @@ +package catering.customer; + +import jakarta.validation.constraints.NotEmpty; +import org.springframework.validation.Errors; + +public class RegistrationForm { + + private final @NotEmpty String name, password, address; + + public RegistrationForm(String name, String password, String address) { + + this.name = name; + this.password = password; + this.address = address; + } + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String getAddress() { + return address; + } + + public void validate(Errors errors) { + + } +} diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html new file mode 100644 index 0000000..b076068 --- /dev/null +++ b/src/main/resources/templates/login.html @@ -0,0 +1,69 @@ + + +

Login

+ + + + + +
+ +
+ + + + + +
+ +
+ + + + + +
+ +
+ + + +
+ +
+ +

Haben Sie noch keinen Account? Registrieren Sie sich hier.

+ + diff --git a/src/main/resources/templates/register.html b/src/main/resources/templates/register.html new file mode 100644 index 0000000..6854d81 --- /dev/null +++ b/src/main/resources/templates/register.html @@ -0,0 +1,40 @@ + + +

Registrierung

+ +
+ +
+ + + + + +
+ +
+ + + + +
+ +
+ + + + + +
+ +
+ + + +
+ +
+ + +