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.RequestMapping;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import ru.redrise.marinesco.User;
|
||||
import ru.redrise.marinesco.data.RolesRepository;
|
||||
import ru.redrise.marinesco.data.UserRepository;
|
||||
import ru.redrise.marinesco.User;
|
||||
|
||||
@Slf4j
|
||||
@Controller
|
||||
|
@ -22,11 +23,16 @@ public class RegistrationController {
|
|||
private UserRepository userRepo;
|
||||
private RolesRepository rolesRepo;
|
||||
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.rolesRepo = rolesRepo;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
@ModelAttribute(name = "registrationForm")
|
||||
|
@ -51,6 +57,9 @@ public class RegistrationController {
|
|||
|
||||
User user = userRepo.save(registerForm.toUser(passwordEncoder, rolesRepo));
|
||||
log.info("Added user {} {} {}", user.getId(), user.getUsername(), user.getDisplayname());
|
||||
|
||||
if (registerForm.auth(request))
|
||||
return "redirect:/";
|
||||
return "redirect:/login";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@ package ru.redrise.marinesco.security;
|
|||
|
||||
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.NotNull;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
@ -36,4 +38,14 @@ public class RegistrationForm {
|
|||
public boolean isPasswordsNotEqual(){
|
||||
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("/styles/**")).permitAll()
|
||||
.requestMatchers(mvc.pattern("/images/*")).permitAll()
|
||||
.requestMatchers(mvc.pattern("/register")).permitAll()
|
||||
.requestMatchers(mvc.pattern("/register")).anonymous()
|
||||
.requestMatchers(mvc.pattern("/login")).anonymous()
|
||||
.requestMatchers(mvc.pattern("/error")).permitAll()
|
||||
.requestMatchers(PathRequest.toH2Console()).permitAll()
|
||||
|
|
Loading…
Reference in a new issue