在Unfiltered中添加响应标头

时间:2017-02-16 15:10:26

标签: scala server cors jetty unfiltered

如何为Unfiltered中的所有请求添加“Access-Control-Allow-Origin:*”?我尝试添加响应头的原始代码,以便服务器不会阻止响应:

object SimplePlan extends Plan {
  def intent = {

  case req @ POST(Path("/get_custom")) => {
    val imageByte = (new BASE64Decoder()).decodeBuffer(Body.string(req));
    val bytes = new ByteArrayInputStream(imageByte)
    val image = ImageIO.read(bytes)
    ImageIO.write(image, "png", new File("image.png"))
    Ok ~> ResponseString(IPApp.testImage("image.png"))
   }
  }
}

object SimpleServer extends App {
  val bindingIP = SocketPortBinding(host = "127.0.0.1", port = 8080)
  unfiltered.jetty.Server.portBinding(bindingIP).plan(SimplePlan).run()
}

我已尝试过以下内容,但它无效。

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

object SimplePlan extends Plan {
def intent = AllowAllOrigin {
case req @ POST(Path("/get_custom")) => {
  val imageByte = (new BASE64Decoder()).decodeBuffer(Body.string(req));
  val bytes = new ByteArrayInputStream(imageByte)
  val image = ImageIO.read(bytes)
  ImageIO.write(image, "png", new File("image.png"))
  Ok ~> ResponseString(IPApp.testImage("image.png"))
 }
}

我做错了什么?以及如何解决这个问题?我已经广泛搜索了文档,我所能找到的只是this并没有暗示解决我的问题。

0 个答案:

没有答案