简体   繁体   中英

should grails filters run twice on forward in this instance?

According to this mailinglist entry , grails filters should be invoked on a forward. which effectively causes filters to be invoked 1 + x number of forwards .

However, in my situation i question the method of how this is done. Here is a rough mashup of my system:

  • i have a filter which is invoked on the uri condition of "/api/**"
  • an urlmapping that goes something like this: "/api/$version/$apiKey/$rest**"
    • $rest may contain (as an example) the value /book/show/1
  • an ApiController thats sole purpose is to forward to another uri depending on $rest

So lets say i access the url /api/0.1/Ks38...7fn38/book/show/1 what then happens is that the api filter is invoked and it does all the version and apiKey validatation. it then continues to forward to the uri /book/show/1 which then invokes the Api filter a second time even though the filter is mapped up agains /api/** and the new uri is effectively /book/... . Now let it be said, i agree that filters should be invoked on a forward, but i dont agree that the api filter should be invoked a second time when i am doing a forward to /book...

This can't be correct behaviour? can it?

well the forward is technically not a new 'request' and is using the original request. If you compare the forwardUri with the controller/action, you will notice the change. This is how the API Toolkit handles chaining and avoids a collision like that.

I have experienced the same issue, and discussed it at great length on the Grails mailing lists. This behavior has been present since prior to 1.2.x and is not likely to change.

As a result I was able to "fix" the issue myself by adding a flash parameter (flash.apiValid = true) prior to forwarding within my "ApiController" which is then checked in my filter to prevent the ApiController from being called again.

While this may not be the most elegant solution, it is a solution that works. Hope it helps you too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM