auto-login after registration
This commit is contained in:
parent
ed9bac4d4e
commit
78ddbffb7b
3 changed files with 25 additions and 4 deletions
|
@ -9,11 +9,12 @@ import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import ru.redrise.marinesco.User;
|
||||||
import ru.redrise.marinesco.data.RolesRepository;
|
import ru.redrise.marinesco.data.RolesRepository;
|
||||||
import ru.redrise.marinesco.data.UserRepository;
|
import ru.redrise.marinesco.data.UserRepository;
|
||||||
import ru.redrise.marinesco.User;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Controller
|
@Controller
|
||||||
|
@ -22,11 +23,16 @@ public class RegistrationController {
|
||||||
private UserRepository userRepo;
|
private UserRepository userRepo;
|
||||||
private RolesRepository rolesRepo;
|
private RolesRepository rolesRepo;
|
||||||
private PasswordEncoder passwordEncoder;
|
private PasswordEncoder passwordEncoder;
|
||||||
|
private HttpServletRequest request;
|
||||||
|
|
||||||
public RegistrationController(UserRepository userRepo, RolesRepository rolesRepo, PasswordEncoder passwordEncoder) {
|
public RegistrationController(UserRepository userRepo,
|
||||||
|
RolesRepository rolesRepo,
|
||||||
|
PasswordEncoder passwordEncoder,
|
||||||
|
HttpServletRequest request) {
|
||||||
this.userRepo = userRepo;
|
this.userRepo = userRepo;
|
||||||
this.rolesRepo = rolesRepo;
|
this.rolesRepo = rolesRepo;
|
||||||
this.passwordEncoder = passwordEncoder;
|
this.passwordEncoder = passwordEncoder;
|
||||||
|
this.request = request;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ModelAttribute(name = "registrationForm")
|
@ModelAttribute(name = "registrationForm")
|
||||||
|
@ -41,7 +47,7 @@ public class RegistrationController {
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public String postMethodName(@Valid RegistrationForm registerForm, Errors errors, Model model) {
|
public String postMethodName(@Valid RegistrationForm registerForm, Errors errors, Model model) {
|
||||||
if (registerForm.isPasswordsNotEqual()){
|
if (registerForm.isPasswordsNotEqual()) {
|
||||||
model.addAttribute("passwordsMismatch", "Passwords must be the same.");
|
model.addAttribute("passwordsMismatch", "Passwords must be the same.");
|
||||||
return "registration";
|
return "registration";
|
||||||
}
|
}
|
||||||
|
@ -51,6 +57,9 @@ public class RegistrationController {
|
||||||
|
|
||||||
User user = userRepo.save(registerForm.toUser(passwordEncoder, rolesRepo));
|
User user = userRepo.save(registerForm.toUser(passwordEncoder, rolesRepo));
|
||||||
log.info("Added user {} {} {}", user.getId(), user.getUsername(), user.getDisplayname());
|
log.info("Added user {} {} {}", user.getId(), user.getUsername(), user.getDisplayname());
|
||||||
|
|
||||||
|
if (registerForm.auth(request))
|
||||||
|
return "redirect:/";
|
||||||
return "redirect:/login";
|
return "redirect:/login";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@ package ru.redrise.marinesco.security;
|
||||||
|
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.validation.constraints.NotEmpty;
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import jakarta.validation.constraints.Size;
|
import jakarta.validation.constraints.Size;
|
||||||
|
@ -36,4 +38,14 @@ public class RegistrationForm {
|
||||||
public boolean isPasswordsNotEqual(){
|
public boolean isPasswordsNotEqual(){
|
||||||
return ! password.equals(passwordConfirm);
|
return ! password.equals(passwordConfirm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean auth(HttpServletRequest request) {
|
||||||
|
try{
|
||||||
|
request.login(username, password);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch(Exception e){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,7 @@ public class SecurityConfig {
|
||||||
.requestMatchers(mvc.pattern("/jquery.js")).permitAll()
|
.requestMatchers(mvc.pattern("/jquery.js")).permitAll()
|
||||||
.requestMatchers(mvc.pattern("/styles/**")).permitAll()
|
.requestMatchers(mvc.pattern("/styles/**")).permitAll()
|
||||||
.requestMatchers(mvc.pattern("/images/*")).permitAll()
|
.requestMatchers(mvc.pattern("/images/*")).permitAll()
|
||||||
.requestMatchers(mvc.pattern("/register")).permitAll()
|
.requestMatchers(mvc.pattern("/register")).anonymous()
|
||||||
.requestMatchers(mvc.pattern("/login")).anonymous()
|
.requestMatchers(mvc.pattern("/login")).anonymous()
|
||||||
.requestMatchers(mvc.pattern("/error")).permitAll()
|
.requestMatchers(mvc.pattern("/error")).permitAll()
|
||||||
.requestMatchers(PathRequest.toH2Console()).permitAll()
|
.requestMatchers(PathRequest.toH2Console()).permitAll()
|
||||||
|
|
Loading…
Reference in a new issue