SpringBoot file download cross-domain problem

Let me talk about the scene first:

The project has a requirement, which is to realize one, and download multiple files into one .zip compressed file

 

The code is written, and the global cross-domain settings are also configured, as shown in the following code

@Configuration
public class SimpleCORSFilter {

    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        /*是否允许请求带有验证信息*/
        corsConfiguration.setAllowCredentials(true);
        /*允许访问的客户端域名*/
        corsConfiguration.addAllowedOrigin("*");
        /*允许服务端访问的客户端请求头*/
        corsConfiguration.addAllowedHeader("*");
        /*允许访问的方法名,GET POST等*/
        corsConfiguration.addAllowedMethod("*");
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }

}

image.png

 

It’s okay to run locally, I feel that everything is going well and I am very happy

image.png

 

Then publish to the company test server

 

 

Then the problem came. The front-end buddies came to me and said that the batch download interface I wrote was cross-domain.

 

I wonder, no, the project has already set up a global cross-domain configuration, why only this batch download interface is cross-domain

image.png

 

After looking for the reason for a long time, Du Niang also asked for a long time. Most of Du Niang’s answers were copied and copied, basically no nutrition.

In the end, there was really no way. He asked the company’s big cow. He told me whether it was the interface that I downloaded in batches and cleared the global cross-domain configuration.

Then, I took a closer look at the code, and finally found the ultimate cause of the problem

image.png

 

As shown below

image.png

The code shown above

response.reset(); //This code is the culprit, he will clear some of the response information, including the global cross-domain configuration

So the solution is: (choose one)

1. Comment out response.reset(); as shown in the following code

image.png

2. Response.reset(); No comment, and the configuration is as follows

image.png

Guess you like

Origin blog.csdn.net/qq_39999478/article/details/107157324