我需要在注册成功后自动登录。
java.lang.StackOverflowError : null
,但在通过 Postman 测试我的代码时得到了。
控制器类:
@RestController
public class RegistrationController {
@Autowired
private UserService userService;
@Autowired
private AuthenticationManager authenticationManager;
@PostMapping("/api/user/registration")
public ResponseEntity registerNewUserAccount(
@RequestBody @Valid RegistrationDto userDto, HttpServletRequest request){
userService.save(userDto);
authenticateUser(userDto, request);
return ResponseEntity.ok().build();
}
private void authenticateUser(RegistrationDto userDto, HttpServletRequest request){
String username = userDto.getEmailAddress();
String password = userDto.getPassword();
UsernamePasswordAuthenticationToken token =
new UsernamePasswordAuthenticationToken(username, password);
request.getSession();
token.setDetails(new WebAuthenticationDetails(request));
Authentication authenticatedUser =
authenticationManager.authenticate(token);
SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
}
}
安全配置类:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
super.configure(auth);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().permitAll()
.and()
.csrf().disable();
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public BCryptPasswordEncoder cryptPasswordEncoder(){
return new BCryptPasswordEncoder();
}
}
我知道StackOverflowError并且我猜测AuthenticationManagerBuilder或者authenticationManagerBean应该导致这个问题。
摇曳的蔷薇
相关分类