简体   繁体   English

Spring wevmvc应用中URI匹配

[英]URI matching in Spring wevmvc application

I have a very basic web mvc program.我有一个非常基本的 web mvc 程序。 I currently scan all classes with Controller annotation and scan all the declared methods to see if they have a custom annotation.我目前扫描所有带有 Controller 注释的类,并扫描所有声明的方法以查看它们是否具有自定义注释。 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.到目前为止一切都很好,但是我必须匹配过滤器中的路径,这就是我的问题所在。我还没有找到一个库/util/工具可以做到这一点。 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.过滤器所做的不是此时的问题,而是将传入的请求 URI 与注释定义的 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.这意味着定义的路径可以是 /mypath/{param1} 而传入的 URI 是 /mypath/somevariable 所以我不能使用精确匹配。 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.我知道我可以做一些事情,例如用 / 拆分 URI,用 * 替换变量部分,并使用 ANT 路径匹配器或类似的东西。 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.应用程序容器(或 web 容器或任何正式名称)可以将传入请求路由到正确的处理程序这一事实意味着它已经完成,并且用于执行此操作的工具已经可用或存在。 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.我想知道我是否应该尝试找到现有的工具,或者是否有其他更明智的方法来做到这一点,因为容器已经完成(或将要完成)URI 匹配。 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.这发生在我提出问题并且有人发表评论之前,我已经得到了遗漏的词,这让我找到了解决方案。

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 路径(很可能是过去使用 ANT 构建系统时的“创伤”和一般懒惰的结合)。 ANT path matching was just the tool I was looking for and so far it seems to work for my purpose. ANT 路径匹配正是我一直在寻找的工具,到目前为止它似乎对我的目的有用。 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);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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