Get the ip address of the request in ninja framework

132 views Asked by At

I have the following frameworks in my app

Ninja, Akka, Spray

How do I get the client's request's ip address ?

My routing is defined as follows:

    router.GET.route(urlPrefix + "/method").`with`(classOf[Controller],"method")
1

There are 1 answers

0
Piotr Kuciapski On

I'm not sure whether your question refers to www.ninjaframework.org or spray.io, but if you're using the former, you can get the remote IP address in your controller method:

public Result method(Context context) {
    String ip = context.getRemoteAddr();
    // ...
}

As the documentation states (http://www.ninjaframework.org/apidocs/ninja/Context.html#getRemoteAddr--), you have to set the ninja.ninja.x_forwarded_for_enabled flag to true in your application.conf if your app is running behind a http proxy or load balancer.