Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法在spring引导数据rest中启用CORS_Java_Spring Mvc_Spring Boot_Cors_Spring Data Rest - Fatal编程技术网

Java 无法在spring引导数据rest中启用CORS

Java 无法在spring引导数据rest中启用CORS,java,spring-mvc,spring-boot,cors,spring-data-rest,Java,Spring Mvc,Spring Boot,Cors,Spring Data Rest,我需要在spring boot data rest api中启用全局CORS,以防止在从浏览器调用api时出现以下错误: . 请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“” 我可以在浏览器中键入url并接收该资源的正确get响应,但我无法在网页中通过ajax调用进行相同的调用 你知道我错过了什么吗 我的代码和配置如下: @SpringBootApplication @EnableAutoConfiguration @EnableJpaRe

我需要在spring boot data rest api中启用全局CORS,以防止在从浏览器调用api时出现以下错误: . 请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”

我可以在浏览器中键入url并接收该资源的正确get响应,但我无法在网页中通过ajax调用进行相同的调用

你知道我错过了什么吗

我的代码和配置如下:

@SpringBootApplication
@EnableAutoConfiguration
@EnableJpaRepositories
@EnableWebMvc
public class Application  {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);

}

@Bean
public WebMvcConfigurer corsConfigurer() {

    return new WebMvcConfigurerAdapter() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**");
        }
    };
}

@Bean
public CommandLineRunner demo(UserRepository userRepository, PostRepository postRepository,
                              CommentRepository commentRepository, EventRepository eventRepository, VenueRepository venueRepository) {
    return (args) -> {

        User user = new User("fsdsdfsd","sdsdsds","121212");
        userRepository.save(user);

        Venue venue = new Venue("dsaesrdfddgd","aerttyhyyty","yyyyyyyyyyyyyy");
        venueRepository.save(venue);

        Event event = new Event("dsaesrdfddgd","aerttyhyyty","yyyyyyyyyyyyyy",venue,user);
        eventRepository.save(event);

        Post post = new Post("some posts are funny. Some are not.",user, event);
        postRepository.save(post);

        Comment comment = new Comment("commmentntnrnejfnerfdgdfgdfdt", user, post);
        commentRepository.save(comment);
    };
}
}


尝试在全局配置中添加以下内容:


registry.addMapping(“/**”).AllowedOriginates(“*”)

因此,在我发现的评论中进行讨论后,您可以在您的addCorsMappings方法中尝试这一点

registry.addMapping("/**").allowedOrigins("http://localhost:8090")
                          .allowedMethods("PUT", "DELETE", "GET", "POST");

嗯,添加这个bean有帮助

@Bean
public FilterRegistrationBean corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("GET");
    source.registerCorsConfiguration("/**", config);
    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(0);
    return bean;
}
有趣的是,spring不允许使用以下bean在spring引导数据rest端点上支持CORS。但是,它与开发人员创建的其他自定义端点一样工作

@Bean
public WebMvcConfigurer corsConfigurer() {

    return new WebMvcConfigurerAdapter() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET");
        }
    };
}

这似乎很有希望,所以我在WebMVCConfigurerbean声明中修改了这一行。不幸的是,这并没有什么不同。奇怪的是,对于常规rest端点来说,这是完美的。我认为您不需要带有全局配置的@crossorigin注释。可能对rest存储库有限制,请尝试使用cors过滤器。Tomcat附带了一个或实现您自己的非常基本的一个,它填充访问控制头到*嗨,请检查这个项目,它完全是使用SpringMVC4和SpringBoot制作的。在您的帖子中,您缺少为响应标题设置过滤器的功能,还必须允许GET、post等原始方法。。。从应用程序中使用。通过项目,如果不理解我会在一个完整的答案描述。非常感谢。我试试这个你明白了吗,或者你想让我发布详细的答案,但是你也需要发布你的项目结构,因为它需要改变。啊,好吧,说实话,我很挣扎,尽管这篇文章看起来真的有我想要的。如果你能帮我提供一个更详细的答案,我将不胜感激。至于项目结构……我目前把所有东西都放在一个名为hello的包中——这不是一件好事,但我的项目已经缩小了规模,所以我可以先让简单的东西工作起来——所以现在可以确定的是,如果你在上帝的课堂上工作,事情就不会是模块化的。按照他们的指导设置您的项目。!不幸的是,这没有帮助,但是谢谢你的建议。我发现很难相信Pivotal Spring的人员会费心创建Spring数据rest,如果它不能用于跨源请求。一定有办法做到这一点这对你很有帮助谢谢。有一部分看起来很有希望——当然是关于目前不支持本机cors的spring boot项目的替代基于过滤器的方法(例如…data rest lol)!我得办点事,但回来后我会试试的。我会告诉你事情的进展。谢谢对非常感谢。这基本上是通过使用基于过滤器的方法实现的。在本小节“基于过滤器的CORS支持”中,我尝试了两种几乎相同的方法,但这里的小差异解决了它!今天的英雄!!:)
@Bean
public FilterRegistrationBean corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("GET");
    source.registerCorsConfiguration("/**", config);
    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(0);
    return bean;
}
@Bean
public WebMvcConfigurer corsConfigurer() {

    return new WebMvcConfigurerAdapter() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET");
        }
    };
}