SpringBoot后台处理全局跨域

今天发现Vue的全局配置不顶用,项目很急就用了Java配置全局跨域


@Configuration

public class CorsConfig {

    private CorsConfiguration buildConfig() {

        CorsConfiguration corsConfiguration = new CorsConfiguration();

        corsConfiguration.addAllowedOrigin("*"); // 允许域名

        corsConfiguration.addAllowedHeader("*"); // 允许头

        corsConfiguration.addAllowedMethod("*"); // 允许方法

        return corsConfiguration;

    }

    @Bean

    public CorsFilter corsFilter() {

        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();

        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", buildConfig());

        return new CorsFilter(urlBasedCorsConfigurationSource);

    }

}

你可能感兴趣的:(SpringBoot,Mysql,Java,spring,boot,java,后端)