Spring Cloud gateway 跨域问题

有两个问题,cloud的版本兼容匹配问题\ 集成网关后的跨域问题

springCloud版本兼容匹配问题

因为版本的问题使用了Hoxton.SR2 ,zuul使用的版本限制在了2.0~2.5之间,况且gateway是官方支持的网关模块,所以使用了gateway。

如果你们的模块之间的一直出现NoClassFound 之类的异常,多半怕是版本的问题,
SpringCloud版本非常多,而且版本兼容性很差,Netflix-client、gateway使用的和cloud的版本不一样的话会出来很多莫名其妙的错误,
建议单项目的话,在pom里声明

  <!-- 依 赖 管 理 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

然后其他地方需要使用那些模块直接引用,千万不要再声明版本了。

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot </groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

这样的话你的项目就会少很多齐齐乖乖的问题。
如果你的项目是多模块

Eureka,Gateway、Config每个都是一个项目的话那么在父项目pom中声明,当然父项母一定是POM项目,规定各个版本,其他模块直接引用就可以了。

关于 dependencyManagement 中 scope=import

即此项目继承自官方的依赖关系,如果装了maven helper插件,
可以查看此坐标的源,其中有很多依赖,每一个依赖都有对应的合适的版本
所以你在另外的子模块中需要哪个直接引用即可,切记不可以写版本。
因为maven的就近原则 会以你的版本为准使用抛弃官方的

springCloud多模块使用gateway网关后跨域问题的解决

多模块后集成网关后会有跨域问题,
如果你是每个模块配置跨域

package com.jinwhui.datashareweb.config.crosconfig;


import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName WebConfig
 * @Author Qin
 * @Description WebConfig
 * @Version 1.0
 */
@Configuration
public class WebConfig implements Filter {
    
    


    @Override
    public void init(FilterConfig filterConfig) {
    
    

    }

    @Override
    public void doFilter(ServletRequest req , ServletResponse res , FilterChain chain) throws IOException, ServletException {
    
    
        HttpServletResponse response = (HttpServletResponse) res;
        HttpServletRequest request = (HttpServletRequest) req;
        response.setHeader("Access-Control-Allow-Origin" , "*"); // 设置允许所有跨域访问
        response.setHeader("Access-Control-Allow-Methods" , "POST,GET,PUT,OPTIONS,DELETE");
        response.setHeader("Access-Control-Max-Age" , "3600");
        response.setHeader("Access-Control-Allow-Headers" , "Origin,X-Requested-With,Content-Type,Accept,Authorization,token");
        response.setHeader("Access-Control-Allow-Credentials" , "true");
        chain.doFilter(req , res);
    }
    @Override
    public void destroy() {
    
    
    }
}

当然可以直接通过访问 ,
假设有两个服务分别处两个模块
8081/hello
8082/hi
直接访问各个模块请求上面的样子,
通过网关访问下面这个样子
8080/service-a/hello
8080/service-b/hi

gateway的模块中配置好自动转发

spring:

  application:
    name: gateway

  instance:
    lease-expiration-duration-in-seconds: 90
    lease-renewal-interval-in-seconds: 60
    preferIpAddress: true
  cloud:
    loadbalancer:
      ribbon:
        enabled: false
    gateway:
      discovery:
        locator:
          lower-case-service-id: true
          enabled: true

关于这里有个大坑,lower-case-service-id 和enable的true一定要这样
变了颜色
而不是

在这里插入图片描述
当然也可能是莫名其妙的IDEA问题,true并没有颜色,现在复现不了了,我直接 让这两行错位才能复现,那次格式正确却并没有生效,而且也不报错,
切记不要直接复制,照着写,会有自动提示的。

多模块且有网关解决跨域问题(Get没问题Post有问题)的情况

一定要在gateway中配置

package com.jinshui.eurekagateway.crosconfig;

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;

/**
 * @ClassName WebConfig
 * @Author Qin
 * @Description WebConfig
 * @Version 1.0
 */
@Configuration
public class WebConfig {
    
    
    @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);
    }

}

并且在各个模块中去除掉跨域的配置部分,才能实现
8080/service-a/hello
8080/service-b/hi

Get和post的成功访问,
究其原因,Gateway已经实现了将请求和响应从各模块中包装返回给客户端,就咩有必要在各模块中再实现一层跨域配置了,网关和模块之间可以视为一体,且网关的作用也是如此,
关于网关的配置routes还有特别多的用法
详情可以查看这个

猜你喜欢

转载自blog.csdn.net/weixin_41086086/article/details/118382243
今日推荐