While this sounds silly, I haven't been able to find a way to get the IP of the client in Play 2.0. It was available in 1.2.x as Http.Request.remoteAddress
, but 2.0 (I primarily checked the Scala API, but I did a quick check of the Java one too) only seems to provide information about the server side of the request in play(.api).mvc.Request
. Is there something I'm missing? The closest hack I can think of right now is setting it up behind a reverse proxy and then checking X-Forwarded-For
, but this doesn't work too well in dev.
问题:
回答1:
EDIT:Coming back to this answer, this was added in playframework 2.0.2: see release announcement on https://groups.google.com/forum/#!msg/play-framework/Z97GQ2VnR5M/T-STGaeuN68J%5B1-25%5D and more importantly the API documentation at http://www.playframework.org/documentation/api/2.0.2/scala/index.html#play.api.mvc.Request
As you can see there is now support for Request.remoteAddress
Original:
There is currently a bug open for this in the tracker: https://play.lighthouseapp.com/projects/82401/tickets/256-add-back-requestheaderremoteaddress
I think that hacking the play2.0 libraries to include the linked patch is currently the best bet. It is probably still cleaner than to use a proxy with the sole purpose of adding XFF headers, and using those to determine the IP.
回答2:
Please check this Github project at https://github.com/orefalo/play2-xforward
I took the code from Play1 and ported it to Play2, it should work the same.
Still a work in progress...