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

How to implement CORS in Reactor Netty?

王林
Release: 2024-02-05 11:57:51
forward
814 people have browsed it
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);
}
Copy after login


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()));
}
Copy after login

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);
}
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template