Getting user's ip address

960 views
Skip to first unread message

razataz000

unread,
Oct 30, 2012, 4:36:52 PM10/30/12
to play-fr...@googlegroups.com
I am wondering how to get a user's ip address from play framework and scala.

I have found this but it doesn't work:

def index = Action { implicit request =>
    val address =  request.remoteAddress
}


Do I need to import a special library at the top of the file? Thanks for you help!

Michael Schleichardt

unread,
Oct 30, 2012, 6:00:56 PM10/30/12
to play-fr...@googlegroups.com
No, it should work that way.

  def index = Action {  implicit request =>
    val address =  request.remoteAddress
    Logger.info("address " + address)
    Ok(address)
  }

Brian Smith

unread,
Oct 30, 2012, 6:08:54 PM10/30/12
to play-fr...@googlegroups.com
Note that if you're using a proxy you need to set trustxforwarded=true in your conf, and make sure that the proxy supports X-Forwarded-For.

Even then it's not a guarantee that other network components don't screw about with the request, I've seen banking networks that use reverse proxy based security products with half a dozen different request headers to check in turn.

Regards

Brian

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/Fii7zZFtps4J.

To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

razataz000

unread,
Oct 31, 2012, 10:53:46 AM10/31/12
to play-fr...@googlegroups.com
This code doesn't work. And what config file do I put the configuration in?

Jay

unread,
Oct 31, 2012, 1:12:48 PM10/31/12
to play-fr...@googlegroups.com

You do something simple like this

val ipAddress = request.headers.get("X-Forwarded-For") match {
      case Some(ip: String) => ip
      case None => request.remoteAddress     

Marc Siegel

unread,
Oct 31, 2012, 3:21:52 PM10/31/12
to play-fr...@googlegroups.com
Also, if you are using Play head by some chance, there is now  constant for "X-Forwarded-For":
https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/scala/play/api/http/StandardValues.scala#L197

Brian Smith

unread,
Oct 31, 2012, 7:41:02 PM10/31/12
to play-fr...@googlegroups.com
Presumably in application.conf.  I was guessing from the the source code comment [1].

There is also info on proxying at [2].

Those indicate that you shouldn't need to add code for this if you set the config variable and the remote address is in the X-Forwarded-For header.




--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/msp896xpLwEJ.
Reply all
Reply to author
Forward
0 new messages