CORS blocks the requests

Asked

Viewed 93 times

1

Hello, I’m developing a web application with Vue JS in a discipline and another person is making a Rest api with Spring, but when I run the api locally and try to access some of the resources through Postman, it works normally, but when accessing the same resource through the web application CORS blocks and the request does not even reach the api. The same happens if the api is hosted in Heroku for example.

This annotation has already been added to all controllers inserir a descrição da imagem aqui

And in the VUE JS application

inserir a descrição da imagem aqui

The token I also store in localStorage and set as default header

inserir a descrição da imagem aqui

I’ve tried disabling and activating this extension and it’s the same inserir a descrição da imagem aqui

That is the mistake: inserir a descrição da imagem aqui

I don’t know what else to do :/

1 answer

0

Try this way, go to your configuration class, this class is annotated with @Configuration if you don’t have create one, and create the following Bean:

@Bean
public CorsFilter corsFilter() {
    final CorsConfiguration config = new CorsConfiguration();
    config.addAllowedHeader("*");
    config.addAllowedMethod("GET");
    config.addAllowedMethod("POST");
    config.addAllowedMethod("PUT");
    config.addAllowedMethod("OPTIONS");
    config.setMaxAge(3600L);

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

    return new CorsFilter(source);
}

Some settings change from version to spring version, so when asking always inform the versions you are using.

Browser other questions tagged

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