前端VUE+springcloud+gateway怎样解决跨域问题?

11 篇文章 0 订阅

1、如果是springboot的项目,则可以直接在Controller类上添加一个注解,或者添加一个配置类允许跨域请求访问。配置类如下:


```java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.TimeoutCallableProcessingInterceptor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * created with IDEA.
 *
 * @Author:wqz
 * @Date:2020-01-06
 * @Description:Mult_AdPlatform
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").
                allowedOrigins("*").allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE").
                maxAge(3600).allowCredentials(true);
    }

    @Override
    public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
        configurer.setDefaultTimeout(5000);
        configurer.registerCallableInterceptors(timeoutInterceptor());
    }

    @Bean

    public TimeoutCallableProcessingInterceptor timeoutInterceptor() {
        return new TimeoutCallableProcessingInterceptor();
    }
}

2、如果是springcloud项目+gateway,页面报错不能访问请求,则需要进行如下配置,在gateway这个服务中新建一个CorsConfig类:

```java
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;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * created with IDEA.
 *
 * @Author:wqz
 * @Date:2020-01-09
 * @Description:Mult_AdPlatform
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsWebFilter(){
        CorsConfiguration config=new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedMethod("*");
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        //设置预检请求的缓存时间(秒),在这个时间段里,对于相同的跨域请求不会再预检了
        config.setMaxAge(18000L);
        UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**",config);
        return new CorsWebFilter(source);
    }
}

有些博主采用在配置文件application.yml中进行这样的设置:

spring:
     cloud:
          gateway:
               globalcors:
                 cors-configurations:
                   '[/**]':
                     allowedOrigins: "*"
                     allowedMethods: "*"
                     allowedHeader: "*"

但是经过实测,没有什么作用,因此目前只能采用添加配置类的方式来解决跨域到此跨域问题就完美解决了。


但是我像上面一番设置过后并没有完美解决,又报了个错,还是不能正常提交请求。

仔细看了下错误提示,说我有多个CORS头…突然反应过来,我的目的是所有的请求经过我的gateway然后分发到其余的微服务中,因此只需要解决8080到gateway的18760这个跨域请求,其余的18760到我的各个微服务不用在多余添加配置类或者注解,我在其中一个用户管理的微服务的UserController上面加了个@CrossOrign这个注解,还有上面1中解决springboot跨域问题的CorsConfig类也没有删除,因此在用户管理的这个微服务中删除@CrossOrign注解和CorsConfig类,然后再测试,完美解决问题。

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qzxl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值