互联网技术-alibaba-gateway网关中统一配置跨域请求

本文介绍了如何在Spring Cloud Gateway中配置全局CORS,包括YML文件设置详细参数和编写配置类的方法。重点讲解了如何处理跨域请求,如允许的域名、HTTP方法和头信息,并展示了两种实现方式。
摘要由CSDN通过智能技术生成

1.添加pom坐标

 

    <dependencies>
        <!-- 网关 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <!-- nacos 服务发现 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

    </dependencies>

方法一: yml文件

2.yml文件配置添加

spring:
  cloud:
    gateway: 
      globalcors:
        cors-configurations:
          '[/**]':
            allowedOrigins: "*"   # 允许那些网站的跨域请求
            allowedMethods: "*"   # 允许的跨域ajax的请求方式
            allowedHeaders: "*"   # 允许在请求中携带的头信息

2.1yml文件进行详细设置

spring:
  cloud:
    gateway:
      globalcors:
        add-to-simple-url-handler-mapping: true   #解决options请求被拦截
        cors-configurations:
          '[/**]':
            allowedOrigins: "http://localhost:8080"   # 允许那些网站的跨域请求
            allowedMethods:           # 允许的跨域ajax的请求方式
              - GET
              - POST
            allowedHeaders: "*"       # 允许在请求中携带的头信息
            allowCredentials: true	  # 是否允许携带Cookie

方法二: 书写配置类

package com.czxy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.cors.reactive.CorsUtils;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
 * @author 桐叔
 * @email liangtong@itcast.cn
 */
@Configuration
public class GlobalCorsConfig {

    @Bean
    public WebFilter corsFilter2() {
        return (ServerWebExchange ctx, WebFilterChain chain) -> {
            ServerHttpRequest request = ctx.getRequest();
            if (CorsUtils.isCorsRequest(request)) {
                HttpHeaders requestHeaders = request.getHeaders();
                ServerHttpResponse response = ctx.getResponse();
                HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod();
                HttpHeaders headers = response.getHeaders();
                headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin());
                headers.addAll(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS,
                        requestHeaders.getAccessControlRequestHeaders());
                if (requestMethod != null) {
                    headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name());
                }
                headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
                headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "*");
                if (request.getMethod() == HttpMethod.OPTIONS) {
                    response.setStatusCode(HttpStatus.OK);
                    return Mono.empty();
                }
            }
            return chain.filter(ctx);
        };
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值