Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about filters and CORS #2193

Open
Leviathan856 opened this issue Oct 26, 2024 · 0 comments
Open

Question about filters and CORS #2193

Leviathan856 opened this issue Oct 26, 2024 · 0 comments

Comments

@Leviathan856
Copy link

Hi,
I have encountered some troubles implementing custom filters to enable access to server from a front-end application.
So, I added the following code and couldn't figure out why the custom filter is not applied (doFilter function was not called):

class AuthFilter : public drogon::HttpFilter<AuthFilter, false>
{
public:
    void doFilter(const drogon::HttpRequestPtr &req,
                        drogon::FilterCallback &&fcb,
                        drogon::FilterChainCallback &&fccb) override
    {
        auto resp = drogon::HttpResponse::newHttpResponse();

        resp->addHeader("Access-Control-Allow-Origin", "http://localhost:xxxx");

        fcb(resp);
    }
};

int main()
{
    auto authFilter = std::make_shared<AuthFilter>();

    app()
        .loadConfigFile("./config.json")
        .setLogPath("./build/log")
        .registerFilter(authFilter)
        .registerBeginningAdvice(serviceSetup)
        .run();
    return EXIT_SUCCESS;
}

static void serviceSetup()
{
...
    app()
        .registerHandler("/api/auth/isUserExist?username={username}", &usernameCheck, {Get, "AuthFilter"})
        .registerHandler("/api/auth/signup", &signUpHandler, {Post, "AuthFilter"})
        .registerHandler("/api/auth/signin", &signInHandler, {Post, "AuthFilter"});
...
}

I know I can bypass using filters by using registerPostHandlingAdvice, but I need different policy for different controllers.
I would appreciate any help on this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant