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
答案 0 :(得分:2)
我有同样的问题,但使用其他类型的过滤器。事实证明,浏览器进行两次调用,第二次调用root(/favicon.ico)上的favicon.ico,触发过滤器。
我的应用程序中的根路径上没有配置任何服务,因此似乎所有调用都会触发过滤器,即使是那些未映射的调用。
我通过使用未映射的其他路径验证了,尝试类似:
此调用也使我的过滤器触发两次。第一个是不存在的服务,另一个是获取favicon.ico。
使用像Fiddler之类的http监控软件看看有什么电话是有帮助的。
在过滤器中检查favicon案例并忽略它非常容易。要检查是否对有效服务进行了调用,需要更多工作。也许还有更好的方法。