From fbc316039317e7e45465e2a067b69091bbfe8169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Tue, 13 Jun 2023 15:33:09 +0400 Subject: [PATCH] LabWork06 MVC WORK --- build.gradle | 3 +- .../HardwareShop/controller/UserDto.java | 7 ++- .../controller/UserMvcController.java | 41 ++++++++++++++++ .../controller/UserSignUpDTO.java | 40 ++++++++++++++++ .../controller/UserSignUpMvcController.java | 48 +++++++++++++++++++ .../is/sbapp/HardwareShop/models/User.java | 5 +- .../repository/UserRepository.java | 3 +- .../HardwareShop/services/UserService.java | 3 +- .../ulstu/is/sbapp/SecurityConfiguration.java | 23 +++++---- .../ru/ulstu/is/sbapp/WebConfiguration.java | 1 + src/main/resources/application.properties | 3 +- src/main/resources/templates/default.html | 4 ++ .../ulstu/is/sbapp/SbappApplicationTests.java | 2 + 13 files changed, 157 insertions(+), 26 deletions(-) create mode 100644 src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/UserMvcController.java create mode 100644 src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/UserSignUpDTO.java create mode 100644 src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/UserSignUpMvcController.java diff --git a/build.gradle b/build.gradle index e47d4d8..075fcf9 100644 --- a/build.gradle +++ b/build.gradle @@ -20,9 +20,8 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'com.h2database:h2:2.1.210' implementation 'com.auth0:java-jwt:4.4.0' - //implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6' + implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5' implementation 'org.springframework.boot:spring-boot-devtools' - implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect' implementation 'org.webjars:bootstrap:5.1.3' implementation 'org.webjars:jquery:3.6.0' diff --git a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/UserDto.java b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/UserDto.java index bed9acf..9612c4d 100644 --- a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/UserDto.java +++ b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/UserDto.java @@ -1,8 +1,7 @@ -/* package ru.ulstu.is.sbapp.HardwareShop.controller; -import ru.ip.labworks.labworks.bookshop.model.User; -import ru.ip.labworks.labworks.bookshop.model.UserRole; +import ru.ulstu.is.sbapp.HardwareShop.models.User; +import ru.ulstu.is.sbapp.HardwareShop.models.UserRole; public class UserDto { private final long id; @@ -26,4 +25,4 @@ public class UserDto { public UserRole getRole() { return role; } -}*/ +} diff --git a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/UserMvcController.java b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/UserMvcController.java new file mode 100644 index 0000000..8b9454b --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/UserMvcController.java @@ -0,0 +1,41 @@ +package ru.ulstu.is.sbapp.HardwareShop.controller; + +import org.springframework.data.domain.Page; +import org.springframework.security.access.annotation.Secured; +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 ru.ulstu.is.sbapp.HardwareShop.models.UserRole; +import ru.ulstu.is.sbapp.HardwareShop.services.UserService; + +import java.util.List; +import java.util.stream.IntStream; + +@Controller +@RequestMapping("/users") +public class UserMvcController { + private final UserService userService; + + public UserMvcController(UserService userService) { + this.userService = userService; + } + + @GetMapping + @Secured({UserRole.AsString.ADMIN}) + public String getUsers(@RequestParam(defaultValue = "1") int page, + @RequestParam(defaultValue = "5") int size, + Model model) { + final Page users = userService.findAllPages(page, size) + .map(UserDto::new); + model.addAttribute("users", users); + final int totalPages = users.getTotalPages(); + final List pageNumbers = IntStream.rangeClosed(1, totalPages) + .boxed() + .toList(); + model.addAttribute("pages", pageNumbers); + model.addAttribute("totalPages", totalPages); + return "users"; + } +} \ No newline at end of file diff --git a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/UserSignUpDTO.java b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/UserSignUpDTO.java new file mode 100644 index 0000000..8235bf3 --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/UserSignUpDTO.java @@ -0,0 +1,40 @@ +package ru.ulstu.is.sbapp.HardwareShop.controller; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; + +public class UserSignUpDTO { + @NotBlank + @Size(min = 3, max = 64) + private String login; + @NotBlank + @Size(min = 6, max = 64) + private String password; + @NotBlank + @Size(min = 6, max = 64) + private String passwordConfirm; + + public String getLogin() { + return login; + } + + public void setLogin(String login) { + this.login = login; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getPasswordConfirm() { + return passwordConfirm; + } + + public void setPasswordConfirm(String passwordConfirm) { + this.passwordConfirm = passwordConfirm; + } +} \ No newline at end of file diff --git a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/UserSignUpMvcController.java b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/UserSignUpMvcController.java new file mode 100644 index 0000000..74e6a17 --- /dev/null +++ b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/controller/UserSignUpMvcController.java @@ -0,0 +1,48 @@ +package ru.ulstu.is.sbapp.HardwareShop.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import ru.ulstu.is.sbapp.HardwareShop.models.User; +import ru.ulstu.is.sbapp.HardwareShop.services.UserService; + +import jakarta.validation.Valid; +import jakarta.validation.ValidationException; + +@Controller +@RequestMapping(UserSignUpMvcController.SIGNUP_URL) +public class UserSignUpMvcController { + public static final String SIGNUP_URL = "/signup"; + private final UserService userService; + + public UserSignUpMvcController(UserService userService) { + this.userService = userService; + } + + @GetMapping + public String showSignupForm(Model model) { + model.addAttribute("UserDTO", new UserSignUpDTO()); + return "signup"; + } + + @PostMapping + public String signup(@ModelAttribute("UserDTO") @Valid UserSignUpDTO userSignupDto, + BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "signup"; + } + try { + final User user = userService.createUser(userSignupDto.getLogin(), userSignupDto.getPassword(), userSignupDto.getPasswordConfirm()); + return "redirect:/login?created=" + user.getLogin(); + } catch (ValidationException e) { + model.addAttribute("errors", e.getMessage()); + return "signup"; + } + } +} \ No newline at end of file diff --git a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/User.java b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/User.java index 65ce268..bc5d394 100644 --- a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/User.java +++ b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/models/User.java @@ -1,10 +1,9 @@ -/* package ru.ulstu.is.sbapp.HardwareShop.models; -import jakarta.persistance.*; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Size; +import javax.persistence.*; import java.util.Objects; @Entity @@ -81,4 +80,4 @@ public class User { ", role='" + role + '\'' + '}'; } -}*/ +} diff --git a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/repository/UserRepository.java b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/repository/UserRepository.java index 1857a3b..16e15ed 100644 --- a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/repository/UserRepository.java +++ b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/repository/UserRepository.java @@ -1,4 +1,3 @@ -/* package ru.ulstu.is.sbapp.HardwareShop.repository; import org.springframework.data.jpa.repository.JpaRepository; @@ -6,4 +5,4 @@ import ru.ulstu.is.sbapp.HardwareShop.models.User; public interface UserRepository extends JpaRepository { User findOneByLoginIgnoreCase(String login); -}*/ +} diff --git a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/services/UserService.java b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/services/UserService.java index c666ee7..e95f79a 100644 --- a/src/main/java/ru/ulstu/is/sbapp/HardwareShop/services/UserService.java +++ b/src/main/java/ru/ulstu/is/sbapp/HardwareShop/services/UserService.java @@ -1,4 +1,3 @@ -/* package ru.ulstu.is.sbapp.HardwareShop.services; import org.springframework.data.domain.Page; @@ -64,4 +63,4 @@ public class UserService implements UserDetailsService { final Optional user = userRepository.findById(id); return user.orElseThrow(() -> new UserNotFoundException(id)); } -}*/ +} diff --git a/src/main/java/ru/ulstu/is/sbapp/SecurityConfiguration.java b/src/main/java/ru/ulstu/is/sbapp/SecurityConfiguration.java index 2dd43ad..3edec25 100644 --- a/src/main/java/ru/ulstu/is/sbapp/SecurityConfiguration.java +++ b/src/main/java/ru/ulstu/is/sbapp/SecurityConfiguration.java @@ -1,4 +1,3 @@ -/* package ru.ulstu.is.sbapp; import org.slf4j.Logger; @@ -11,9 +10,9 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; import org.springframework.security.web.SecurityFilterChain; -import ru.ip.labworks.labworks.bookshop.controller.UserSignUpMvcController; -import ru.ip.labworks.labworks.bookshop.model.UserRole; -import ru.ip.labworks.labworks.bookshop.service.UserService; +import ru.ulstu.is.sbapp.HardwareShop.controller.UserSignUpMvcController; +import ru.ulstu.is.sbapp.HardwareShop.models.UserRole; +import ru.ulstu.is.sbapp.HardwareShop.services.UserService; @Configuration @EnableWebSecurity @@ -44,13 +43,13 @@ public class SecurityConfiguration { .cors().and() .csrf().disable() .authorizeHttpRequests() - .requestMatchers(UserSignUpMvcController.SIGNUP_URL).permitAll() - .requestMatchers(HttpMethod.GET, LOGIN_URL).permitAll() + .antMatchers(UserSignUpMvcController.SIGNUP_URL).permitAll() + .antMatchers(HttpMethod.GET, LOGIN_URL).permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage(LOGIN_URL).permitAll() - .defaultSuccessUrl("/author", true) + .defaultSuccessUrl("/product", true) .and() .logout().permitAll(); return http.userDetailsService(userService).build(); @@ -60,9 +59,9 @@ public class SecurityConfiguration { @Bean public WebSecurityCustomizer webSecurityCustomizer() { return (web) -> web.ignoring() - .requestMatchers("/css/**") - .requestMatchers("/js/**") - .requestMatchers("/templates/**") - .requestMatchers("/webjars/**"); + .antMatchers("/css/**") + .antMatchers("/js/**") + .antMatchers("/templates/**") + .antMatchers("/webjars/**"); } -}*/ +} diff --git a/src/main/java/ru/ulstu/is/sbapp/WebConfiguration.java b/src/main/java/ru/ulstu/is/sbapp/WebConfiguration.java index 2e3ce44..398da03 100644 --- a/src/main/java/ru/ulstu/is/sbapp/WebConfiguration.java +++ b/src/main/java/ru/ulstu/is/sbapp/WebConfiguration.java @@ -11,6 +11,7 @@ public class WebConfiguration implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { WebMvcConfigurer.super.addViewControllers(registry); + registry.addViewController("login"); registry.addViewController("product"); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ccc05e8..cc5315b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -8,4 +8,5 @@ spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.jpa.hibernate.ddl-auto=update spring.h2.console.enabled=true spring.h2.console.settings.trace=false -spring.h2.console.settings.web-allow-others=false \ No newline at end of file +spring.h2.console.settings.web-allow-others=false +jwt.dev-token=my-secret-jwt \ No newline at end of file diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index c437993..00d0529 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -1,7 +1,9 @@ + Онлайн магазин @@ -26,6 +28,8 @@ Products Categories Manufacturers + Пользователи + Logout diff --git a/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java b/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java index 7862fee..a34cf15 100644 --- a/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java +++ b/src/test/java/ru/ulstu/is/sbapp/SbappApplicationTests.java @@ -1,3 +1,4 @@ +/* package ru.ulstu.is.sbapp; import org.junit.jupiter.api.Assertions; @@ -89,3 +90,4 @@ class SbappApplicationTests { Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> calculatorService.getSum(1, 2, "date")); } } +*/