启用CORS原点GraphQL [英] Enable CORS origin graphql

查看:32
本文介绍了启用CORS原点GraphQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行GraphQL和Spring Boot项目。该API使用raphiQL运行良好,但当尝试使用Apollo vueJS使用它时,会导致CORS原点错误。

我在实现GraphQLQueryResolver的ProductQuery类中使用@CrossOrigin注释,如下所示:

 @CrossOrigin(origins = "https://localhost:8081")
public List<Product> getProducts(){return this.productService.findAll(); } 

以下是前端项目显示的错误:

感谢您的帮助。

推荐答案

对我有效的是官方docs

中解释的解决方案

我的configurer bean版本如下所示:

@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(final CorsRegistry registry) {
            registry.addMapping("/graphql/**")
                    .allowedOrigins(CorsConfiguration.ALL)
                    .allowedHeaders(CorsConfiguration.ALL)
                    .allowedMethods(CorsConfiguration.ALL);
        }
    };
}

这篇关于启用CORS原点GraphQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆