The Java spring rest API CORS is not applicable to delete requests through jQuery and chrome

I am creating a web front end written in jQuery, which uses the spring framework to send rest requests to rest Web Services written in Java I encountered a strange error in the CORS request In particular, if I use Safari, everything is normal; On the contrary, with chrome, all delete requests fail (get and post requests also work in chrome)

My mistake is:

Response to preflight request doesn't pass access control check: No 
'Access-Control-Allow-Origin' header is present on the requested 
resource. Origin 'null' is therefore not allowed access. 
The response had HTTP status code 403.

This happens when chrome makes the first options request

My code for CORS is:

@Configuration
@EnableWebMvc
public class CorsConfigurator extends WebMvcConfigurerAdapter {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET","POST","DELETE","PUT","OPTIONS");
    }
}

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class,args);
    }
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new CorsConfigurator();
    }

}

I also tried to add the @ crossorigin annotation to my @ restcontroller, but nothing changed Does anyone know to solve this problem?

Thank you, Marco

Solution

I just added @ crossorigin to the method in the controller, which solved my problem

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>