You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vert.x应用CORS响应头配置无效问题求助

Vert.x应用CORS响应头配置问题

我在Vert.x应用中配置必要的CORS响应头时遇到问题,尽管尝试了相关解决方案,仍无法成功为响应添加对应头信息。前端发送请求时收到以下错误:

Access to XMLHttpRequest at 'http://localhost:8081/api' from origin 'http://localhost:4287' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我的HttpServerVerticle类相关代码片段如下:

public class HttpServerVerticle extends AbstractVerticle {

    @Override
    public void start(Promise<Void> startPromise) {
        ApiServiceApiHandler apiHandler = new ApiServiceApiHandler(new ApiServiceApiImpl());

        RouterBuilder.create(vertx, specFile)
                .map(builder -> {
                    builder.setOptions(
                            new RouterBuilderOptions()
                                    // For production use case, you need to enable this flag and provide the
                                    // proper security handler
                                    .setRequireSecurityHandlers(false));

                    vertx.eventBus().registerDefaultCodec(DBUserRequest.class, new LocalEventBusCodec<>(DBUserRequest.class));
                    vertx.eventBus().registerDefaultCodec(ArrayList.class, new LocalEventBusCodec<>(ArrayList.class));

                    apiHandler.mount(builder);

                    Router router = builder.createRouter();

                    router.route().handler(CorsHandler.create("http://localhost:4287")
                            .allowedMethod(io.vertx.core.http.HttpMethod.GET)
                            .allowedMethod(io.vertx.core.http.HttpMethod.POST)
                            .allowedMethod(io.vertx.core.http.HttpMethod.OPTIONS)
                            .allowCredentials(true)
                            .allowedHeader("Access-Control-Allow-Method")
                            .allowedHeader("Access-Control-Allow-Origin")
                            .allowedHeader("Access-Control-Allow-Credentials")
                            .allowedHeader("Content-Type"));

                    router.errorHandler(400, new ExceptionHandler());
                    router.errorHandler(405, new ExceptionHandler());
                    router.errorHandler(404, new ExceptionHandler());

                    return router;

                })
               .compose(router -> vertx.createHttpServer(options).requestHandler(router).listen(Vertx.currentContext().config().getInteger(ConfigConstants.PROPERTY_SERVICE_HTTP_PORT)))
               .onSuccess(server -> logger.info("Http verticle deploy successful"));
    }
}

我已尝试过相关解决方案,也尝试将origin改为.*,但结果一致,恳请提供解决该CORS问题的指导或建议。

内容的提问来源于stack exchange,提问作者Shakya Rathnaweera

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 02:40:22