Springboot文件下载跨域问题解决方案

简介: Springboot文件下载跨域问题解决方案

错误:

The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.


刚才是很奇怪,明明后端代码做了全局跨域处理了,咋还出现跨域呢?

代码:

 try {
            response.reset();
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/x-msdownload");
            response.addHeader("Content-Disposition",
                    "attachment;filename=" + new String(entity.getRealName().getBytes("gb2312"), "ISO8859-1"));
            input = new BufferedInputStream(ossObject.getObjectContent());
            byte[] buffBytes = new byte[1024];
            outputStream = response.getOutputStream();
            int read = 0;
            while ((read = input.read(buffBytes)) != -1) {
                outputStream.write(buffBytes, 0, read);
            }
            outputStream.flush();
            // 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
            ossObject.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (input != null) {
                    input.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

解决:

原来错误代码是: response.reset(); //这句代码是罪魁祸首,他会清空响应的一些信息,包括全局的跨域配置。

所以解决办法是:

把 response.reset(); 注释掉

相关文章
|
2天前
|
人工智能 JSON 前端开发
Spring Boot解决跨域问题方法汇总
Spring Boot解决跨域问题方法汇总
|
2天前
|
Java Spring
快速解决Spring Boot跨域困扰:使用CORS实现无缝跨域支持
这是一个简单的配置示例,用于在Spring Boot应用程序中实现CORS支持。根据你的项目需求,你可能需要更详细的配置来限制允许的来源、方法和标头。
29 3
|
2天前
|
前端开发 JavaScript Java
SpringBoot解决跨域访问的问题
本文介绍了跨域访问的概念及其解决方案。同源策略规定浏览器限制不符合协议、Host和端口的请求,导致跨域访问被禁止。为解决此问题,文中提出了三种策略:1) 前端利用HTML标签的特性(如script、iframe)和JSONP、postMessage规避同源策略;2) 通过代理,如nginx或nodejs中间件,使得所有请求看似来自同一源;3) CORS(跨域资源共享),通过设置HTTP响应头允许特定跨域请求。在SpringBoot中,实现CORS有四种方式,包括使用CorsFilter、重写WebMvcConfigurer、CrossOrigin注解以及直接设置响应头。
|
2天前
|
前端开发 Java 应用服务中间件
Springboot解决跨域问题方案总结(包括Nginx,Gateway网关等)
Springboot解决跨域问题方案总结(包括Nginx,Gateway网关等)
|
2天前
|
Java
SpringBoot 配置解决跨域问题
SpringBoot 配置解决跨域问题
|
2天前
|
前端开发 应用服务中间件 nginx
Vue+SpringBoot+Axios的跨域问题
Vue+SpringBoot+Axios的跨域问题
21 0
|
2天前
|
缓存 Java
SpringBoot跨域代码两种方式
SpringBoot跨域代码两种方式
14 0
|
2天前
|
Java
SpringBoot解决跨域问题 几种方案
SpringBoot解决跨域问题 几种方案
17 0
|
Java
SpringBoot 2.7.0 处理跨域的问题
SpringBoot 2.7.0 处理跨域的问题
667 0
|
Java
SpringBoot 2.6.7 处理跨域的问题
SpringBoot 2.6.7 处理跨域的问题
255 0