Table of Contents
Correct answer
Home Java How to implement CORS in Reactor Netty?

How to implement CORS in Reactor Netty?

Feb 05, 2024 am 11:57 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)