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

Closed
Leviathan856 opened this issue Oct 26, 2024 · 2 comments
Closed

Question about filters and CORS #2193

Leviathan856 opened this issue Oct 26, 2024 · 2 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

@ysysimon
Copy link

ysysimon commented Nov 2, 2024

I'm not sure if you have the same problem as me

@hwc0919
Copy link
Member

hwc0919 commented Nov 12, 2024

You should register handlers before run().

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

4 participants
@hwc0919 @ysysimon @Leviathan856 and others