spring cloud gateway 跨域问题(srping cloud Hoxton.SR3版本)
来源:10-1 为什么要使用网关?

慕斯卡3003623
2020-04-17
升级到Hoxton.SR3+spring cloud alibaba 2.2.1之后,前后端分离出现前端访问网关跨域问题。
0,升级后
spring cloud Hoxton.SR3 + spring-cloud-alibaba 2.2.1.RELEASE + nacos1.2.1
在网上搜了各种办法都无法解决网关跨域问题,老师的配置属性方式也尝试了,还是有跨域问题。
前端项目传统的dom操作ui框架 layui,使用的是 ajax请求。
依赖版本升级也只修改了pom文件,代码并没有任何变化。
尝试过的spring cloud gateway 代码跨域配置
package com.wei.gatway.config;
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.reactive.config.EnableWebFlux;
import org.springframework.web.util.pattern.PathPatternParser;
/**
* @author wei_msi
*/
@Configuration
@EnableWebFlux
public class CorsConfig{
@Bean
public CorsWebFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
// config.addAllowedMethod("*");
// config.addAllowedOrigin("*");
// config.addAllowedHeader("*");
config.setAllowCredentials(true); // 允许cookies跨域
config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
config.addAllowedHeader("*");// #允许访问的头信息,*表示全部
config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
config.addAllowedMethod("OPTIONS");// 允许提交请求的方法类型,*表示全部允许
config.addAllowedMethod("HEAD");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");
org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource source =
new org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource(new PathPatternParser());
source.registerCorsConfiguration("/**", config);
return new CorsWebFilter(source);
}
}
配置属性方式
server:
port: 8888
##请求头据过大配置
max-http-header-size: 10000000
# servlet:
# context-path: /auth # 应用上下文 localhost:8000/auth
# 指定日志
logging:
#请自行修改logback-spring.xml文件中 自定义属性 开始 自行修改value 的值
config: classpath:logback-spring.xml
#spring 公共配置
spring:
application:
# 服务名称尽量用-,不要用_,不要用特殊字符
name: wei-gatway-server
#spring cloud环境配置
cloud:
#当存在相同的远程配置和本地配置时,远程配置优先级要高于本地配置文件。
#但可以通过添加如下配置调整,需要注意的是这段配置需要放在远程配置才会生效:
config:
# 是否允许本地配置覆盖远程配置,默认true
allow-override: true
# 远端不覆盖本地,默认false
override-none: true
# 远端是否覆盖系统配置,默认是覆盖,默认true
override-system-properties: false
nacos:
discovery:
# 指定nacos server的地址
server-addr: localhost:8848
# # 指定namespace 需要在nacos控制台创建,赋值控制台生成的命名空间ID
# # namespace: 56116141-d837-4d15-8842-94e153bb6cfb
# # 指定集群名称
cluster-name: XA
# #定义元数据
# metadata:
# version: v1
#spring Cloud gateway 网关信息配置
gateway:
routes:
- id: wei-oauth2-server_route
uri: lb://wei-oauth2-server
predicates:
- Path=/oauth/**
filters:
- StripPrefix=1
- id: wei-resource-smartaudit_route
uri: lb://wei-resource-smartaudit
predicates:
- Path=/smartaudit/**
filters:
- StripPrefix=1
- id: wei-fpb-server_route
uri: lb://wei-fpb-server
predicates:
- Path=/fpb/**
filters:
- StripPrefix=1
discovery:
locator:
# 让gateway通过服务发现组件找到其他的微服务
enabled: true
# 跨域
globalcors:
corsConfigurations:
'[/**]':
allowedHeaders: "*"
allowedOrigins: "*"
allowedMethods: "*"
#cloud alibaba sentinel流量防卫兵相关配置
# sentinel:
# filter:
# # 打开/关闭掉对Spring MVC端点的保护
# enabled: true
# transport:
# # 指定sentinel 控制台的地址
# dashboard: localhost:8181
#监控Actuator
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
1,没升级前的项目结构
spring cloud Hoxton.SR2 + spring-cloud-alibaba 2.2.0.RELEASE nacos1.1.4
网关没有任何跨域相关配置,只在springboot 微服务中用了全局配置CorsFilter进行跨域处理,
前端项目可直接通过网关地址进行后台数据交互,不存在网关跨域问题。
父工程pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wei</groupId>
<artifactId>wei-cloud-platform-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<description>微云平台父项目</description>
<!-- 声明子模块 -->
<modules>
<module>wei-common</module>
<module>wei-code-generator</module>
<module>wei-oauth2-server</module>
<module>wei-sso-client1</module>
<module>wei-sso-client2</module>
<module>wei-gatway-server</module>
<module>wei-resource-smartaudit</module>
<module>wei-monitor-server</module>
</modules>
<properties>
<maven.test.skip>true</maven.test.skip>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version>
<!-- 定义版本号, 子模块直接引用-->
<wei-cloud-platform.version>1.0-SNAPSHOT</wei-cloud-platform.version>
<spring.boot.version>2.2.2.RELEASE</spring.boot.version>
<spring-boot-admin.version>2.2.2</spring-boot-admin.version>
<spring-cloud.version>Hoxton.SR2</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.0.RELEASE</spring-cloud-alibaba.version>
<commons-io.version>2.6</commons-io.version>
<mybatis-plus-version>3.3.1</mybatis-plus-version>
<druid-version>1.1.21</druid-version>
<kaptcha.version>2.3.2</kaptcha.version>
<fastjson-version>1.2.62</fastjson-version>
<easypoi-version>4.1.3</easypoi-version>
<userAgentUtils-version>1.21</userAgentUtils-version>
<swagger2-knife4j-version>2.0.2</swagger2-knife4j-version>
<thumbnailator-version>0.4.8</thumbnailator-version>
<tika-core-version>1.22</tika-core-version>
<beetl-version>3.0.13.RELEASE</beetl-version>
<jpinyin-version>1.1.8</jpinyin-version>
<jsoup-version>1.12.1</jsoup-version>
<qlExpress-version>3.2.4</qlExpress-version>
<hanlp-version>portable-1.7.6</hanlp-version>
<hutool.verson>5.1.2</hutool.verson>
<dynamic.datasource.version>3.0.0</dynamic.datasource.version>
</properties>
<!--依赖声明 并没有实际引用-->
<dependencyManagement>
<dependencies>
<!--springBoot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring-boot-admin-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--整合spring cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--整合spring cloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- kaptcha 用于图形验证码 -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>${kaptcha.version}</version>
</dependency>
<!--alibaba.druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid-version}</version>
</dependency>
<!-- mybatis-plus动态数据源 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>${dynamic.datasource.version}</version>
</dependency>
<!-- mybatis-plus 和代码生成-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus-version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis-plus-version}</version>
</dependency>
<!--jsoup java爬虫解析html-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>${jsoup-version}</version>
</dependency>
<!-- 拼音转换工具-->
<dependency>
<groupId>com.github.stuxuhai</groupId>
<artifactId>jpinyin</artifactId>
<version>${jpinyin-version}</version>
</dependency>
<!-- QLExpress-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>QLExpress</artifactId>
<version>${qlExpress-version}</version>
</dependency>
<!--hanlp分词器-->
<dependency>
<groupId>com.hankcs</groupId>
<artifactId>hanlp</artifactId>
<version>${hanlp-version}</version>
</dependency>
<!-- google处理图片工具包 -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>${thumbnailator-version}</version>
</dependency>
<!-- 获取文件类型 -->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>${tika-core-version}</version>
</dependency>
<!-- beetl模板引擎,用于代码生成 -->
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>${beetl-version}</version>
</dependency>
<!-- 从request获取设备信息 -->
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>${userAgentUtils-version}</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson-version}</version>
</dependency>
<!-- swaggerui -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${swagger2-knife4j-version}</version>
</dependency>
<!-- easypoi-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>${easypoi-version}</version>
</dependency>
<!-- Apache Commons io工具的封装 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<!-- Hutool是一个小而全的Java工具类库 官方API https://www.hutool.cn/docs/ -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.verson}</version>
</dependency>
<!--微云平台公共模块-->
<dependency>
<groupId>com.wei</groupId>
<artifactId>wei-common</artifactId>
<version>${wei-cloud-platform.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 公共真实依赖 所有基于此父工程创建的项目都会自动依赖-->
<dependencies>
<!--Spring Boot Actuator提供一系列HTTP端点来暴露项目信息,用来监控和管理项目。 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- application.yml 配置处理器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- springboot 单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--热部署 idea ctrl+f9-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<!-- maven编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--springboot 打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 打包跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
网关pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>wei-cloud-platform-parent</artifactId>
<groupId>com.wei</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>wei-gatway-server</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<description>微云平台网关服务</description>
<dependencies>
<!--gateway网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- com-cloud-alibaba-nacos服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- com-cloud-alibaba-nacos配置中心 使用配置中心时添加此依赖 不使用不要添加-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-config</artifactId>
</dependency>
<!-- com-cloud-alibaba sentinel流量防卫兵-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- sentinel 网关限流 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!--springboot 打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--启动类-->
<configuration>
<mainClass>com.wei.WeiGatWayApplication</mainClass>
</configuration>
</plugin>
<!-- 打包跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
网关配置文件
server:
port: 8888
##请求头据过大配置
max-http-header-size: 10000000
# servlet:
# context-path: /auth # 应用上下文 localhost:8000/auth
# 指定日志
logging:
#请自行修改logback-spring.xml文件中 自定义属性 开始 自行修改value 的值
config: classpath:logback-spring.xml
#spring 公共配置
spring:
application:
# 服务名称尽量用-,不要用_,不要用特殊字符
name: wei-gatway-server
#spring cloud环境配置
cloud:
#当存在相同的远程配置和本地配置时,远程配置优先级要高于本地配置文件。
#但可以通过添加如下配置调整,需要注意的是这段配置需要放在远程配置才会生效:
config:
# 是否允许本地配置覆盖远程配置,默认true
allow-override: true
# 远端不覆盖本地,默认false
override-none: true
# 远端是否覆盖系统配置,默认是覆盖,默认true
override-system-properties: false
nacos:
discovery:
# 指定nacos server的地址
server-addr: localhost:8848
# # 指定namespace 需要在nacos控制台创建,赋值控制台生成的命名空间ID
# # namespace: 56116141-d837-4d15-8842-94e153bb6cfb
# # 指定集群名称
cluster-name: XA
# #定义元数据
# metadata:
# version: v1
#spring Cloud gateway 网关信息配置
gateway:
routes:
- id: wei-oauth2-server_route
uri: lb://wei-oauth2-server
predicates:
- Path=/oauth/**
filters:
- StripPrefix=1
- id: wei-resource-smartaudit_route
uri: lb://wei-resource-smartaudit
predicates:
- Path=/smartaudit/**
filters:
- StripPrefix=1
- id: wei-fpb-server_route
uri: lb://wei-fpb-server
predicates:
- Path=/fpb/**
filters:
- StripPrefix=1
discovery:
locator:
# 让gateway通过服务发现组件找到其他的微服务
enabled: true
#cloud alibaba sentinel流量防卫兵相关配置
# sentinel:
# filter:
# # 打开/关闭掉对Spring MVC端点的保护
# enabled: true
# transport:
# # 指定sentinel 控制台的地址
# dashboard: localhost:8181
#监控Actuator
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
网关项目结构
无任何跨域相关配置
微服务中的跨域配置
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 zhangweiwei-pro
*/
@Configuration
public class MyCorsConfiguration {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
/* 1允许任何域名使用*/
corsConfiguration.addAllowedOrigin("*");
/* 2允许任何头*/
corsConfiguration.addAllowedHeader("*");
/* 3允许任何方法(post、get等)*/
corsConfiguration.addAllowedMethod("*");
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
}
一个微服务的pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>wei-cloud-platform-parent</artifactId>
<groupId>com.wei</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>wei-oauth2-server</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<description>微云平台授权认证服务</description>
<dependencies>
<!--微云平台公共模块-->
<dependency>
<groupId>com.wei</groupId>
<artifactId>wei-common</artifactId>
</dependency>
<!--spring mvc相关的-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!-- mybatis-plus动态数据源 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- com-cloud-alibaba-nacos服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!--springboot 打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--启动类-->
<configuration>
<mainClass>com.wei.AuthServerApplication</mainClass>
</configuration>
</plugin>
<!-- 打包跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
写回答
1回答
-
慕斯卡3003623
提问者
2020-04-17
https://www.cnblogs.com/tusheng/p/12256184.html
问题已解决,是因为网关和微服务同时存在跨域配置,使用网关时只需要在网关配置跨域处理即可,微服务中不要跨域处理
012020-04-17
相似问题