Gateway服务网关,跨域问题

跨域问题:

浏览器禁止请求的发起者与服务端发生跨域ajax请求,请求被浏览器拦截的问题

解决方案:CORS,这个以前应该学习过,这里不再赘述了。

不知道的小伙伴可以查看跨域资源共享 CORS 详解 - 阮一峰的网络日志

解决跨域问题

方式一:在gateway服务的application.yml文件中,添加下面的配置:

spring:
  cloud:
    gateway:
      # 。。。
      globalcors: # 全局的跨域处理
        add-to-simple-url-handler-mapping: true # 解决options请求被拦截问题
        corsConfigurations:
          '[/**]': # 所有的请求都要走以下策略
            allowedOrigins: # 允许哪些网站的跨域请求 
              - "http://localhost"
            allowedMethods: # 允许的跨域ajax的请求方式
              - "GET"
              - "POST"
              - "DELETE"
              - "PUT"
              - "OPTIONS"
            allowedHeaders: "*" # 允许在请求中携带的头信息
            allowCredentials: true # 是否允许携带cookie
            maxAge: 360000 # 这次跨域检测的有效期(单位:秒)

 方式二:用代码方式解决跨域

package com.itheima.gateway.config;

import lombok.extern.slf4j.Slf4j;
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;

@Slf4j
@Configuration
public class WebSecurityConfig {
 @Bean
 public CorsWebFilter corsFilter() {
  log.info("cors跨域处理...");
  CorsConfiguration config = new CorsConfiguration();
  config.setAllowCredentials(true); // 是否允许携带cookie
  config.addAllowedOrigin("*"); // 可接受的域,是一个具体域名或者*(代表任意域名)
  config.addAllowedHeader("*"); // 允许携带的头
  config.addAllowedMethod("*"); // 允许访问的方式

  // 基于Url的跨域配置
  UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  // 针对所有的请求Url,采用上面的Config配置
  source.registerCorsConfiguration("/**", config);

  return new CorsWebFilter(source);
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Gateway网关中进行跨域配置的方法是通过在application.yml文件中进行配置。具体的配置内容如下所示: ```yaml server: port: 10011 spring: application: name: gateway cloud: nacos: server-addr: 127.0.0.1:8848 gateway: routes: - id: user-service uri: lb://userservice predicates: - Path=/user/** - id: admin-service uri: lb://adminservice predicates: - Path=/admin/** globalcors: add-to-simple-url-handler-mapping: true cors-configurations: '\[/**\]': allowedOriginPatterns: "*" allowedHeaders: "*" allowedMethods: - "GET" - "POST" - "DELETE" - "PUT" - "OPTIONS" allowCredentials: true maxAge: 3600 ``` 这段配置代码中,通过设置`globalcors`来实现跨域配置。`allowedOriginPatterns`表示允许跨域的来源,使用通配符`*`表示允许所有来源。`allowedHeaders`表示允许跨域的请求头,同样使用通配符`*`表示允许所有请求头。`allowedMethods`表示允许跨域的请求方法,包括GET、POST、DELETE、PUT和OPTIONS。`allowCredentials`表示是否允许发送Cookie等凭证信息。`maxAge`表示本次跨域检测的有效期,单位为秒。 需要注意的是,以上配置是针对整个网关的跨域配置,如果需要对某个具体的路由进行跨域配置,可以在对应的路由配置中添加`filters`来实现。 #### 引用[.reference_title] - *1* [Gateway网关配置跨域](https://blog.csdn.net/weixin_62166514/article/details/131287227)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringCloud笔记(五)——Gateway](https://blog.csdn.net/weixin_46508271/article/details/121754796)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值