как сделать автоматическую авторизацию после регистрации в spring sequrity
Всем привет. Не могу сделать автоматическую авторизацию после регистрации. После регистрации пользователя кидает на страницу логина и не регистрирует в системе. Заранее спасибо за ответы!
@Autowired
private AuthenticationManager authenticationManager;
@PostMapping("/registration")
public String registration(@ModelAttribute("userForm") User userForm, Model model, HttpServletRequest request) {
if (userService.findByUsername(userForm.getUsername()) != null) {
model.addAttribute("userPresent", true);
return "registration";
}
userService.save(userForm);
authWithAuthManager(request, userForm.getUsername(), userForm.getPassword());
return "redirect:/main";
}
public void authWithAuthManager(HttpServletRequest request, String username, String password) {
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(username, password);
authToken.setDetails(new WebAuthenticationDetails(request));
Authentication authentication = authenticationManager.authenticate(authToken);
SecurityContextHolder.getContext().setAuthentication(authentication);
}
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<!--default-target-url="/ "-->
<http auto-config="true">
<intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')" />
<access-denied-handler error-page="/accessDenied"/>
<form-login login-page="/login" authentication-failure-url="/login?error"
username-parameter="username" password-parameter="password"/>
<logout logout-success-url="/login?logout"/>
<remember-me key="uniqueAndSecret"/>
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userDetailsServiceImpl">
<password-encoder ref="encoder"/>
</authentication-provider>
</authentication-manager>
<beans:bean id="userDetailsServiceImpl"
class="org.test.service.UserDetailsServiceImpl"/>
<beans:bean id="encoder"
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
<beans:constructor-arg name="strength" value="11"/>
</beans:bean>
</beans:beans>