zoukankan      html  css  js  c++  java
  • 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);
        }
        
    
    }
    

      

  • 相关阅读:
    equals标准写法
    抽象类的概述
    多态的弊端
    多态
    final关键字
    java 静态代码块 构造块 构造方法
    java 工具类
    逻辑运算符&&和&的区别 ||和|的区别
    react-route
    跨域
  • 原文地址:https://www.cnblogs.com/linlf03/p/10392537.html
Copyright © 2011-2022 走看看