栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springcloud-gateway搭建、跨域、路由重写相关

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springcloud-gateway搭建、跨域、路由重写相关

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录
  • 前言
  • 一、gateway搭建
    • 1. pom
    • 2. yaml
      • 1 注册
      • 2 配置路由与路径重写
    • 3.主启动类
  • 二、网关统一配置跨域
    • 1. 问题分析:
    • 2.预检请求
    • 3.解决跨域有2种办法
      • 1 通过nginx反向代理服务器
      • 2 通过网关配置CorsWebFilter之后,**不要加**`@CrossOrigin`


前言

提示::

官方开发文档地址

https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/


提示:

一、gateway搭建 1. pom

    org.springframework.cloud
    spring-cloud-starter-gateway

2. yaml 1 注册

要使用gateway,先注入到服务注册中心中,以alibaba下的开源nacos为例

spring:
  application:
    name: gulimall-gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
2 配置路由与路径重写

再去配置gateway相关yaml,这里以配置路由(routes)和路径重写(RewritePath)

spring:
  application:
    name: gulimall-gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        #==========================以api开头的都是前端发请求==============
        - id: product_route #处理商品服务,精确路由
          uri: lb://gulimall-product
          predicates:
            - Path=/api/product/**
          filters:
            - RewritePath=/api/?(?.*), /${segment}
     #===========================模糊路由=============================
        - id: renren_fast
          uri: lb://renren-fast
          predicates:
            - Path=/api/**
          filters:
            - RewritePath=/api/?(?.*), /renren-fast/${segment}
server:
  port: 88
3.主启动类
@EnableDiscoveryClient
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class GulimallGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GulimallGatewayApplication.class, args);
    }

}

二、网关统一配置跨域 1. 问题分析:

浏览器不能执行其他网站的脚本,是由浏览器的同源策略造成的,是浏览器对javascript施加的安全限制
同源策略:是指协议,域名,端口要相同,任何一个不一样都会产生跨域问题

2.预检请求

发起方发送一个预检请求Request Method: OPTIONS给服务器,获知服务器是否允许该实际请求,可以以这个原理解决跨域问题

3.解决跨域有2种办法 1 通过nginx反向代理服务器

通过nginx代理服务器,暴露一个相同的端口给浏览器,解决了跨域问题,但是如果开发的项目庞大,且每次需要去部署服务器里对nginx做nginx.conf配置,不方便解耦。

2 通过网关配置CorsWebFilter之后,不要加@CrossOrigin

代码

@Configuration
public class GuliMallCorsConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter(){
        //配置跨域
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);  //允许携带cookie

        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(urlBasedCorsConfigurationSource);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/962470.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号