跨域失败 过滤器_Spring Boot 跨域过滤器(过滤器跨域问题)
重写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 跨域过滤器(过滤器跨域问题)相关推荐
- cors跨域_Spring Boot 中通过 CORS 解决跨域问题
(给ImportNew加星标,提高Java技能) 转自:江南一点雨 今天和小伙伴们来聊一聊通过CORS解决跨域问题. 同源策略 很多人对跨域有一种误解,以为这是前端的事,和后端没关系,其实不是这样的, ...
- gateway中的局部过滤器_Spring Cloud Gateway中的过滤器工厂:重试过滤器
Spring Cloud Gateway基于Spring Boot 2,是Spring Cloud的全新项目,该项目提供了一个构建在Spring 生态之上的API网关.本文基于的Spring Clou ...
- 【笔记】跨域重定向中使用Ajax(XHR请求)导致跨域失败
背景: 1.前端Web中有两个域名,a.com和b.com,其中a.com是访问主站(页面),b.com是数据提交接口的服务器(XHR请求) 2.a.com中用XHR调用b.com/cerate[没有 ...
- 解决前后端分离中文件传输跨域失败问题
笔者在之前的开发中就遇到过这种情况:给与后端的文件下载接口,我在浏览器在postman上试过都是可以的,但在前端上就报这种错误: 看上面的介绍是说前后端跨域问题,可是后端代码中明明配置了放行所有请求, ...
- WCF跨域 这可能是由于试图以跨域方式访问服务而又没有正确的跨域策略,或策略不适用于 SOAP...
尝试向 URI"http://localhost:8001/AccountService.svc"发出请求时出错.这可能是由于试图以跨域方式访问服务而又没有正确的跨域策略,或策略不 ...
- ldap服务器配置信息错误,客户域控启用了LDAP服务器签名要求导致配置域信息失败...
[故障类型]:配置域信息失败 [关 键 词]:ldap,域策略 [适用版本]:FusionCloud Solution V100R006C00 [问题现象]: 在FusionAccess上添加客户的域 ...
- 将一个域下的cookie传到另一个域_单点登录那些事儿(二)同域下的单点登录
单点登录(SSO,Single Sign On)对我们来说已经不陌生了,一次登录,处处登录.企业旗下都是自己的应用系统,所有的应用系统都使用同一个一级域名,通过不同的二级域名来区分应用系统.例如:百度 ...
- 升级域控制器:向现有域添加 Windows Server 2008 或 Windows Server 2008 R2 域控制器的 Microsoft 支持快速入门...
1(共 1)对本文的评价是有帮助 - 评价此主题 更新时间: 2011年11月 应用到: Windows Server 2008, Windows Server 2008 R2 本主题介绍将域控制器升 ...
- 私域认知篇:私域流量到底是什么?私域流量现象,私域流量可以解决什么问题,如何开展私域流量?...
最近"私域流量"火了! 尽管热度如此高,但很多人提起"私域流量"仍然一知半解,众说纷纭. "私域不就是品牌会员群么?用一个新概念包装一下就能出去忽悠甲 ...
最新文章
- linux change linux login shell
- 人才短缺是数据中心运营商面临的新问题
- keras从入门到放弃(五)独热编码和顺序编码
- MariaDB安装、卸载以及相关设置(Linux服务器Mysql部署)
- SAP License:我应该怎么学习SAP?
- 比特币区块链将分道扬镳、Libra苦难继续,2020区块链进入关键时期!
- 【直播预告】“CCF-NLP走进高校”系列之“走进郑州大学”
- JavaWeb项目作业 Market商品管理系统
- [Power--IC]电源管理IC-STNS01
- Android自定义View,仿QQ显示用户等级
- 数据库服务器如何备份详细教程!
- 让耳机有个思想准备 趣谈煲耳机方法
- 木瓜移动:黑五网一爆冷跨境电商如何破局?
- JAVA光头之路(一)--环境变量
- STM32CubeMx-SPI读写W25QXXX
- php自动适应屏幕大小,如何解决js获取屏幕大小并且让图片自适应的方法
- SDNU-多校训练赛2-[ABF]
- mendeley使用技巧
- 数据分析的前世今生_李孟_新浪博客
- 船舶电子电气工程专业出来的交响_船舶电子电气工程本科专业介绍