-
Notifications
You must be signed in to change notification settings - Fork 38.9k
Enhance UrlHandlerFilter with ordered pattern matching and status validation #35975
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
base: main
Are you sure you want to change the base?
Enhance UrlHandlerFilter with ordered pattern matching and status validation #35975
Conversation
Signed-off-by: James Missen <[email protected]>
Signed-off-by: James Missen <[email protected]>
1306db6 to
91c3bb0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes.
The extraction of the lookupPath as a subpath is a breaking change. I appreciate the proposal, but as implemented it will break any application with a context path. This needs to be opt-in, e.g. excludeContextPath().
The idea with excludes was not in the original description and has not been discussed. If using specificity order, it seems like it can end up in the wrong order if mixed with all regular patterns. If using order of registration, one has to be careful to specify those first and they would always have to be checked first adding overhead. I don't quite see the use case for this in combination with trailing slash handling. Did this come from an actual use case? I would prefer to take it out and consider it separately, and as a general rule of thumb, it's best to split out extras like that so it doesn't hold up the rest of the PR.
For useSpecificityOrder, maybe sortPatternsBySpecificity().
For the HandlerRegistry abstraction, I didn't try but it seems the two implementations can be merged into one by using Map<PathPattern, Handler> where the implementation is either LinkedHashMap or TreeMap. That would remove the need for a HandlerRegistry abstraction in the first place, and significantly simplify the implementation.
Signed-off-by: James Missen <[email protected]>
Signed-off-by: James Missen <[email protected]>
Signed-off-by: James Missen <[email protected]>
|
You're right about the lookup path. I adopted the same path matching as Spring Security, as that was a major reason why the filter was introduced (#28552). Currently the filter matches include the context path. This is probably not the desired behaviour, as any path matching should be context path-invariant as it is elsewhere in Spring (e.g. in web routes, resource handlers etc.). In addition, the current behaviour not being the same as Spring Security means that the
Obviously the application code breaking when a different context path is used is likely not what anyone would want or expect. I have changed this to opt-in as you suggested, with The excluded paths wasn't discussed, but the idea is simply that, just as this originally presented use case is valid: an equally valid use case would be: That is, it might be just as desirable to not handle trailing slashes for a more specific path, as it is desirable to handle them in a different way for a more specific path. However I have removed this from the PR as suggested. I have renamed As for the Given the current behaviour is to look up handlers in the order that they added, refactoring to use Conversely, you could refactor the specificity ordering to also use the current It basically boils down to the fact that:
So it makes the most sense to have Even if you wanted the two cases to use the same map structure and remove the Hopefully that makes sense and it's clear why abstraction is the best approach. I know it looks and feels sizeable with the addition of the new classes, but that size is deceptive in conveying the actual complexity. Really, they're mostly just encapsulating maps. Java doesn't make it easy to be succinct. |
This PR addresses issue #35882.
It does the following:
Allows for ordering handlers based on path specificity, so handlers are selected based on how well a
PathPatternmatches the request path.Validates the HTTP status code for redirect handlers, using a simple assertion to check if the code is
3xx.The changes are backwards compatible:
A new
useSpecificityOrdermethod is added to theBuilderto opt in to specificity-based ordering. Not using this method (or using a value offalse) results in the current behaviour.A new
excludemethod is added to theBuilderto optionally exclude URL handling for specific patterns. Not using this method results in the current behaviour.For the servlet implementation, the parameter of the
redirectmethod was changed from typeHttpStatustoHttpStatusCodeto align with the reactive implementation. This change will have no effect asHttpStatusCodeis implemented byHttpStatus.Feedback and edits are welcome, particularly regarding the implementation of specificity-based ordering, and the new method names (
useSpecificityOrderandexclude).