Zuul 跨域

JS访问会出现跨域问题的解决,

一、对单个接口,处理跨域,只需要在被调用的类或或方法增加注解 CoossOrigin

如下设置 allowCredenticals=true,表示运行Cookie跨域

二、对所有接口,处理跨域问题。

在Zuul里增加CorsFilter过滤器

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

/**
 * 跨域配置
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter(){
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        //支持Cookie跨域
        config.setAllowCredentials(true);
        //原始域名
        config.setAllowedOrigins(Arrays.asList("*")); // http://a.com http://b.com
        config.setAllowedHeaders(Arrays.asList("*"));
        config.setAllowedMethods(Arrays.asList("*"));
        config.setMaxAge(300l);
        source.registerCorsConfiguration("/**",config);
        return new CorsFilter(source);
    }
    

}

  

原文地址:https://www.cnblogs.com/linlf03/p/10392537.html