Spark WS Framework Filter being called twice
Asked Answered
L

1

6

I am tying to use a before filter to add trailing slashes as well as a before filter to handle authentication on certain endpoint.

Here is my routing code:

// Add filter to all requests which adds a trailing slash if it is missing //
before("*", Filters.addTrailingSlashes);
path("/api", () -> {
    // Authentication Intercept //
    before("/*", AuthenticationIntercept.authenticationIntercept);

    // Sampling Endpoints //
    get(Path.Web.SAMPLES_FETCH_LATEST, SamplingController.fetchLatestSamples, new JsonTransformer());
    get(Path.Web.SAMPLES_FETCH_FOR_DEVICE, SamplingController.getLatestSamplesForDevice, new JsonTransformer());
});

I then hit the following endpoint:

localhost:4567/api/samples/10

What happens is that the addTrailingSlashes gets called first. Then the authentication Filter gets called, then the addTrailingSlashes gets called again this time with localhost:4567/api/samples/10/ as the request endpoint and finally the authentication filter gets called again.

Is this the expected behavior? What I want to happen is that the addTrailingSlashes gets called once adds the slash and then forwards the request one time so that the authentication filter gets called only once.

Any ideas would be most appreciated.

Thanks, Nathan

Lattermost answered 26/2, 2018 at 17:48 Comment(0)
B
2

I had the same issue but with another type of filter. It turned out that the browser makes two calls, the second to get favicon.ico on the root (/favicon.ico) which triggered the filter.

I do not have any services configured on the root path in my app, so it seems that the filter is triggered for all calls, even those that are not mapped.

I verified that by using some other path that was not mapped, try something like:

http://yourdomain.com/aaa/bbb

This call did also make my filter trigger twice. The first to the non-existent service, and one to get the favicon.ico.

It's helpful to use a http-monitoring software like Fiddler or the like to see what calls are made.

It's quite easy to check for the favicon case in the filter and ignore it. To check that the call is made to a valid service is a bit more work. Maybe there's a better way to do it.

Blanchblancha answered 1/3, 2018 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.