As you know, the cors standard includes initially sending options request to check validity, I decided to release the processing of options request in the handler, but the return boolean value appeared , and only handle the options request and skip the rest in the runon, maybe you know of other ways to handle cors helper requests?
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); }
Correct answer
If anyone suddenly needs it
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())); }
Try replacing handle()
like this to create an http server which should solve your problem:
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); }
View more details of this configuration: https://www.php.cn/link/e2a23af417a2344fe3a23e652924091f
The above is the detailed content of How to implement CORS in Reactor Netty?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor
