Home >Java >How to implement CORS in Reactor Netty?

How to implement CORS in Reactor Netty?

王林
王林forward
2024-02-05 11:57:51813browse
Question content

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete