springboot解决自定义请求头跨域问题(CORS error)

硅谷探秘者 算法基础,linux 4275 0 0

问题描述

springboot项目 在跨域名调用接口,并且需要传自定义的请求头时报错:

Access to XMLHttpRequest at 'http://ydatestapi.libawall.com/admin/equipment//notSelectPageResultByQuery?pageSize=10&pageIndex=1&isPagination=true&searchNumber=' from origin 'http://localhost:8080' has been blocked by CORS policy: Request header field x-access-token is not allowed by Access-Control-Allow-Headers in preflight response.

解决办法

添加统一配置response允许请求头

例如需要传 X-Access-Token 请求头,添加如下配置:

import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Component
public class AddResponseHeaderFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                    FilterChain filterChain) throws ServletException, IOException {
        httpServletResponse.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Access-Token");
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }
}

评论区
请写下您的评论...
暂无评论...
猜你喜欢
框架 5244 springmvc项目中,如一个项目的页面调用另一个项目的接口会产生403。对于一个接口而言很好,springmvc中只需要在接口上加一个注。@CrossOrigin
weblog 7738 执行一个脚本时会检查访的资源是否同源,如果非同源,那么在数据时,浏览器会在控制台中报一个异常,提示拒绝访。那么什么是呢?,指的是从一个名去另外一个名的资源。即时,
java基础 2104 Java异常体系java异常体系继承图ThrowableThrowable类是所有异常的父类,常见的子类有两个Error、Exception。ErrorError类就是程序运行时候抛出的最严重级别的
框架 2284 很多项目在开发的时候都会遇到分页的,一般分页主要用到两个参数page和limit,page是第几页,limit是多少条数据。而一般在数据库的时候我们用不到page参数,用到的是用page
框架 3861 springbootjson数据不返回对象的指字段在实体类的字段上加上注importcom.fasterxml.jackson.annotation.JsonIgnore;例
框架 1240 一、logback-spring.xml配置文件二、application.yml配置三、onMatch和onMismatch属性说明一、logback-spring.xml配置文件
框架 2614 日志log4j.properties配置1.完整示例:##屏蔽com.mchange.v2包下面的debug,info,warn信息##log4j.logger.xxx=error意为屏蔽xxx包下
框架 1692 参数呢?首先我们有一个基础的配置类BasePage,这个类里面只有page,limit,index这三个属性,我们在创建其他查询参数封装类的时候要继承BasePage这个类。然后用我们己实现的参数
归档
2018-11  12 2018-12  33 2019-01  28 2019-02  28 2019-03  32 2019-04  27 2019-05  33 2019-06  6 2019-07  12 2019-08  12 2019-09  21 2019-10  8 2019-11  15 2019-12  25 2020-01  9 2020-02  5 2020-03  16 2020-04  4 2020-06  1 2020-07  7 2020-08  13 2020-09  9 2020-10  5 2020-12  3 2021-01  1 2021-02  5 2021-03  7 2021-04  4 2021-05  4 2021-06  1 2021-07  7 2021-08  2 2021-09  8 2021-10  9 2021-11  16 2021-12  14 2022-01  7 2022-05  1 2022-08  3 2022-09  2 2022-10  2 2022-12  5 2023-01  3 2023-02  1 2023-03  4 2023-04  2 2023-06  3 2023-07  4 2023-08  1 2023-10  1 2024-02  1 2024-03  1
标签
算法基础 linux 前端 c++ 数据结构 框架 数据库 计算机基础 储备知识 java基础 ASM 其他 深入理解java虚拟机 nginx git 消息中间件 搜索 maven redis docker dubbo vue 导入导出 软件使用 idea插件 协议 无聊的知识 jenkins springboot mqtt协议 keepalived minio mysql ensp 网络基础 xxl-job rabbitmq haproxy srs 音视频 webrtc javascript
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。