regex - Azure 应用程序网关自定义 header 规则

标签 regex azure request azure-application-gateway

我们已经设置并运行了 Azure 应用程序网关。我们想要添加一条与名为 X 的 header 匹配以下条件的规则。

  1. 如果传入请求中根本不存在 X - 则让请求通过
  2. 如果 X 存在但为空或包含空格 - 则阻止请求
  3. 如果 X 存在但包含至少 1 个非空格字符 - 则让请求通过

我使用了以下模式 .+ (任意字符 0 多次) - 这仅满足要点 1,但无法满足要点 2(如果 header 为空,它不会阻止请求或有空格)和 3(如果 header 包含非空格字符,例如 p,则请求将无法通过)。

enter image description here

我需要使用什么类型的正则表达式来满足所有三个条件? 我期望 .+ 至少满足要点 1 和 3。

https://learn.microsoft.com/en-us/azure/application-gateway/rewrite-http-headers-url#check-for-the-presence-of-a-header

最佳答案

使用

^ *[^ ].*$

说明

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
   *                       ' ' (0 or more times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  [^ ]                     any character except: ' '
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

关于regex - Azure 应用程序网关自定义 header 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68698331/

相关文章:

java - 我无法在java中获取第一组正则表达式模式

regex - 使用Powershell和正则表达式操作String

mysql - 正则表达式模式相当于 mysql 中的 %word%

android - FileAsyncHttpResponseHandler 取消请求

node.js - 如何使用请求模块缓冲 HTTP 响应?

ios - Facebook iOS App 邀请好友

java - 正则表达式替换如果变量与字符串不匹配

database - 如何安排 Azure 数据库导入?

azure - 如何将脚本操作添加到 Azure HD Insight 群集

azure - 尝试通过 Azure 进行身份验证以使用 Power BI Embedded