Ну почти закончил по лекции, но оно 100% не будет работать((
This commit is contained in:
parent
f735902131
commit
ce4fbf0aeb
@ -13,6 +13,8 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import premium_store.controller.controller.UserSignupMvcController;
|
||||
import premium_store.model.UserRole;
|
||||
import premium_store.service.UserService;
|
||||
|
||||
@Configuration
|
||||
|
@ -0,0 +1,48 @@
|
||||
package premium_store.controller.DTO;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.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 UserSignupDto() {}
|
||||
|
||||
public UserSignupDto( String login, String password, String passwordConfirm) {
|
||||
this.login = login;
|
||||
this.password = password;
|
||||
this.passwordConfirm = passwordConfirm;
|
||||
}
|
||||
|
||||
public void setLogin(String login){
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
public void setPassword(String password){
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public void setPasswordConfirm(String passwordConfirm){
|
||||
this.passwordConfirm = passwordConfirm;
|
||||
}
|
||||
|
||||
public String getLogin(){
|
||||
return login;
|
||||
}
|
||||
|
||||
public String getPassword(){
|
||||
return password;
|
||||
}
|
||||
|
||||
public String getPasswordConfirm(){
|
||||
return passwordConfirm;
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package premium_store.controller.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 premium_store.controller.DTO.ClientDTO;
|
||||
import premium_store.controller.DTO.UserSignupDto;
|
||||
import premium_store.model.GameClient;
|
||||
import premium_store.service.UserService;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.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 GameClient user = userService.createUser(
|
||||
userSignupDto.getLogin(), userSignupDto.getPassword(), userSignupDto.getPasswordConfirm());
|
||||
return "redirect:/login?created=" + user.getNickName();
|
||||
} catch (ValidationException e) {
|
||||
model.addAttribute("errors", e.getMessage());
|
||||
return "signup";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package premium_store.model;
|
||||
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
|
||||
public enum UserRole implements GrantedAuthority {
|
||||
ADMIN,
|
||||
USER;
|
||||
private static final String PREFIX = "ROLE_";
|
||||
@Override
|
||||
public String getAuthority() {
|
||||
return PREFIX + this.name();
|
||||
}
|
||||
public static final class AsString {
|
||||
public static final String ADMIN = PREFIX + "ADMIN";
|
||||
public static final String USER = PREFIX + "USER";
|
||||
}
|
||||
}
|
@ -40,7 +40,7 @@
|
||||
th:classappend="${#strings.equals(activeLink, '/client')} ? 'active' : ''">Обзор клиентов
|
||||
</a>
|
||||
<a class="nav-link btn border border-3 border-dark fs-4 lh-15 Button_Main_Group text-dark" href="/logout">
|
||||
Выход
|
||||
Выход(<span th:text="${#authentication.name}"></span>)
|
||||
</a>
|
||||
</ul>
|
||||
</div>
|
||||
|
@ -10,6 +10,9 @@
|
||||
<div th:if="${param.error}" class="alert alert-danger margin-bottom">
|
||||
Пользователь не найден или пароль указан не верно
|
||||
</div>
|
||||
<div th:if="${param.created}" class="alert alert-success margin-bottom">
|
||||
Пользователь '<span th:text="${param.created}"></span>' успешно создан
|
||||
</div>
|
||||
<div th:if="${param.logout}" class="alert alert-success margin-bottom">
|
||||
Выход успешно произведен
|
||||
</div>
|
||||
@ -23,6 +26,7 @@
|
||||
placeholder="Пароль" required="true"/>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-success button-fixed">Войти</button>
|
||||
<a class="btn btn-primary button-fixed" href="/signup">Регистрация</a>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
|
Loading…
Reference in New Issue
Block a user