'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't...

Discussão em 'Angular' iniciado por Roxana, Fevereiro 13, 2025.

  1. Roxana

    Roxana Guest

    I have a project in java,angular with mysql database.
    I followed a tutorial to make the login page. I did exactly as there but it doesn't work for me. The login page is at /api/authenticate. At /api/user I have the list of users and a button to add a new user and a login button. This page is to see how it works, it has no use. the problem is that I get these 3 errors. enter image description here

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http
    .csrf(csrf -> csrf.disable()) // Dezactivează CSRF
    .authorizeRequests(authorizeRequests ->
    authorizeRequests
    .requestMatchers("/api/users", "/api/user/**","/api/authenticate").permitAll() // Permite accesul la aceste endpoint-uri
    .anyRequest().authenticated() // Orice altă cerere necesită autentificare
    )
    .formLogin(formLogin ->
    formLogin
    .loginPage("/api/authenticate")
    .loginProcessingUrl("/api/authenticate")
    .defaultSuccessUrl("/api/users", true)
    .permitAll()
    )
    .logout(logout ->
    logout
    .invalidateHttpSession(true)
    .clearAuthentication(true)
    .logoutRequestMatcher(new AntPathRequestMatcher("/api/logout"))
    .logoutSuccessUrl("/api/authenticate")
    .permitAll()
    );

    return http.build();
    }


    this is the sequence from java

    @RestController
    @RequestMapping("/api")
    @RequiredArgsConstructor
    @CrossOrigin(origins = "http://localhost:4200")
    public class UserController {.........

    @GetMapping("/authenticate")
    public String authenticate(Model model, User user) {
    model.addAttribute("USER", user); // corectare: adăugăm atributul în model
    return "login"; // returnăm numele view-ului (login.html)
    }


    I tried to solve it without the encrypted password, initially I thought that the encryption was not ok, but it seems not

    I want when I click on login to take me to /api/users or a new page, but at the moment I want to know how I can solve the 3 errors

    Continue reading...

Compartilhe esta Página