jakob
jakob

Reputation: 5995

Create custom Play2 Action to retrieve IP address from header

I'm trying to figure out a good way of retrieving the ipadress that is stored in the header for the key X-FORWARDED-FOR in the controllers of my app. My initial thought is to use Actions and ActionComposition but I'm not sure how to do this. I guess I I should in my CustomAction loop through the headers, wrap the request with a case class that takes ipadress as argument, add the ip to that case class and then access the ipadress by request.ipadress in the controller.

Is this a good way and/or how would you implement this in scala?

Upvotes: 1

Views: 324

Answers (2)

Julien Lafont
Julien Lafont

Reputation: 7877

Play can manage that behavior without tricks.

Simply add trustxforwarded=true to your application.conf, and get the ip with request. remoteAddress.

All the details are explained here: http://www.playframework.com/documentation/2.1.1/HTTPServer

Upvotes: 3

Alban Dericbourg
Alban Dericbourg

Reputation: 1636

I won't say it is a bad way. It actually seems good.

But did you notice there was a remoteAddress() method on request headers? If yes, sorry to mention it. If no, it may help you.

Upvotes: 0

Related Questions