How to set up CORS in Springboot?

Asked

Viewed 82 times

1

Recently I decided to try developing in java with Spring boot. I’m not very experienced in this framework. I tried to make a complete application, with back-end with this technology and front-end with Vue using Axios to perform requests.

I did the back-end using Postman, but I felt the need to make requests in the development of the front. I tried to perform CORS configuration using That one and That one tutorials for configuration, but regardless of the form, my requests are being blocked. I even set up as follows:

@Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("*")
                        .allowedMethods("*")
                        .allowedHeaders("*")
                        .allowCredentials(true);
            }
        };
    }

But even so my requests continue to be blocked by CORS.

My Axios is configured as follows:

export default axios.create({
    baseURL:"localhost:8081",
    withCredentials:true
})

My Back-end is running at door 8081, while my front is at door 8080. I literally don’t know what else to do.

  • https://stackoverflow.com/a/59938757/4319922. See if this @Crossorigin annotation helps.

  • If I’m not mistaken the browser blocks the allow-origin: * when there is the flag withCredentials. Basically you can not allow for everyone if there are credentials in the request. It can be this, check in the browser console the error that occurs

No answers

Browser other questions tagged

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