在Akka-http中获取客户端IP

时间:2016-10-19 13:05:29

标签: scala dsl akka-http

我正在尝试编写一个Akka HTTP微服务(akka版本2.4.11,Scala版本2.11.8,这两个版本在撰写时都是最新版本),它们知道客户端服务的IP(即远程地址) ),我无法让它发挥作用。

我可以创建并运行一个服务,上面写着“你好!'使用这样的路线:

    val routeHello: Route = path("SayHello") {
      get {
        entity(as[String]) {
          body => complete {
            HttpResponse(entity = HttpEntity("Hello!"))
          }
        }
      }
    }

我构建了一个与上面相似的路由,这个路由被扩展,以便它知道客户端的IP地址。

我注意到我需要编辑application.conf文件并设置' remote-address-header = on'允许添加保存客户端(远程)IP地址的Remote-Address标头。如果需要,我已经这样做了。

这是路线:

    val routeHelloIp: Route = path("SayHelloIp") {
      get {
        // extractClientIp appears to be working as a filter
        // instead of an extractor - why?
        extractClientIp {
          clientIp => {
            entity(as[String]) {
              body => complete {
                HttpResponse(entity = HttpEntity("Hello!"))
              }
            }
          }
        }
      }
    }

然而,当我运行此路线时,我收到一条消息'无法找到所请求的资源。'。

在上面的示例中,看起来我的Akka-http DSL语法糖错了。如果你能把我放在正确的道路上,我将不胜感激!

编辑:

我已经尝试了以下程序来回应Ramon的有用答案。不幸的是它没有编译,我无法看到我需要做什么才能使它编译。<​​/ p>

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.Http.IncomingConnection
import java.net.InetSocketAddress
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Sink
import akka.http.scaladsl.server.Directives._
import java.net.InetSocketAddress


object TestHttp {
  def main(args: Array[String]) {

    implicit val system = ActorSystem("my-system")
    implicit val materializer = ActorMaterializer()

    // allow connections from any IP
    val interface = "0.0.0.0"

    //from the question
    def createRoute(address: InetSocketAddress) = path("SayHelloIp") {
      get {
        extractRequestEntity { entity =>
          entity(as[String]) { body =>
         complete(entity = s"Hello ${address.getAddress().getHostAddress()}")
          }
        }
      }
    }

    Http().bind(interface).runWith(Sink foreach { conn =>
  val address = conn.remoteAddress
   conn.handleWithAsyncHandler(createRoute(address))
    })
  }
}

我有以下build.sbt以确保使用最新版本的Scala和akka-http:

import sbt.Keys._

name := "Find my IP"

version := "1.0"

scalaVersion := "2.11.8"

resolvers ++= Seq(
  "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
)

libraryDependencies ++= {
  Seq(
    "com.typesafe.akka" %% "akka-actor" % "2.4.11",
    "com.typesafe.akka" %% "akka-stream" % "2.4.11",
    "com.typesafe.akka" %% "akka-http-experimental" % "2.4.11",
    "com.typesafe.akka" %% "akka-http-core" % "2.4.11"
  )
}

我收到以下编译时错误:

[error] /Users/tilopa/temp/akka-test/src/main/scala/Test.scala:24: akka.http.scaladsl.model.RequestEntity does not take parameters
[error]           entity(as[String]) { body =>
[error]                 ^
[error] /Users/tilopa/temp/akka-test/src/main/scala/Test.scala:25: reassignment to val
[error]             complete(entity = s"Hello ${address.getAddress().getHostAddress()}")
[error]                             ^
[error] two errors found
[error] (compile:compileIncremental) Compilation failed

1 个答案:

答案 0 :(得分:7)

使用extractClientIp

extractClientIp不适合您,因为发件人未指定其中一个必需的标头字段。来自documentation

  

提供X-Forwarded-For,Remote-Address或X-Real-IP的值   标头作为RemoteAddress的一个实例。

您只需在发件人中启用正确的设置:

  

akka-http服务器引擎将Remote-Address标头添加到每个   如果相应的设置,请自动请求   akka.http.server.remote-address-header设置为on。默认情况下是   设置为关闭。

通用解决方案

如果您希望这适用于任何HttpRequest,而不仅仅是具有正确标头设置的HttpRequest,那么您必须在HttpExt上使用bind方法而不是bindAndHandle

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.Http.IncomingConnection

import java.net.InetSocketAddress

implicit val actorSystem : ActorSystem = ???
implicit val actorMat = ActorMaterializer()


//alow connections from any IP
val interface = "0.0.0.0"

//from the question
def createRoute(address : InetSocketAddress) = path("SayHelloIp") {
  get {
    extractRequestEntity { entity =>
      entity(as[String]) { body =>
        complete(entity = s"Hello ${address.getAddress().getHostAddress()}")
      }
    }
  }
}

Http().bind(interface).runWith(Sink foreach { conn =>
  val address =  conn.remoteAddress

  conn.handleWithAsyncHandler(createRoute(address))
})

修改

正如评论中所述:由于akka 10.0.13使用conn.handleWith