'URI matching in Spring wevmvc application
I have a very basic web mvc program. I currently scan all classes with Controller annotation and scan all the declared methods to see if they have a custom annotation. I gather all the paths that have the custom annotation so I can do my custom magic in a filter. All is well so far, but then I have to match the path in the filter and here is where my question comes in. I have yet to find a library/util/tool that does that. What I am trying to achieve is a non intrusive way to custom annotate a request mapping so I can do some filter magic. What the filter does is not the issue at this point, but matching the incoming equest URI to annotation defined URI. The problem here is that the defined path has placeholders for parameters, but the incoming request has them already filled. Meaning that the defined path can be /mypath/{param1} and the incoming URI is /mypath/somevariable so I cannot use exact matching. I know I could do something like for example split the URI by / and replace the variable part with * and use ANT path matcher or something similar like that. But I see no point in reinventing the wheel and doing work that has already been done. The fact that the application container (or web container or whatver it is officially called) can ruote the incoming request to a correct handler means it is done already and the tools for doing that are already available or exists. While writing this I came up with another question. I wonder should I try to find the existing tools or might there be some other more sensible way of doing this since the container has already done (or will do) the URI matching. These aren’t things I came up with I am pretty sure that these might have been thought out already.I haven’t yet found solution for this, but it might also be that I am just missing the right search words. This has happened before I have asked something and someone has commented and I have gotten the missing words and it had lead to me find the solution.
Solution 1:[1]
It appears that I have misunderstood or understood incorrectly ANT paths (most likely combination of past “trauma” when working with ANT build systems and generic laziness). ANT path matching was just the tool I was looking for and so far it seems to work for my purpose. Here is a little code snippet that shows what I was aiming for (I modified my original code on the spot so sorry for any major mistakes):
@Component
@Order(Ordered.HIGHEST_PRECEDENCE - 1)
public class AuthorizationFilter extends OncePerRequestFilter
{
private static final Logger LOG = LoggerFactory.getLogger(AuthorizationFilter.class);
Private List<String> collectedEndpointPaths;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException
{
LOG.trace("Checking authorization for [{}] {}", request.getMethod(), request.getRequestURI());
for (String path : collectedEndpointPaths) {
if((new AntPathMatcher()).match(path,z request.getRequestURI())) {
// Do my magic
}
}
filterChain.doFilter(request, response);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mikko Sirén |
