Request blocked by CORS - Vue and Spring Boot

Asked

Viewed 497 times

0

I have a Spring Boot API and a Vue application, both running locally, but I can’t make a PUT request from the Vue application for API. If you send a GET request, it works perfectly. I tried, including creating a proxy as shown aui: Request with Axios and React blocked by CORS policy. But to no avail.

The error I get in the browser is as follows:

Erro vue

I’ve already enabled the annotation in my Source API and disabled Cors in the security configuration as well: Resource:

@RestController
@CrossOrigin
@RequestMapping(value = "/curso")
public class CursoResource {

    @Autowired
    CursoService cursoService;

    @PutMapping(value = "/{id}")
    public ResponseEntity<Void> update(@PathVariable Long id, @RequestBody Curso curso){
        curso.setId(id);
        cursoService.updateCurso(curso);
        return ResponseEntity.noContent().build();
    }

Security config:

@Override
    protected void configure(HttpSecurity http) throws Exception{
        http.cors().and().csrf().disable();
        http.authorizeRequests()
                .anyRequest().permitAll();
                //.antMatchers(HttpMethod.GET, PUBLIC_MATCHERS_GET).permitAll()
                //.antMatchers(HttpMethod.POST, ADMIN_MATCHERS_POST).hasAuthority("ADMIN");
                //.anyRequest().authenticated();
        http.addFilter(new JWTAuthenticationFilter(authenticationManager(), jwtUtil));
        http.addFilter(new JWTAuthorizationFilter(authenticationManager(), jwtUtil, userDetailsService));
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

I try to make the requisition with Axios:

axios.put('http://localhost:8082/curso/' + this.cursoid, this.curso).
            then((response) => {
                alert('Disciplina adicionada!')
            })

1 answer

0

Try performing this configuration class to adjust the CORS headers:

@Configuration
public class CorsConfig {
    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowedOrigins(Arrays.asList("*"));
        config.setAllowedMethods(Arrays.asList("*"));

        final UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);

        return configSource;
    }
}

Maybe it’s the only missing configuration, you can check by documentation

Browser other questions tagged

You are not signed in. Login or sign up in order to post.