13 -- Add registration page

This commit is contained in:
Anton Romanov 2025-02-15 23:53:46 +04:00
parent 5f111a9671
commit 1fe44198b8
6 changed files with 138 additions and 2 deletions

22
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,22 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "FuzzyControllerApplication",
"request": "launch",
"mainClass": "ru.ulstu.fc.FuzzyControllerApplication",
"projectName": "fuzzy-controller"
}
]
}

View File

@ -14,11 +14,10 @@ import ru.ulstu.fc.user.model.UserRoleConstants;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration {
private final Logger log = LoggerFactory.getLogger(SecurityConfiguration.class);
private final String[] permittedUrls = new String[]{
"/login", "/index",
"/login", "/index", "/user/register",
"/public/**", "/organizers", "/webjars/**",
"/h2-console/*", "/h2-console",
"/css/**", "/js/**", "/img/**",

View File

@ -0,0 +1,42 @@
package ru.ulstu.fc.user.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
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.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import ru.ulstu.fc.user.model.User;
import ru.ulstu.fc.user.model.UserDto;
import ru.ulstu.fc.user.service.UserService;
@Controller
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/user/register")
public String showRegistrationForm(WebRequest request, Model model) {
UserDto userDto = new UserDto();
model.addAttribute("user", userDto);
return "register";
}
@PostMapping("/user/register")
public String registerUserAccount(
@ModelAttribute("user") @Valid UserDto userDto,
HttpServletRequest request,
Errors errors) {
userService.createUser(new User(userDto));
return "redirect:/login";
}
}

View File

@ -6,6 +6,7 @@ import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.Table;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
@ -46,6 +47,11 @@ public class User extends BaseEntity {
this.roles = roles;
}
public User(UserDto userDto) {
this.login = userDto.getLogin();
this.password = userDto.getPassword();
}
public String getLogin() {
return login;
}

View File

@ -0,0 +1,40 @@
package ru.ulstu.fc.user.model;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
public class UserDto {
@NotNull
@NotEmpty
private String login;
@NotNull
@NotEmpty
private String password;
private String matchingPassword;
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 getMatchingPassword() {
return matchingPassword;
}
public void setMatchingPassword(String matchingPassword) {
this.matchingPassword = matchingPassword;
}
}

View File

@ -0,0 +1,27 @@
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<div class="container" layout:fragment="content">
<h1 th:text="#{label.form.title}">form</h1>
<form action="/user/register" th:object="${user}" method="POST" enctype="utf8">
<div>
<label th:text="#{label.user.firstName}">first</label>
<input th:field="*{login}" />
<p th:each="error: ${#fields.errors('login')}" th:text="${error}">Validation error</p>
</div>
<div>
<label th:text="#{label.user.password}">password</label>
<input type="password" th:field="*{password}" />
<p th:each="error : ${#fields.errors('password')}" th:text="${error}">Validation error</p>
</div>
<div>
<label th:text="#{label.user.confirmPass}">confirm</label>
<input type="password" th:field="*{matchingPassword}" />
</div>
<button type="submit" th:text="#{label.form.submit}">submit</button>
</form>
<a th:href="@{/login.html}" th:text="#{label.form.loginLink}">login</a>
</div>
</body>
</html>