重写WebMvcConfigurer#addCorsMappings()可以解决一部分跨域的问题,但是对于有些过滤器涉及到跨域,且拦截器位面较高的话,还是会出现一些跨域问题。

配置 CorsFilter 跨域过滤器,一劳永逸

CorsFilterRegistrationConfig

package site.yuyanjia.template.common.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.boot.web.servlet.FilterRegistrationBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.cors.CorsConfiguration;

import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import org.springframework.web.filter.CorsFilter;

import java.util.ArrayList;

import java.util.List;

/**

* 跨域过滤器

*

* @author seer

* @date 2018/6/29 10:09

*/

@Configuration

@ConfigurationProperties(prefix = CorsFilterRegistrationConfig.PREFIX)

public class CorsFilterRegistrationConfig {

public static final String PREFIX = "yuyanjia.filter.cors";

/**

* 允许跨域地址

* 允许所有:*

*/

private List allowedOriginList = new ArrayList<>();

/**

* 允许请求头信息

* 允许所有:*

*/

private List allowedHeaderList = new ArrayList<>();

/**

* 允许请求信息

* 允许所有:*

*/

private List allowedMethodList = new ArrayList<>();

/**

* 允许暴露信息

*/

private List exposedHeaderList = new ArrayList<>();

/**

* 允许证书

*/

private Boolean allowCredentials = true;

/**

* 缓存时间

*/

private Long maxAge = 3600L;

/**

* 过滤地址

*/

private String mapping = "";

/**

* 跨域过滤器

*

* @return

*/

@Bean

public FilterRegistrationBean CrosFilterRegistrationBean() {

CorsConfiguration corsConfiguration = new CorsConfiguration();

corsConfiguration.setAllowedOrigins(allowedOriginList);

corsConfiguration.setAllowedHeaders(allowedHeaderList);

corsConfiguration.setAllowedMethods(allowedMethodList);

corsConfiguration.setExposedHeaders(exposedHeaderList);

corsConfiguration.setMaxAge(maxAge);

corsConfiguration.setAllowCredentials(allowCredentials);

UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();

configurationSource.registerCorsConfiguration(mapping, corsConfiguration);

CorsFilter corsFilter = new CorsFilter(configurationSource);

FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(corsFilter);

filterRegistrationBean.setOrder(0);

return filterRegistrationBean;

}

public List getAllowedOriginList() {

return allowedOriginList;

}

public void setAllowedOriginList(List allowedOriginList) {

this.allowedOriginList = allowedOriginList;

}

public List getAllowedHeaderList() {

return allowedHeaderList;

}

public void setAllowedHeaderList(List allowedHeaderList) {

this.allowedHeaderList = allowedHeaderList;

}

public List getAllowedMethodList() {

return allowedMethodList;

}

public void setAllowedMethodList(List allowedMethodList) {

this.allowedMethodList = allowedMethodList;

}

public List getExposedHeaderList() {

return exposedHeaderList;

}

public void setExposedHeaderList(List exposedHeaderList) {

this.exposedHeaderList = exposedHeaderList;

}

public Boolean getAllowCredentials() {

return allowCredentials;

}

public void setAllowCredentials(Boolean allowCredentials) {

this.allowCredentials = allowCredentials;

}

public Long getMaxAge() {

return maxAge;

}

public void setMaxAge(Long maxAge) {

this.maxAge = maxAge;

}

public String getMapping() {

return mapping;

}

public void setMapping(String mapping) {

this.mapping = mapping;

}

}

application.yml

yuyanjia:

filter:

cors:

allowed-origin-list:

- '*'

allowed-header-list:

- '*'

allowed-method-list:

- POST

- GET

exposed-header-list:

- access-control-allow-headers

- access-control-allow-methods

- access-control-allow-origin

- access-control-max-age

- X-Frame-Options

mapping: /website/**

推荐一些关于HTTP请求的相关资料,有助于了解跨域请求

跨域失败 过滤器_Spring Boot 跨域过滤器(过滤器跨域问题)相关推荐

  1. cors跨域_Spring Boot 中通过 CORS 解决跨域问题

    (给ImportNew加星标,提高Java技能) 转自:江南一点雨 今天和小伙伴们来聊一聊通过CORS解决跨域问题. 同源策略 很多人对跨域有一种误解,以为这是前端的事,和后端没关系,其实不是这样的, ...

  2. gateway中的局部过滤器_Spring Cloud Gateway中的过滤器工厂:重试过滤器

    Spring Cloud Gateway基于Spring Boot 2,是Spring Cloud的全新项目,该项目提供了一个构建在Spring 生态之上的API网关.本文基于的Spring Clou ...

  3. 【笔记】跨域重定向中使用Ajax(XHR请求)导致跨域失败

    背景: 1.前端Web中有两个域名,a.com和b.com,其中a.com是访问主站(页面),b.com是数据提交接口的服务器(XHR请求) 2.a.com中用XHR调用b.com/cerate[没有 ...

  4. 解决前后端分离中文件传输跨域失败问题

    笔者在之前的开发中就遇到过这种情况:给与后端的文件下载接口,我在浏览器在postman上试过都是可以的,但在前端上就报这种错误: 看上面的介绍是说前后端跨域问题,可是后端代码中明明配置了放行所有请求, ...

  5. WCF跨域 这可能是由于试图以跨域方式访问服务而又没有正确的跨域策略,或策略不适用于 SOAP...

    尝试向 URI"http://localhost:8001/AccountService.svc"发出请求时出错.这可能是由于试图以跨域方式访问服务而又没有正确的跨域策略,或策略不 ...

  6. ldap服务器配置信息错误,客户域控启用了LDAP服务器签名要求导致配置域信息失败...

    [故障类型]:配置域信息失败 [关 键 词]:ldap,域策略 [适用版本]:FusionCloud Solution V100R006C00 [问题现象]: 在FusionAccess上添加客户的域 ...

  7. 将一个域下的cookie传到另一个域_单点登录那些事儿(二)同域下的单点登录

    单点登录(SSO,Single Sign On)对我们来说已经不陌生了,一次登录,处处登录.企业旗下都是自己的应用系统,所有的应用系统都使用同一个一级域名,通过不同的二级域名来区分应用系统.例如:百度 ...

  8. 升级域控制器:向现有域添加 Windows Server 2008 或 Windows Server 2008 R2 域控制器的 Microsoft 支持快速入门...

    1(共 1)对本文的评价是有帮助 - 评价此主题 更新时间: 2011年11月 应用到: Windows Server 2008, Windows Server 2008 R2 本主题介绍将域控制器升 ...

  9. 私域认知篇:私域流量到底是什么?私域流量现象,私域流量可以解决什么问题,如何开展私域流量?...

    最近"私域流量"火了! 尽管热度如此高,但很多人提起"私域流量"仍然一知半解,众说纷纭. "私域不就是品牌会员群么?用一个新概念包装一下就能出去忽悠甲 ...

最新文章

  1. linux change linux login shell
  2. 人才短缺是数据中心运营商面临的新问题
  3. keras从入门到放弃(五)独热编码和顺序编码
  4. MariaDB安装、卸载以及相关设置(Linux服务器Mysql部署)
  5. SAP License:我应该怎么学习SAP?
  6. 比特币区块链将分道扬镳、Libra苦难继续,2020区块链进入关键时期!
  7. 【直播预告】“CCF-NLP走进高校”系列之“走进郑州大学”
  8. JavaWeb项目作业 Market商品管理系统
  9. [Power--IC]电源管理IC-STNS01
  10. Android自定义View,仿QQ显示用户等级
  11. 数据库服务器如何备份详细教程!
  12. 让耳机有个思想准备 趣谈煲耳机方法
  13. 木瓜移动:黑五网一爆冷跨境电商如何破局?
  14. JAVA光头之路(一)--环境变量
  15. STM32CubeMx-SPI读写W25QXXX
  16. php自动适应屏幕大小,如何解决js获取屏幕大小并且让图片自适应的方法
  17. SDNU-多校训练赛2-[ABF]
  18. mendeley使用技巧
  19. 数据分析的前世今生_李孟_新浪博客
  20. 船舶电子电气工程专业出来的交响_船舶电子电气工程本科专业介绍

热门文章

  1. 让linux系统做网关,代理上网!!!
  2. LCD RGB 控制技术讲解 — 时钟篇(上)
  3. 【目录】 网络瑞士军刀-netcat的秘诀
  4. 序列化picklejson模块
  5. u大侠pe系统桌面计算机,详解各种PE启动的过程
  6. taro 小程序转h5之后报错_记录微信小程序转Taro中遇到的问题
  7. 阿里云飞天论文获国际架构顶会 ATC 2021最佳论文:全球仅三篇
  8. 阿里研究员:警惕软件复杂度困局
  9. 【图解机器学习】人人都能懂的算法原理
  10. 2019 Flink Forward 大会最全视频来了!(附PPT下载) | 5大专题不容错过