java - 为什么我的 Spring Controller 不处理 GraphiQL 发送的 OPTIONS 请求?

标签 java spring xss graphql graphql-java

我正在尝试制作 GraphQL Java server使用 GraphiQL 服务器。

使用本地运行的 GraphiQL 提交带有以下参数的查询:

GraphiQL query and its results

我的 Spring Controller (从 here 复制)看起来像这样:

@Controller
@EnableAutoConfiguration
public class MavenController {
    private final MavenSchema schema = new MavenSchema();
    private final GraphQL graphql = new GraphQL(schema.getSchema());
    private static final Logger log = LoggerFactory.getLogger(MavenController.class);

    @RequestMapping(value = "/graphql", method = RequestMethod.OPTIONS, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Object executeOperation(@RequestBody Map body) {
        log.error("body: " + body);
        final String query = (String) body.get("query");
        final Map<String, Object> variables = (Map<String, Object>) body.get("variables");
        final ExecutionResult executionResult = graphql.execute(query, (Object) null, variables);
        final Map<String, Object> result = new LinkedHashMap<>();
        if (executionResult.getErrors().size() > 0) {
            result.put("errors", executionResult.getErrors());
            log.error("Errors: {}", executionResult.getErrors());
        }
        log.error("data: " + executionResult.getData());
        result.put("data", executionResult.getData());
        return result;
    }
}

理论上,当我在 GraphiQL 中提交查询时,应该调用 executeOperation。不是的,我在控制台输出中没有看到日志语句。

我做错了什么?在 GraphiQL 中提交查询时,如何确保调用 MavenController.executeOperation?

更新 1 (13.01.2017 13:35 MSK):以下是有关如何重现错误的教程。我的目标是创建一个基于 Java 的 GraphQL 服务器,我可以使用 GraphiQL 与之交互。如果可能的话,这应该在本地工作。

我已经read为此,需要执行以下步骤:

  1. Set up a GraphQL server. An example of this can be found here https://github.com/graphql-java/todomvc-relay-java. That example uses Spring Boot, but you can use whatever HTTP server you like to get that going.
  2. Set up the GraphiQL server. That is a bit beyond the scope of this project, but basically you need to get GraphiQL talking to the server in step 1 above. It will use introspection to load the schema.

我查看了该项目todomvc-relay-java ,根据我的需要修改一下,放入目录E:\graphiql-java\graphql-server。您可以下载该目录的存档 here .

第 1 步:安装 Node.JS

第 2 步

转到 E:\graphiql-java\graphql-server\app 并在那里运行 npm install

第3步

从同一目录运行npm start

第 4 步

转到 E:\graphiql-java\graphql-server 并在那里运行 gradlew start

第 5 步

运行docker run -p 8888:8080 -d -e GRAPHQL_SERVER=http://localhost:8080/graphql merapar/graphql-browser-docker

Docker 来源:graphql-browser-docker

第 6 步

启动 Chrome 并禁用 XSS 检查,例如。 G。 “C:\Program Files (x86)\Google\Chrome\Application\chrome.exe”--args --disable-xss-auditor。一些消息来源声称您必须杀死所有其他 Chrome 实例才能使这些参数生效。

第 7 步

打开http://localhost:8888/在该浏览器中。

第 8 步

尝试运行查询

{ allArtifacts(组:“com.graphql-java”,名称:“graphql-java”){ 团体 姓名 版本 } }

实际结果:

1) Chrome 的控制台选项卡中出现错误:Fetch API 无法加载 http://localhost:8080/graphql。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin” header 。因此,不允许访问源“http://localhost:8888”。响应的 HTTP 状态代码为 403。如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以在禁用 CORS 的情况下获取资源。

Errors in the "Console" tab

2) Chrome 的网络标签中出现错误:

Errors in the "Network" tab

更新2(2017年1月14日13:51):目前,CORS在Java应用程序中按以下方式配置。

主类:

@SpringBootApplication
public class Main {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Main.class, args);
    }

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry
                        .addMapping("/**")
                        .allowedMethods("OPTIONS")
                        .allowedOrigins("*")
                        .allowedHeaders(
                                "Access-Control-Request-Headers",
                                "Access-Control-Request-Method",
                                "Host",
                                "Connection",
                                "Origin",
                                "User-Agent",
                                "Accept",
                                "Referer",
                                "Accept-Encoding",
                                "Accept-Language",
                                "Access-Control-Allow-Origin"
                        )
                        .allowCredentials(true)
                        ;
            }
        };
    }
}

WebSecurityConfigurerAdapter 子类:

@Configuration
@EnableWebSecurity
public class SpringWebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        System.out.println("SpringWebSecurityConfiguration");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("OPTIONS");
        source.registerCorsConfiguration("/**", config);
        http
                .addFilterBefore(new CorsFilter(source), ChannelProcessingFilter.class)
                .httpBasic()
                .disable()
                .authorizeRequests()
                    .anyRequest()
                    .permitAll()
                .and()
                .csrf()
                    .disable();
    }
    @Override
    public void configure(WebSecurity web) throws Exception {
    }
}

Controller :

@Controller
@EnableAutoConfiguration
public class MavenController {
    private final MavenSchema schema = new MavenSchema();
    private final GraphQL graphql = new GraphQL(schema.getSchema());
    private static final Logger log = LoggerFactory.getLogger(MavenController.class);

    @CrossOrigin(
            origins = {"http://localhost:8888", "*"},
            methods = {RequestMethod.OPTIONS},
            allowedHeaders = {"Access-Control-Request-Headers",
            "Access-Control-Request-Method",
            "Host",
            "Connection",
            "Origin",
            "User-Agent",
            "Accept",
            "Referer",
            "Accept-Encoding",
            "Accept-Language",
             "Access-Control-Allow-Origin"},
            exposedHeaders = "Access-Control-Allow-Origin"
             )
    @RequestMapping(value = "/graphql", method = RequestMethod.OPTIONS, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Object executeOperation(@RequestBody Map body) {
        [...]
    }
}

MavenController.executeOperation 是当我在 GraphiQL 中发出查询请求时应该调用的方法。

更新3(2017年1月15日22:05):尝试使用CORSFilter,新源代码为here 。没有结果,我仍然收到“无效的 CORS 响应”错误。

最佳答案

问题在于应用程序中使用 .allowedMethods("OPTIONS") 配置。

通过设计发送的预检请求使用 OPTIONS 请求方法发送。

Access-Control-Request-Method 由浏览器自动添加,allowedMethods 属性实际上控制实际请求允许的请求方法。

来自docs ,

The Access-Control-Request-Method header notifies the server as part of a preflight request that when the actual request is sent, it will be sent with a POST request method.

所以它是 POST 方法,并且请求失败,因为验证完成后您只允许 OPTIONS

因此,将 allowedMethods 更改为 * 将匹配浏览器为实际请求设置的 POST 请求方法。

完成上述更改后,您将收到 405 错误,因为您的 Controller 仅允许您的 POST 请求使用 OPTIONS

因此,您需要更新 Controller 请求映射,以允许 POST 使实际请求在预检请求之后成功。

响应示例:

{
    "timestamp": 1484606833696,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "graphql.AssertException",
    "message": "arguments can't be null",
    "path": "/graphql"
}

我不确定您是否在太多地方设置了 CORS 配置。我只需更改 spring security 配置中的 .allowedMethods 即可使其按照我描述的方式工作。所以你可能想研究一下。

关于java - 为什么我的 Spring Controller 不处理 GraphiQL 发送的 OPTIONS 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41589542/

相关文章:

Java:带有 switch 语句的 do-while 循环

java - Arrays.asList 给出 UnsupportedOperationException

java - 每个请求一个事务 - servlet 响应提交得太早

java - 如何在没有applicationContext的情况下在Spring服务类中请求Prototype bean

c# - ASP.NET DropDownList 中的 XSS 可能吗?

java - 通过 Spring 注入(inject)空 map

java - -XX :+PrintTenuringDistribution not printing the tenuring age

java - org.hibernate.MappingException : Unknown entity when trying to create a new record

security - 如果我将所有 '<' 替换为 '&lt;' ,我的站点是否免受 XSS 攻击?

xss - 具有隐藏输入的跨站脚本