转载自:https://blog.csdn.net/qq_45925197/article/details/126708024
背景
-
下载文件压缩成zip包接口,postman自测没问题,但在和前端联调的时候前端就会出现如下跨域的报错:
-
而且与其他正常请求相比,响应头如下少了跨域的几个header文件:
-
同时,后台日志出现了java.io.IOException: UT010029: Stream is closed
解决
猜测全局的跨域配置在某一个地方失效了;经排查,最终发现代码中response#reset重置了response,造成前端无法下载并报跨域问题。
#下载工具类
response.reset(); //将这一行注释掉就解决了
response.setCharacterEncoding(CharsetConstant.UTF8);
response.setContentType("application/zip");
项目本身的跨域配置如下:
@Configuration
public class CorsConfig {
@Bean
public FilterRegistrationBean<CorsFilter> corsFilter() {
final CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowCredentials(true);
corsConfiguration.addAllowedOriginPattern("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfiguration);
FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return bean;
}
}
标签:Stream,java,跨域,corsConfiguration,FilterRegistrationBean,source,new,response
From: https://www.cnblogs.com/sudokill/p/16952880.html