CORS跨域处理

CORS:Cross-Origin Resource Sharing

  • CORS是一种允许当前域(domain)的资源(比如html/js/web service)被其他域(domain)的脚本请求访问的机制,通常由于同域安全策略(the same-origin security policy)浏览器会禁止这种跨域请求。

处理方法

  • 后台设置允许的请求源/请求头等信息

后台配置

CorsFilter Bean配置

使用 Spring 提供的 CorsFilter 过滤器实现跨域配置

  • io.geekidea.springbootplus.core.config.SpringBootPlusCorsConfig
/**
* CORS跨域设置
*
* @return
*/
@Bean
public FilterRegistrationBean corsFilter(SpringBootPlusCorsProperties corsProperties) {
log.debug("corsProperties:{}", corsProperties);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
// 跨域配置
corsConfiguration.setAllowedOrigins(corsProperties.getAllowedOrigins());
corsConfiguration.setAllowedHeaders(corsProperties.getAllowedHeaders());
corsConfiguration.setAllowedMethods(corsProperties.getAllowedMethods());
corsConfiguration.setAllowCredentials(corsProperties.isAllowCredentials());
corsConfiguration.setExposedHeaders(corsProperties.getExposedHeaders());
corsConfiguration.setMaxAge(corsConfiguration.getMaxAge()); source.registerCorsConfiguration(corsProperties.getPath(), corsConfiguration);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
bean.setEnabled(corsProperties.isEnable());
return bean;
}

配置文件

配置文件类:io.geekidea.springbootplus.core.properties.SpringBootPlusCorsProperties

  • application.yml
spring-boot-plus:
############################ CORS start ############################
# CORS跨域配置,默认允许跨域
cors:
# 是否启用跨域,默认启用
enable: true
# CORS过滤的路径,默认:/**
path: /**
# 允许访问的源
allowed-origins: '*'
# 允许访问的请求头
allowed-headers: x-requested-with,content-type,token
# 是否允许发送cookie
allow-credentials: true
# 允许访问的请求方式
allowed-methods: OPTION,GET,POST
# 允许响应的头
exposed-headers: token
# 该响应的有效时间默认为30分钟,在有效时间内,浏览器无须为同一请求再次发起预检请求
max-age: 1800
############################ CORS end ##############################

参考

GITHUB:https://github.com/geekidea/spring-boot-plus

spring-boot-plus CORS跨域处理的更多相关文章

  1. spring boot:解决cors跨域问题的两种方法(spring boot 2.3.2)

    一,什么是CORS? 1,CORS(跨域资源共享)(CORS,Cross-origin resource sharing), 它是一个 W3C 标准中浏览器技术的规范, 它允许浏览器向非同一个域的服务 ...

  2. Spring MVC学习总结(10)——Spring MVC使用Cors跨域

    跨站 HTTP 请求(Cross-site HTTP request)是指发起请求的资源所在域不同于该请求所指向资源所在的域的 HTTP 请求.比如说,域名A(http://domaina.examp ...

  3. Java Spring boot 2.0 跨域问题

    跨域 一个资源会发起一个跨域HTTP请求(Cross-site HTTP request), 当它请求的一个资源是从一个与它本身提供的第一个资源的不同的域名时 . 比如说,域名A(http://dom ...

  4. spring boot项目配置跨域

    1.在项目启动入口类实现 WebMvcConfigurer 接口: @SpringBootApplication public class Application implements WebMvcC ...

  5. spring boot之配置跨域

    在启动类中配置 @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override p ...

  6. 关于 Spring Security OAuth2 中 CORS 跨域问题

    CORS 是一个 W3C 标准,全称是”跨域资源共享”(Cross-origin resource sharing).它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从而克服了 AJA ...

  7. spring boot / cloud (六) 开启CORS跨域访问

    spring boot / cloud (六) 开启CORS跨域访问 前言 什么是CORS? Cross-origin resource sharing(跨域资源共享),是一个W3C标准,它允许你向一 ...

  8. Spring Boot Web应用开发 CORS 跨域请求支持:

    Spring Boot Web应用开发 CORS 跨域请求支持: 一.Web开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS等等CORS与JSONP相比 1. JSONP只能实现 ...

  9. Spring Boot 2中对于CORS跨域访问的快速支持

    原文:https://www.jianshu.com/p/840b4f83c3b5 目前的程序开发,大部分都采用前后台分离.这样一来,就都会碰到跨域资源共享CORS的问题.Spring Boot 2 ...

  10. spring MVC cors跨域实现源码解析

    # spring MVC cors跨域实现源码解析 > 名词解释:跨域资源共享(Cross-Origin Resource Sharing) 简单说就是只要协议.IP.http方法任意一个不同就 ...

随机推荐

  1. JAVA基础知识|内部类

    一.什么是内部类? 内部类(inner class)是定义在另一个类中的类 为什么使用内部类? 1)内部类方法可以访问该类定义所在的作用域中的数据,包括私有数据 2)内部类可以对同一个包中的其他类隐藏 ...

  2. 使用 docker 部署 typecho 的 nginx 配置文件

    savokiss.com.conf server { listen ssl http2 reuseport; server_name savokiss.com www.savokiss.com; ro ...

  3. mysql 误删除所有用户或者忘记root密码

    /etc/init.d/mysqld stop //停止数据库/etc/init.d/mysqld restart //启动数据库(1)开启特殊启动模式mysqld_safe --skip-grant ...

  4. haproxy配置文件实例

    [root@kube-node1 ~]# cat /etc/haproxy/haproxy.cfg global log /dev/log local0 log /dev/log local1 not ...

  5. Vue学习笔记(四)一起进阶吧

    参考链接地址:https://segmentfault.com/a/1190000009188689?from=timeline Vuex框架原理与源码分析: http://tech.meituan. ...

  6. SparkMLLib的简单学习

    一. 简介 1. 机器学习中,可以将数据划分为连续数据和离散数据 a. 连续数据:可以取任何值,如房价 b. 离散数据:仅有少量特殊值,如一个房屋有2个或3个房间,但不能为2.75个房间 二. 创建向 ...

  7. Jmeter 逻辑控制器 之 Include Controller

    一.认识 Include Controller Include Controller :译为包含控制器,用来添加 Test Fragment(测试片段).具体是什么意思呢,我们先来了解下 Test F ...

  8. 攻防世界MISC新手练习

    0x01 this_is_flag 对!!!这就是flag 0x02 ext3 题目提示是Linux光盘,附件下载下来 在linux中挂载mount linux /mnt 找一下flagtrings ...

  9. Windows下的3389端口渗透

    1.Win7.Win2003.XP系统 在CMD命令行开启3389端口:REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" &qu ...

  10. windows下安装配置winpcap

    winpcap官网:http://www.winpcap.org/ 1.首先下载安装winpcap.exe,http://www.winpcap.org/install/default.htm 目的是 ...