java web解决跨域问题,亲测有效!

@Configuration
public class BeanConfiguration {
    @Bean
     public CorsFilter corsFilter() {
                final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
                final CorsConfiguration corsConfiguration = new CorsConfiguration();
                corsConfiguration.setAllowCredentials(true);
                 corsConfiguration.addAllowedOrigin("*");
                 corsConfiguration.addAllowedHeader("*");
                 corsConfiguration.addAllowedMethod("*");
                 urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
                 return new CorsFilter(urlBasedCorsConfigurationSource);
             }

}

自定义配置类,加入该代码,即可解决!

上一篇:报错:跨域问题解决 No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.


下一篇:谷粒商城学习日记(25)——网关配置同一跨域