Spring Cloud Gateway解决跨域问题

1、什么是跨域

  跨域,指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器施加的安全限制。同源是指,域名,协议,端口均相同。

也就是说域名、协议、端口只要有一处不同,就会产生跨域问题。

以下情况都属于跨域

跨域原因说明实例
域名不同www.jd.com 与 www.taobao.com
域名相同,端口不同www.jd.com:8080 与 www.jd.com:8081
二级域名不同item.jd.com 与 miaosha.jd.com

如果域名和端口都相同,但是请求路径不同,不属于跨域,如:www.jd.com/item与www.jd.com/goods

http和https也属于跨域

2、为什么会有跨域问题

  跨域不一定都会有跨域问题。因为跨域问题是浏览器对于ajax请求的一种安全限制:一个页面发起的ajax请求,只能是与当前页域名相同的路径,这能有效的阻止跨站攻击。

  因此:跨域问题 是针对ajax的一种限制。但是这却给我们的开发带来了不便,而且在实际生产环境中,肯定会有很多台服务器之间交互,地址和端口都可能不同,怎么办?

3、Spring Cloud Gateway解决跨域问题

  在微服务项目中,项目被拆分成多个微服务,如果项目太大的话,那对应的微服务也会比较多,如果在每个微服务中分别配置跨域有点麻烦,我们可以在网关中统一配置,比较方便开发。

下面将我的网关微服务模块配置放出来仅供参考

3.1 搭建server-gateway模块

我搭建好的项目结构,由于是微服务项目,模块比较多。

image-20220104164515465

3.2 引入相关依赖

  这里引入nacos和gateway的相关依赖,因为我们需要将网关服务也注册到nacos注册中心里面。

<dependencies>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>

    <!-- 服务注册 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
</dependencies>

3.3 resources下添加配置文件

application.properties

# 服务端口
server.port=80
# 服务名
spring.application.name=service-gateway

# nacos服务地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

#使用服务发现路由
spring.cloud.gateway.discovery.locator.enabled=true

#设置路由id
spring.cloud.gateway.routes[0].id=service-hosp
#设置路由的uri
spring.cloud.gateway.routes[0].uri=lb://service-hosp
#设置路由断言,代理servicerId为auth-service的/auth/路径
spring.cloud.gateway.routes[0].predicates= Path=/*/hosp/**

#设置路由id
spring.cloud.gateway.routes[1].id=service-cmn
#设置路由的uri
spring.cloud.gateway.routes[1].uri=lb://service-cmn
#设置路由断言,代理servicerId为auth-service的/auth/路径
spring.cloud.gateway.routes[1].predicates= Path=/*/cmn/**

这里我只配置了两个微服务,用于测试

3.4 启动类

开启服务注册发现功能的注解@EnableDiscoveryClient可以省略,最好还是加上

@SpringBootApplication
@EnableDiscoveryClient
public class ServerGatewayApplication {
public static void main(String[] args) {
      SpringApplication.run(ServerGatewayApplication.class, args);
   }
}

3.5 解决跨域问题

添加全局配置类CorsConfig

@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedMethod("*");
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);

        return new CorsWebFilter(source);
    }
}

3.6 服务调整

  目前我们已经在网关做了跨域处理,那么service服务就不需要再做跨域处理了,将之前在controller类上添加过@CrossOrigin标签的去掉,防止程序异常、

3.7 测试

启动各个微服务,查看下nacos是否注册成功

image-20220104165314136

访问个前端路由试试,发现这次没有报跨域错误了,网关配置跨域成功。

image-20220104165354510

posted @   别团等shy哥发育  阅读(328)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示