Skip to content

Commit 54a4b19

Browse files
committed
Completed registration back and front
1 parent 9c3d2f5 commit 54a4b19

File tree

10 files changed

+163
-51
lines changed

10 files changed

+163
-51
lines changed

src/main/java/ru/xpressed/javatemplatescoursework/config/SecurityConfig.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,21 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
2626
@Override
2727
protected void configure(HttpSecurity http) throws Exception {
2828
http
29-
.csrf().disable()
30-
.cors().disable()
3129
.authorizeRequests().antMatchers("/login", "/logout", "/registration").permitAll()
3230
.anyRequest().authenticated()
31+
3332
.and().formLogin()
33+
.loginPage("/login").permitAll()
34+
.loginProcessingUrl("/perform-login")
35+
.usernameParameter("username")
36+
.passwordParameter("password")
37+
.defaultSuccessUrl("/index", true)
38+
.failureUrl("/login?error")
39+
40+
.and()
41+
.logout()
42+
.deleteCookies("JSESSIONID")
43+
3444
.and().userDetailsService(userDetailsService());
3545
}
3646

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ru.xpressed.javatemplatescoursework.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
6+
import javax.servlet.http.HttpServletRequest;
7+
8+
@Controller
9+
public class IndexController {
10+
@GetMapping("/index")
11+
public String showIndexPage(HttpServletRequest request) {
12+
System.out.println(request.getUserPrincipal().getName());
13+
return "index";
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.xpressed.javatemplatescoursework.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
6+
@Controller
7+
public class LoginController {
8+
@GetMapping("/login")
9+
public String showLoginForm() {
10+
return "login";
11+
}
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ru.xpressed.javatemplatescoursework.controller;
2+
3+
import org.springframework.security.core.Authentication;
4+
import org.springframework.security.core.context.SecurityContextHolder;
5+
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
9+
import javax.servlet.http.HttpServletRequest;
10+
import javax.servlet.http.HttpServletResponse;
11+
12+
@Controller
13+
public class LogoutController {
14+
@GetMapping("/logout")
15+
public String executeLogout(HttpServletRequest request, HttpServletResponse response) {
16+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
17+
if (authentication != null) {
18+
new SecurityContextLogoutHandler().logout(request, response, authentication);
19+
}
20+
return "redirect:/login?logout";
21+
}
22+
}

src/main/java/ru/xpressed/javatemplatescoursework/controller/RegistrationController.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.stereotype.Controller;
55
import org.springframework.ui.Model;
6+
import org.springframework.validation.BindingResult;
67
import org.springframework.web.bind.annotation.GetMapping;
78
import org.springframework.web.bind.annotation.PostMapping;
89
import ru.xpressed.javatemplatescoursework.config.SecurityConfig;
910
import ru.xpressed.javatemplatescoursework.entity.Customer;
1011
import ru.xpressed.javatemplatescoursework.repository.CustomerRepository;
1112

13+
import javax.validation.Valid;
14+
1215
@Controller
1316
public class RegistrationController {
1417
@Autowired
@@ -17,19 +20,31 @@ public class RegistrationController {
1720
SecurityConfig securityConfig;
1821

1922
@GetMapping("/registration")
20-
public String showRegistrationForm(Model model) {
21-
model.addAttribute("user", new Customer());
23+
public String showRegistrationForm(Customer customer, Model model) {
24+
model.addAttribute("customer", new Customer());
2225
return "registration";
2326
}
2427

2528
@PostMapping("/registration")
26-
public String completeRegistration(Model model, Customer customer) {
29+
public String completeRegistration(@Valid Customer customer, BindingResult bindingResult, Model model) {
30+
if (bindingResult.hasErrors()) {
31+
model.addAttribute("customer", customer);
32+
return "registration";
33+
}
34+
35+
if (!customer.getPassword().equals(customer.getRepeated())) {
36+
model.addAttribute("reperr", "Passwords don't match!");
37+
return "registration";
38+
}
39+
2740
if (customerRepository.findByUsername(customer.getUsername()) == null) {
2841
customer.setPassword(securityConfig.encoder().encode(customer.getPassword()));
2942
customerRepository.save(customer);
30-
return "registrationCompleted";
43+
model.addAttribute("message", "Registration Completed");
44+
model.addAttribute("onload", "redirect()");
3145
} else {
32-
return "registrationFailed";
46+
bindingResult.rejectValue("username", "customer.username", "This username is already taken!");
3347
}
48+
return "registration";
3449
}
3550
}

src/main/java/ru/xpressed/javatemplatescoursework/entity/Customer.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package ru.xpressed.javatemplatescoursework.entity;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
34
import lombok.Getter;
45
import lombok.Setter;
56
import org.springframework.security.core.GrantedAuthority;
67
import org.springframework.security.core.userdetails.UserDetails;
78

89
import javax.persistence.*;
10+
import javax.validation.constraints.NotEmpty;
911
import java.util.Collection;
1012

1113
@Entity
@@ -14,12 +16,16 @@
1416
@Setter
1517
public class Customer implements UserDetails {
1618
@Id
17-
@GeneratedValue(strategy = GenerationType.AUTO)
18-
private int id;
19-
19+
@NotEmpty(message = "Username can not be empty!")
2020
private String username;
21+
22+
@NotEmpty(message = "Password can not be empty!")
2123
private String password;
2224

25+
@Column(updatable = false, insertable = false)
26+
@NotEmpty(message = "Repeated password can not be empty!")
27+
private String repeated;
28+
2329
@Override
2430
public Collection<? extends GrantedAuthority> getAuthorities() {
2531
return null;

src/main/resources/templates/registrationFailed.html renamed to src/main/resources/templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
<title>Title</title>
66
</head>
77
<body>
8-
Registration Failed!
8+
<h1>INDEX</h1>
99
</body>
1010
</html>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml"
3+
xmlns:th="http://www.thymeleaf.org">
4+
<head>
5+
</head>
6+
<body>
7+
<div>
8+
<form name="f" th:action="@{/perform-login}" method="post">
9+
<fieldset>
10+
<legend>Войдите</legend>
11+
<div th:if="${param.error}" class="alert alert-error">
12+
Неправильные имя и пароль.
13+
</div>
14+
<div th:if="${param.logout}" class="alert alert-success">
15+
You have been logged out.
16+
</div>
17+
<label for="username">Имя</label>
18+
<input type="text" id="username" name="username"/>
19+
<label for="password">Пароль</label>
20+
<input type="password" id="password" name="password"/>
21+
<div class="form-actions">
22+
<button type="submit" class="btn">Войти</button>
23+
</div>
24+
</fieldset>
25+
</form>
26+
</div>
27+
</body>
28+
</html>
Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,52 @@
11
<!DOCTYPE html>
2-
<html xmlns:th="http://www.thymeleaf.org">
3-
<head>
4-
<meta charset="ISO-8859-1">
5-
<title>Sign Up - CodeJava</title>
6-
</head>
2+
<html lang="en" xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
5+
<meta charset="UTF-8">
6+
<title>Registration</title>
7+
<script>
8+
function redirect() {
9+
setTimeout('location="/login";', 5000);
10+
}
11+
</script>
12+
</head>
13+
<body th:attr="onload=${onload}" style="background-color: #dfdfe9;">
14+
<div class="mt-5 pt-5 container-fluid">
15+
<div class="row justify-content-center">
16+
<div class="col-10 col-xl-5">
17+
<div style="background-color: white; border-radius: 25px">
18+
<p class="text-center display-4">Registration</p>
719

8-
<body>
9-
<div class="container text-center">
10-
<div>
11-
<h1>User Registration - Sign Up</h1>
12-
</div>
13-
<form th:action="@{/registration}" th:object="${user}"
14-
method="post" style="max-width: 600px; margin: 0 auto;">
15-
<div class="m-3">
16-
<div class="form-group row">
17-
<label class="col-4 col-form-label">Username: </label>
18-
<div class="col-8">
19-
<input th:field="*{username}" class="form-control" required />
20-
</div>
21-
</div>
20+
<form action="#" th:action="@{/registration}" th:object="${customer}" method="post">
2221

23-
<div class="form-group row">
24-
<label class="col-4 col-form-label">Password: </label>
25-
<div class="col-8">
26-
<input type="password" th:field="*{password}" class="form-control"
27-
required minlength="6" maxlength="10"/>
28-
</div>
29-
</div>
22+
<div class="mx-5 px-5 form-group form-group-lg">
23+
<label for="username" class="h4">Your Username</label>
24+
<input type="text" id="username" class="form-control input-lg" th:field="*{username}"/>
25+
<label for="username" th:if="${#fields.hasErrors('username')}" th:errors="*{username}" style="color: red"></label>
26+
</div>
27+
28+
<div class="mx-5 px-5 form-group form-group-lg">
29+
<label for="password" class="h4">Password</label>
30+
<input type="password" id="password" class="form-control" th:field="*{password}"/>
31+
<label for="password" th:if="${#fields.hasErrors('password')}" th:errors="*{password}" style="color: red"></label>
32+
</div>
3033

31-
<div>
32-
<button type="submit" class="btn btn-primary">Sign Up</button>
34+
<div class="mx-5 px-5 form-group form-group-lg">
35+
<label for="repeat" class="h4">Repeat your password</label>
36+
<input type="password" id="repeat" class="form-control" th:field="*{repeated}" />
37+
<label for="repeat" th:if="${#fields.hasErrors('repeated')}" th:errors="*{repeated}" style="color: red"></label>
38+
<label for="repeat" style="color: red" th:text="${reperr}"></label>
39+
</div>
40+
41+
<div class="d-flex justify-content-center m-3">
42+
<button type="submit" class="btn btn-primary btn-lg" style="background-color: #5700f7">Register</button>
43+
</div>
44+
</form>
45+
46+
<p class="text-center h1 fw-bold m-3 pb-3" style="color: #5707f7" th:text="${message}">&nbsp</p>
3347
</div>
3448
</div>
35-
</form>
49+
</div>
3650
</div>
37-
</body>
51+
</body>
3852
</html>

src/main/resources/templates/registrationCompleted.html

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)