diff --git a/src/main/java/com/webproglabs/lab1/SecurityConfiguration.java b/src/main/java/com/webproglabs/lab1/SecurityConfiguration.java index b5e5539..6af1523 100644 --- a/src/main/java/com/webproglabs/lab1/SecurityConfiguration.java +++ b/src/main/java/com/webproglabs/lab1/SecurityConfiguration.java @@ -1,5 +1,8 @@ package com.webproglabs.lab1; +import com.webproglabs.lab1.models.UserRole; +import com.webproglabs.lab1.mvc.UserSignupMvcController; +import com.webproglabs.lab1.services.UserService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Bean; @@ -29,47 +32,47 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter { private final Logger log = LoggerFactory.getLogger(SecurityConfiguration.class); private static final String LOGIN_URL = "/login"; public static final String SPA_URL_MASK = "/{path:[^\\.]*}"; -// private final ProfileService userService; -// -// public SecurityConfiguration(ProfileService userService) { -// this.userService = userService; -// createAdminOnStartup(); -// } + private final UserService userService; -// private void createAdminOnStartup() { -// final String admin = "admin"; -// if (userService.findByLogin(admin) == null) { -// log.info("Admin user successfully created"); -// try { -// userService.createUser(admin, admin, admin, UserRole.ADMIN); -// } catch (Exception e) { -// throw new RuntimeException(e); -// } -// } -// } + public SecurityConfiguration(UserService userService) { + this.userService = userService; + createAdminOnStartup(); + } -// @Override -// protected void configure(HttpSecurity http) throws Exception { -// -// http.exceptionHandling().authenticationEntryPoint(delegatingEntryPoint()); -// http.headers().frameOptions().sameOrigin().and() -// .cors().and() -// .csrf().disable() -// .authorizeRequests() -// .antMatchers(UserSignupMvcController.SIGNUP_URL).permitAll() -// .antMatchers(HttpMethod.GET, LOGIN_URL).permitAll() -// .anyRequest().authenticated() -// .and() -// .formLogin() -// .loginPage(LOGIN_URL).permitAll() -// .and() -// .logout().permitAll(); -// } + private void createAdminOnStartup() { + final String admin = "admin"; + if (userService.findUserByLogin(admin) == null) { + log.info("Admin user successfully created"); + try { + userService.createUser(admin, admin, admin, UserRole.ADMIN); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } -// @Override -// protected void configure(AuthenticationManagerBuilder auth) throws Exception { -// auth.userDetailsService(userService); -// } + @Override + protected void configure(HttpSecurity http) throws Exception { + + http.exceptionHandling().authenticationEntryPoint(delegatingEntryPoint()); + http.headers().frameOptions().sameOrigin().and() + .cors().and() + .csrf().disable() + .authorizeRequests() + .antMatchers(UserSignupMvcController.SIGNUP_URL).permitAll() + .antMatchers(HttpMethod.GET, LOGIN_URL).permitAll() + .anyRequest().authenticated() + .and() + .formLogin() + .loginPage(LOGIN_URL).permitAll() + .and() + .logout().permitAll(); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.userDetailsService(userService); + } @Override public void configure(WebSecurity web) { diff --git a/src/main/java/com/webproglabs/lab1/dto/PostDto.java b/src/main/java/com/webproglabs/lab1/dto/PostDto.java index 001618b..4f0d3bf 100644 --- a/src/main/java/com/webproglabs/lab1/dto/PostDto.java +++ b/src/main/java/com/webproglabs/lab1/dto/PostDto.java @@ -28,6 +28,9 @@ public class PostDto { return id; } public String getText() {return text;} + public void setText(String text) { + this.text = text; + } public List getComments() {return comments;} public String getAuthor() {return authorLogin;} } diff --git a/src/main/java/com/webproglabs/lab1/dto/UserSignupDto.java b/src/main/java/com/webproglabs/lab1/dto/UserSignupDto.java new file mode 100644 index 0000000..c410a01 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/dto/UserSignupDto.java @@ -0,0 +1,40 @@ +package com.webproglabs.lab1.dto; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Size; + +public class UserSignupDto { + @NotBlank + @Size(min = 4, max = 64) + private String login; + @NotBlank + @Size(min = 4, max = 64) + private String password; + @NotBlank + @Size(min = 4, 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; + } +} diff --git a/src/main/java/com/webproglabs/lab1/models/User.java b/src/main/java/com/webproglabs/lab1/models/User.java index 05d5909..b7661c4 100644 --- a/src/main/java/com/webproglabs/lab1/models/User.java +++ b/src/main/java/com/webproglabs/lab1/models/User.java @@ -21,7 +21,7 @@ public class User { @Column(nullable = false, length = 64) @NotBlank - @Size(min = 6, max = 64) + @Size(min = 4, max = 64) private String password; private UserRole role; diff --git a/src/main/java/com/webproglabs/lab1/mvc/UserSignupMvcController.java b/src/main/java/com/webproglabs/lab1/mvc/UserSignupMvcController.java new file mode 100644 index 0000000..9e14725 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/mvc/UserSignupMvcController.java @@ -0,0 +1,50 @@ +package com.webproglabs.lab1.mvc; + +import com.webproglabs.lab1.dto.UserSignupDto; +import com.webproglabs.lab1.models.User; +import com.webproglabs.lab1.services.UserService; +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 javax.validation.Valid; + +@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 (Exception e) { + model.addAttribute("errors", e.getMessage()); + return "signup"; + } + } + +} diff --git a/src/main/java/com/webproglabs/lab1/services/UserService.java b/src/main/java/com/webproglabs/lab1/services/UserService.java index 564614a..d5ebb5a 100644 --- a/src/main/java/com/webproglabs/lab1/services/UserService.java +++ b/src/main/java/com/webproglabs/lab1/services/UserService.java @@ -35,8 +35,8 @@ public class UserService implements UserDetailsService { @Transactional public User findUserByLogin(String login) { - final Optional user = userRepository.findOneByLoginIgnoreCase(login); - return user.orElseThrow(EntityNotFoundException::new); + final User user = userRepository.findOneByLoginIgnoreCase(login).orElse(null); + return user; } @Transactional diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html new file mode 100644 index 0000000..de0a82b --- /dev/null +++ b/src/main/resources/templates/default.html @@ -0,0 +1,41 @@ + + + + + + + СоцСеточка + + + + + + + + + + + +
+

СоцСеточка

+
+
+

+ Топики + Пользователи + Лента + + Выход + +

+
+ +
+
+
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html new file mode 100644 index 0000000..509d6ac --- /dev/null +++ b/src/main/resources/templates/error.html @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html new file mode 100644 index 0000000..5159c8d --- /dev/null +++ b/src/main/resources/templates/login.html @@ -0,0 +1,31 @@ + + + + +
+
+ Пользователь не найден или пароль указан не верно +
+
+ Выход успешно произведен +
+
+ Пользователь '' успешно создан +
+
+
+ +
+
+ +
+ + Регистрация +
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/signup.html b/src/main/resources/templates/signup.html new file mode 100644 index 0000000..6eb4da4 --- /dev/null +++ b/src/main/resources/templates/signup.html @@ -0,0 +1,29 @@ + + + + +
+
+
+
+ +
+
+ +
+
+ +
+
+ + Назад +
+
+
+ + \ No newline at end of file