【问题标题】:Spring RequestMapping antmatcher ignore URLSpring RequestMapping antmatcher 忽略 URL
【发布时间】:2019-01-26 01:11:26
【问题描述】:

我有一个相当简单的 RequestMapping 从 ui-router 转发 url:

@RequestMapping(value = "/**/{path:[^\\.]*}")
    public String redirect(@PathVariable("path") String path) {
        // Forward to home page so that route is preserved.
        return "forward:/";
    }

这是为了让一切都与我的 SAML IDP 配合得很好。但是,我也有一个我正在创建的 websocket,我 不想 想要通过这里。 websocket url 的格式为:

/ws/**

我一直在尝试使用正则表达式来忽略其中包含“ws”的网址,但我运气不佳。我非常接近,但我尝试过的任何事情都没有让我得到我想要的。有没有办法可以将我的 antmatcher 中的第一个 /** 与正则表达式结合起来忽略我想要的?我可以编写正则表达式没问题,我只是不确定如何将它合并到 antmatcher 中。

tldr:我需要一个匹配的匹配器:

/bbs/索引 /邮箱 等等……

但不是:

/ws/信息

【问题讨论】:

  • 正则表达式匹配任何不包含“ws”的字符串:(?!.*ws).*
  • 我应该澄清一下,编写不包含“ws”的正则表达式不是我遇到的问题。我只是不确定如何将它合并到 antmatcher 的第一部分中。
  • 很容易拒绝所有 /ws/** 请求并允许使用以下代码 sn-p 的所有其他请求,但我想这不是您想要的,因为您还应该以某种方式服务 ws 请求。 antMatchers.denyAll("/ws/**").antMatchers.permitAll("/**")

标签: java angularjs spring-boot angular-ui-router saml-2.0


【解决方案1】:

您可以尝试将 antMatchers 保留为默认值,并尝试使用两种不同的 RequestMapping,其中一种用于/**,另一种用于/ws/**

对于 WS 请求,请使用以下内容:

@RequestMapping(value = "/ws/**/{path:[^\\.]*}")
    public String wsHandling(@PathVariable("path") String path) {
        // Do some other stuff
        return null;
    }

对于其余的请求,请使用以下内容:

@RequestMapping(value = "/**/{path:[^\\.]*}")
    public String redirect(@PathVariable("path") String path) {
        // Forward to home page so that route is preserved.
        return "forward:/";
    }

【讨论】:

    【解决方案2】:

    我想出了一个更简单的解决方案。我没有尝试重新设计 antmatcher,而是在我所有的 ui-router 状态 url 前加上“zfgcui”。我的 antmatcher 现在看起来像:

    /zfgcui/**/{path:[^\\.]*}

    现在忽略 ws 端点并继续只转发 ui-router 端点。实现起来要简单得多,也不那么令人沮丧

    【讨论】:

      猜你喜欢
      • 2013-10-19
      • 2021-02-27
      • 2018-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多