spring boot 跨域

spring boot 跨域

package com.xxx.xxx.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;

/**
 * 全局跨域
 * @author xxx
 */
@Configuration
public class GatewayCorsConfig {

	@Bean
    public CorsFilter corsFilter() {
        //添加映射路径
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //添加CORS配置信息
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        //是否发送Cookie信息
        corsConfiguration.setAllowCredentials(true);
        //放行哪些原始域(头部信息)
        corsConfiguration.addAllowedHeader("*");
        //放行哪些原始域
        corsConfiguration.addAllowedOrigin("*");
        //放行哪些原始域(请求方式)
        corsConfiguration.addAllowedMethod("*");
        //注册跨域配置
        source.registerCorsConfiguration("/**", corsConfiguration);

        return new CorsFilter(source);
    }
}

注意:如果你的平台,在网关中统一处理跨域。在你的服务里就不需要再次添加这里的代码。

posted @ 2021-01-21 09:59  代码召唤师  阅读(57)  评论(0)    收藏  举报