2

我们有 Unfiltered 与底层 Jetty 服务器(不确定,但我相信 Unfiltered 使用 Jetty 8)。现在我们需要为我们返回的所有响应添加标题条目。

我可以获得底层的 Jetty Server 并尝试将 Handler 直接添加到它。不确定我是否犯了一些愚蠢的错误或 Unfiltered 是否做了某些事情,因为我设法添加了该标题,但同时我删除了所有其他功能。不好 :)

我还在 jetty.xml 中找到了这样做的方法,但没有得到它的工作。

现在尝试让它与 Cycle.Intent 一起使用,但是在将其中两个添加到 Plan 时遇到类型问题。

object AllowOrigin {
  case class AllowOriginResponseFunctionWrapper[A](req: HttpRequest[A], f: ResponseFunction[Any]) extends ResponseFunction[Any] {
    def apply[T](res: HttpResponse[T]) = req match {
      case _ =>
        val resp = f(res)
        resp.header("Access-Control-Allow-Origin", "*")
        resp
    }
  }

  def apply[A](inner: Cycle.Intent[A,Any]): Cycle.Intent[A,Any] = {
    case req @ _ => {
      inner.orElse({ case _ => Pass }: Cycle.Intent[A,Any])(req) match {
        case Pass => Pass
        case responseFunction => AllowOriginResponseFunctionWrapper(req, responseFunction)
      }
    }
  }
}
4

1 回答 1

2

我想出了如何在不破坏现有功能的情况下做到这一点。不幸的是,我必须在我拥有的每个计划中添加代码,但无论如何这都是很小的变化。

首先定义意图。

object AllowAllOrigin extends unfiltered.kit.Prepend {
  def intent = Cycle.Intent {
    case _ ⇒ ResponseHeader("Access-Control-Allow-Origin", Set("*"))
  }
}

然后将其添加到计划中的意图中,如果在计划特定的东西之前有两件事想要做,只需添加更多。

def intent = AllowAllOrigin { Authentication(config) {
  case _ => Ok // or perhaps something more clever here :)
}}
于 2015-01-26T06:49:38.087 回答