【问题标题】:Wiremock CORS not workingWiremock CORS 不工作
【发布时间】:2017-07-14 07:45:57
【问题描述】:

我已经有效地使用wiremock 有一段时间了,我想启用CORS 对模拟API 的访问。

我尝试在响应头中设置 Access-Control-Allow-Origin: * 和其他头,均无济于事。

这是我拥有的映射示例:

{
    "request": {
        "method": "POST",
        "urlPattern": "/api/v2/general/login[\\/\\&\\?]*",
        "bodyPatterns": [{
            "equalToJson": "{\"password\":\"password\",\"username\":\"john@cougar.com\"} ",
            "jsonCompareMode": "LENIENT",
            "ignoreArrayOrder" : true,
            "ignoreExtraElements" : true
        }]
    },
    "response": {
        "status": 200,
        "headers": {
            "Content-Type": "application/json",
            "Access-Control-Allow-Origin" : "*",
            "Access-Control-Allow-Methods" : "*",
            "Access-Control-Allow-Headers": "Accept, Content-Type, Content-Encoding, Server, Transfer-Encoding",
            "X-Content-Type-Options" : "nosniff",
            "x-frame-options" : "DENY",
            "x-xss-protection" : "1; mode=block"
        },
        "bodyFileName": "/login_response_johncougar.json"
    }
}

我在这里做错了什么导致 CORS 无法工作?

提前致谢。

【问题讨论】:

  • 您提出什么样的要求?如果它是 GET 以外的任何东西,您还需要实现一个带有一些 CORS 标头的 OPTIONS 存根。
  • @Tom,我同时发出 GET 和 POST 请求。 “使用一些 CORS 标头实现 OPTIONS 存根”是什么意思?
  • CORS 协议要求浏览器发出一个 OPTIONS “预检”请求,以在发出符合特定条件的请求之前检查允许他们做什么。值得一看developer.mozilla.org/en-US/docs/Web/HTTP/…
  • 是否已解决,如果已解决,您是如何让它工作的?我为 OPTIONS 请求存根。但它并没有发出我想要的 POST。

标签: rest cors wiremock


【解决方案1】:

这是一个示例,它有效

{
 "request":
 {
 "urlPattern": "/country/([a-z]*)",
 "method": "GET"
 },

 "response":
 {
 "status": 200,
 "headers":
 {
 "Content-Type" : "application/json",
 "Access-Control-Allow-Origin" : "*",
 "Access-Control-Allow-Methods" : "*",
 "Access-Control-Allow-Headers": "Accept, Content-Type, Content-Encoding, Server, Transfer-Encoding",
 "X-Content-Type-Options" : "nosniff",
 "x-frame-options" : "DENY",
 "x-xss-protection" : "1; mode=block"
 },
 "body": "{ \"statusCode\" : \"S1000\", \"statusDescription\" : \"Success\", \"content\" : [ { \"id\" : \"1111\", \"name\" : \"aaaa\"}, { \"id\" : \"2222\", \"name\" : \"asd\" } ] }"
 }
}

按原样使用,wiremock 在间距方面很特别,这里我使用了单个空格而不是制表符,希望对您有所帮助。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。经过长时间的搜索但没有找到解决方案,我开始使用 groovy 文件,终于找到了解决方案。

    您只需要在 header() 方法中添加每个标题。这将解决问题。所以你的示例 groovy 合同将是这样的:

    {
      "request": {
        "method": "POST",
        "urlPattern": "/api/v2/general/login[\\/\\&\\?]*",
        "bodyPatterns": [{
          "equalToJson": "{\"password\":\"password\",\"username\":\"john@cougar.com\"} ",
          "jsonCompareMode": "LENIENT",
          "ignoreArrayOrder": true,
          "ignoreExtraElements": true
        }]
      },
      "response": {
        "status": 200,
        "headers": {
          header("Content-Type": "application/json"),
          header("Access-Control-Allow-Origin": "*"),
          header("Access-Control-Allow-Methods": "*"),
          header("Access-Control-Allow-Headers": "Accept, Content-Type, Content-Encoding, Server, Transfer-Encoding"),
          header("X-Content-Type-Options": "nosniff"),
          header("x-frame-options": "DENY"),
          header("x-xss-protection": "1; mode=block")
        },
        "bodyFileName": "/login_response_johncougar.json"
      }
    }
    

    我希望它能解决您的问题(实际上,如果您使用 groovy 合约,它会很有用)。

    【讨论】:

      【解决方案3】:

      我刚刚设法解决了这个问题。实际上解决方案已经在这里Adding headers to Jetty in Wiremock

      因为您的浏览器会在发出任何实际请求之前发送 CORS 预检请求,所以您需要设置您的 wiremock 以存根 OPTIONS 请求并发送回标头。

      例如,
      Access-Control-Allow-Origin = "*", Access-Control-Allow-Headers: "content-type", Access-Control-Allow-Methods = "POST, GET"

      Access-Control-Allow-Headers 的值必须与 Access-Control-Request-Headers 中包含的值相同 Request header field Access-Control-Allow-Headers is not allowed by itself in preflight response

      您的所有回复也必须发回标头"Access-Control-Allow-Origin": "*"

      【讨论】:

        【解决方案4】:

        您可以通过添加 --enable-stub-cors 标志来禁用 cors

        • 独立示例:
        java -jar wiremock-jre8-standalone-2.31.0.jar --port 8081 --enable-stub-cors
        
        • Docker 示例:
        docker run -it --rm -p 8081:8080 --name wiremock-yadoms -v $PWD:/home/wiremock wiremock/wiremock --enable-stub-cors
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-08-19
          • 2018-06-01
          • 2013-08-25
          • 2013-11-14
          • 2018-09-28
          • 2016-04-18
          • 2018-07-13
          • 2016-06-08
          相关资源
          最近更新 更多