springboot跨域

口说无凭,直接上代码


@Configuration

publicclassCorsConfig {

    privateCorsConfiguration buildConfig() {

        CorsConfiguration corsConfiguration = newCorsConfiguration();

        corsConfiguration.addAllowedOrigin("*");

        corsConfiguration.addAllowedHeader("*");

        corsConfiguration.addAllowedMethod("*");

        returncorsConfiguration;

    }


    @Bean

    publicCorsFilter corsFilter() {

        UrlBasedCorsConfigurationSource source = newUrlBasedCorsConfigurationSource();

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

        returnnewCorsFilter(source);

    }

}

1-新建配置文件,添加Configuration注解

2-测试跨域请求通过,至此已完美解决ajax跨域问题,是不是很easy,小伙伴儿们快去试试把!!!

你可能感兴趣的:(springboot跨域)