Servlet.service() для сервлета [dispatcherServlet] в контексте с путем [] выдало исключение [Ошибка отправки обработчика: java.lang.StackOverflowError] с основной причиной
Я понял, что программа несколько застревает в рекурсивном цикле, но не понимаю, как это сделать.< /p>
Это мой пользовательский класс:
Код: Выделить всё
@Entity
@Table(name = "users")
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int userId;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private String street;
@Column(nullable = false)
private int streetNo;
@Column(nullable = false)
private int zipCode;
@Column(nullable = false)
private String city;
@ManyToMany // add fetchtypes later
private Set roles;
Код: Выделить всё
@Data
public class LoginDTO {
private String username;
private String password;
}
Код: Выделить всё
@PostMapping("/login")
public ResponseEntity authenticateUser(@RequestBody LoginDTO loginDTO) {
return userService.authenticateUser(loginDTO);
}
Код: Выделить всё
public ResponseEntity authenticateUser (LoginDTO loginDTO) {
try {
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(loginDTO.getUsername(), loginDTO.getPassword()));
SecurityContextHolder.getContext().setAuthentication(authentication);
return new ResponseEntity(" is successfully logged in.", HttpStatus.OK);
} catch (AuthenticationException ex) {
return new ResponseEntity("Authentication failed: " + ex.getMessage(), HttpStatus.UNAUTHORIZED);
}
}
Я знаю, что CSRF не следует отключать, но сейчас пока следующий шаг не будет работать правильно, я приказал Spring отключить его.
Код: Выделить всё
@Configuration
public class SecurityConfig {
@Bean
public static PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception {
return configuration.getAuthenticationManager();
}
@Bean
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
http
.securityContext((context) -> context.requireExplicitSave(false))
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.ALWAYS))
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests((requests) -> requests
.requestMatchers(
"/api/product/create",
"/api/user/logout"
).authenticated()
.requestMatchers(
"/api/user/register",
"/api/user/{id}",
"/api/category/**",
"/api/product/**",
"/api/user/login"
).permitAll());
return http.build();
}
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... entication