extractClientIP

Description

Provides the value of X-Forwarded-For, Remote-Address, or X-Real-IP headers as an instance of HttpIp.

The akka-http server engine adds the Remote-Address header to every request automatically if the respective setting akka.http.server.remote-address-header is set to on. Per default it is set to off.

Example

final Route route = extractClientIP(remoteAddr ->
  complete("Client's IP is " + remoteAddr.getAddress().map(InetAddress::getHostAddress)
    .orElseGet(() -> "unknown"))
);

//tests:
final String ip = "192.168.1.2";
testRoute(route).run(HttpRequest.GET("/")
    .addHeader(RemoteAddress
      .create(akka.http.javadsl.model.RemoteAddress.create(InetAddress.getByName(ip)))))
  .assertEntity("Client's IP is " + ip);

testRoute(route).run(HttpRequest.GET("/"))
  .assertEntity("Client's IP is unknown");