如您所知,cors 標準包括最初發送options 請求來檢查有效性,我決定在處理程序中釋放對options 請求的處理,但是出現了返回布林值,並且僅處理options 請求的問題,並跳過runon 中的其餘部分,也許您知道處理cors 幫助程式請求的其他方法?
public void run() { HttpServer.create() .host(this.config.getHost()) .port(this.config.getPort()) .runOn(eventLoopGroup.newEventLoopGroup()) .protocol(HttpProtocol.HTTP11) .handle((request, response) -> { if (request.method().equals(HttpMethod.OPTIONS)) return response .header("Access-Control-Allow-Origin", "*") .header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE") .header("Access-Control-Allow-Headers", "Content-Type, Authorization") .sendHeaders(); else ??????? }) .route(this.provider::run) .bindUntilJavaShutdown(Duration.ofSeconds(30), this::onStart); }
如果有人突然需要
private @notnull corsconfig getcorsconfig() { return corsconfigbuilder.foranyorigin() .allownullorigin() .allowcredentials() .allowedrequestmethods(httpmethod.get, httpmethod.post, httpmethod.delete, httpmethod.put, httpmethod.patch) .allowedrequestheaders("content-type", "authorization") .build(); } public void run() { httpserver.create() .host(this.config.gethost()) .port(this.config.getport()) .protocol(httpprotocol.http11) .doonconnection(this::handler) .route(this.provider::run) .binduntiljavashutdown(duration.ofseconds(30), this::onstart); } private void addhandler() { this.connection.addhandlerlast(new corshandler(getcorsconfig())); }
嘗試像這樣替換 handle()
來建立 http 伺服器應該可以解決您的問題:
public void run() throws IOException { CorsConfig corsConfig = CorsConfigBuilder.forAnyOrigin().disable().build(); HttpServer.create() .host(this.config.getHost()) .port(this.config.getPort()) .runOn(eventLoopGroup.newEventLoopGroup()) .protocol(HttpProtocol.HTTP11) .handle(corsConfig) .route(this.provider::run) .bindUntilJavaShutdown(Duration.ofSeconds(30), this::onStart); }
查看此配置的更多詳細資訊:https://www.php.cn/link/e2a23af417a2344fe3a23e652924091f
#以上是如何在Reactor Netty實現CORS?的詳細內容。更多資訊請關注PHP中文網其他相關文章!