无法在弹簧启动数据休息时启用CORS

无法在弹簧启动数据休息时启用CORS

问题描述:

我需要在我的弹簧启动数据休息API中启用全局CORS,以防止在我从浏览器调用我的api时出现以下错误: http://localhost:8090/posts?user-id=1。请求的资源上没有“Access-Control-Allow-Origin”标题。因此不允许访问'Origin'http://localhost'。'。无法在弹簧启动数据休息时启用CORS

我可以在浏览器中输入url并获得该资源的正确获取响应,但我无法通过网页中的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); 
    }; 
} 

}

@RepositoryRestResource 
public interface PostRepository extends PagingAndSortingRepository<Post, Long> { 


Page<Post> readBydeletedIsFalseOrderByCreated(Pageable pageRequest); 

@CrossOrigin 
Post readByIdAndDeletedIsFalse(Long postId); 

}

buildscript { 
repositories { 
    mavenCentral() 
} 
dependencies { 
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE") 
} 
} 

apply plugin: 'java' 
apply plugin: 'war' 
apply plugin: 'eclipse' 
apply plugin: 'idea' 
apply plugin: 'spring-boot' 

jar { 
    baseName = 'project' 
    version = '0.1.0' 
} 

war { 
    baseName = 'project' 
    version = '0.1.0' 
} 


repositories { 
    mavenCentral() 
} 

sourceCompatibility = 1.8 
targetCompatibility = 1.8 

dependencies { 
    compile("org.springframework.boot:spring-boot-starter-data-rest") 
    compile("org.springframework.boot:spring-boot-starter-data-jpa") 
    compile("com.h2database:h2") 
} 

task wrapper(type: Wrapper) { 
    gradleVersion = '2.3' 
} 
+0

您好,请检查这个项目https://github.com/MFaisalHyder/REST_API 它使用Spring MVC4与Spring启动完全由。在您的文章中,您缺少为响应标题设置过滤器,并且必须允许从应用程序使用GET,POST等原始方法。如果不了解,我会通过项目来完成一个完整的答案。 –

+0

非常感谢。生病尝试这个 – gezinspace

+0

你有没有得到它,或者你想我发表详细的答案,但你也需要发布你的项目结构,因为它需要改变。 –

在评论中我发现了什么,你可以在你addCorsMappings试试这个方法经过讨论所以

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

,不幸的是这没有帮助,但是谢谢你的建议。我发现很难相信如果Pivotal Spring不能用于交叉源请求,那么Pivotal Spring的人会为创建spring-data-rest而烦恼。必须有这样的做法 – gezinspace

+0

https://spring.io/blog/2015/06/08/cors-support-in-spring-framework这将帮助你 –

+0

谢谢。有一个看起来很有希望的部分 - 肯定是关于弹簧启动项目的另一种基于过滤器的方法,目前不支持本地cors(例如... data-rest lol)!我必须执行一项差事,但是当我回来时,我生病了。生病让你知道它是怎么回事。谢谢! – gezinspace

尝试在全局配置添加此:

registry.addMapping("/**").allowedOrigins("*");

+0

这似乎很有希望,所以我在我的WebMvcConfigurer bean声明中修改了这一行。不幸的是,这并没有什么不同。 – gezinspace

+0

奇怪的是,这对于常规休息终点来说是完美无瑕的。我不认为你需要@crossorigin注释与全局配置 – Ulises

+0

奇怪。我已经尝试过没有注释了。 – gezinspace

好加入这个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; 
} 

滑稽足够春天不允许CORS支持使用以下bean的spring引导数据休息端点。但是,它可以像开发人员创建的其他自定义端点那样正常工作。

@Bean 
public WebMvcConfigurer corsConfigurer() { 

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