根据akka-http extractClientIP和Documentation:
akka-http服务器引擎将Remote-Address标头添加到每个 如果相应的设置,请自动请求 akka.http.server.remote-address-header设置为on。默认情况下是 设置为关闭。
已相应地设置了application.conf
akka {
http {
server {
remote-address-header = on
}
}
}
以下测试无法从请求标头中提取Remote-Address
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.client.RequestBuilding
import akka.http.scaladsl.model._
import akka.http.scaladsl.testkit.ScalatestRouteTest
"Remote-Address added to request header by akka-http" in {
val request = RequestBuilding.Get("/").withHeaders(`User-Agent`(this.getClass.getSimpleName))
val miniRoute: Route = { ctx => ctx.complete("Always Succeed") }
request ~> miniRoute ~> check {
assert(responseAs[String] == "Always Succeed")
}
//confirm akka config setting is correct
assert(ConfigFactory.load().getString("akka.http.server.remote-address-header") == "on", "FAILED: please set application.conf: akka.http.server.remote-address-header = on")
val remoteIP: Option[HttpHeader] = request.headers.find(_.name == "Remote-Address")
assert(remoteIP.nonEmpty, "FAILED: Remote-Address missing in Request Headers")
}
问题 akka-http是否应在请求标头中自动注入 Remote-Address
?如果您可以通过编程方式设置akka.http.server.remote-address-header
配置,可以显示如何重写此测试,我也很感激。
答案 0 :(得分:1)
您遇到此问题,因为testkit的ScalatestRouteTest
未执行Remote-Address
的自动添加。它仅发生在Http()
对象级别(有关详细信息,请参阅source)。
如果您想要查看它的实际效果,可以启动正在运行的服务器
object Tmp extends App {
implicit val sys = ActorSystem("tmp")
implicit val mat = ActorMaterializer()
val route = headerValueByName("Remote-Address") { complete(_) }
Http().bindAndHandle(route, "localhost", 8080)
}
并且根据您的配置(在您的问题中是正确的),您应该获得200 OK
(如果设置为on
)或400 Bad Request
(如果设置为off
)。
答案 1 :(得分:0)
根据您的建议,我在下面进行了测试,该测试按预期工作。希望这对其他人有用。
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import akka.http.scaladsl.server.directives.{HeaderDirectives, RouteDirectives}
import com.typesafe.config.ConfigFactory
/** Verify that akka-http engine injects the "Remote-Address" header in the request headers
* if the config key "akka.http.server.remote-address-header = on"
*
* TESTING (set config on/off via command line argument)
* 1. Run the mini web server
* sbt "runMain blabla.AkkaRemoteAddressTest on"
*
* 2. Open Git-Bash and type:
* $ curl -s http://localhost:2017
* (console output) Remote-Address = 127.0.0.1:58208 (Thursday, 02 Mar 2017 - 18:54:55)
* $ curl -s http://$COMPUTERNAME:2017
* (console output) Remote-Address = 10.5.34.101:58285 (Thursday, 02 Mar 2017 - 18:57:30)
*/
object AkkaRemoteAddressTest {
def main(args: Array[String]): Unit = {
val configValue =
if (args.isEmpty) "on"
else if (args(0).toLowerCase != "on" && args(0).toLowerCase != "off") "on"
else args(0)
val miniConfig = ConfigFactory.parseString(
s"""
|akka.http.server.remote-address-header = $configValue
""".stripMargin)
implicit val sys = ActorSystem("AkkaRemoteAddressTest", miniConfig)
implicit val mat = ActorMaterializer()
val sdfmt = new java.text.SimpleDateFormat("EEEE, dd MMM yyyy - HH:mm:ss")
val route = HeaderDirectives.headerValueByName("Remote-Address") {
remaddr => RouteDirectives.complete(s"Remote-Address = $remaddr (${sdfmt.format(java.util.Calendar.getInstance.getTime)})")
}
Http().bindAndHandle(route, "0.0.0.0", 2017)
}
}