当前位置: 首页 > news >正文

SpringCloud Gateway及 Springboot 服务 跨域配置

SpringCloud Gateway 跨域配置

配置文件

#跨域支持
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowed-headers[0]=*
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowed-methods[0]=*
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowed-origins[0]=*

配置组件

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;/*** @author zhe.xiao* @version 1.0* @since 2024/8/21 下午2:59*/
@Configuration
public class CorsConfig {@Beanpublic CorsWebFilter corsFilter() {CorsConfiguration config = new CorsConfiguration();config.addAllowedMethod("*");config.addAllowedOrigin("*");config.addAllowedHeader("*");config.addExposedHeader("*");UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());source.registerCorsConfiguration("/**", config);return new CorsWebFilter(source);}
}

Springboot 服务跨域配置

配置组件

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;/*** @author zhe.xiao* @version 1.0* @since 2024/8/21 下午3:19*/
@Configuration
public class CorsConfig {@Beanpublic CorsFilter corsFilter() {//1. 添加 CORS配置信息CorsConfiguration config = new CorsConfiguration();//放行哪些原始域config.addAllowedOrigin("*");//放行哪些请求方式config.addAllowedMethod("*");//放行哪些原始请求头部信息config.addAllowedHeader("*");//暴露哪些头部信息config.addExposedHeader("*");//2. 添加映射路径UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();corsConfigurationSource.registerCorsConfiguration("/**", config);//3. 返回新的CorsFilterreturn new CorsFilter(corsConfigurationSource);}
}

http://www.mrgr.cn/news/9577.html

相关文章:

  • 【Tesla FSD V12的前世今生】从模块化设计到端到端自动驾驶技术的跃迁
  • 使用 Vue I18n 进行 Vue.js 应用的国际化
  • Java超市收银系统(十、爬虫)
  • vue 精选评论词云 集成echarts-wordcloud TF-IDF算法
  • PDF文件切割,无大小限制
  • Linux文件目录系统
  • ES6 中的 Set 对象
  • 【Linux入门】shell基础篇——shell的构成及基础调试
  • Excel VBA 编程学习指南,1.1 什么是VBA及其应用场景
  • 【ORACLE】listagg() 函数
  • 鸿蒙HarmonyOS开发:如何灵活运用动画效果提升用户体验
  • 深入解析浏览器与Web服务器的通信机制:从URL输入到页面渲染的全过程
  • Spring Boot OAuth2.0应用
  • HCIA云计算实验-1-存储实验
  • Linux云计算 |【第二阶段】SECURITY-DAY4
  • 学习记录:js算法(十):每日温度
  • 小程序 蓝牙API使用全流程
  • 从零开始搭建 LVS 高可用集群 (单机)
  • PDPS软件 那智机器人 (丰田版)离线程序导出处理
  • 华为nova2下无需root安装Metasploit