spring允许cors跨域配置

创建配置类,要求在spring扫描包路径下

@Configuration
public class CorsConfig {

    /**
    * 跨域支持
    */
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowCredentials(true)
                        .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                        .allowedOrigins("*");
            }
        };
    }
}

浏览器在不同的域名下打开控制台

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://请求url');
xhr.send(null);
xhr.onload = function(e) {
    var xhr = e.target;
    console.log(xhr.responseText);
}