lighttpd -> tomcat環境でrequest.getRemoteAddr
アプリ側でIP制限等かけている場合、lighttpd -> tomcatとくると
リモートアドレスが127.0.0.1等になってしまう(proxyサーバのIP)。
クライアントのIPを取得するにはHTTP_X_FORWARD_FORを取れば良いらしい。
request.getRemoteAddr("x-forwarded-for");
javaで-つなぎの小文字みたい。
proxy環境でrequestのヘッダーを取れば出てくる。
ヘッダー一覧取得は
http://www.javadrive.jp/servlet/request/index9.html
に教えてもらいましたo(_ _)o
request.getRemoteAddr()をすべきところで、書き換えてくれる人が欲しかったので
以下のようにした。
/**
*
* @author kapi
*/
import javax.servlet.http.HttpServletRequest;
public class RequestDecorator {
private String remoteAddr;
private String http_x_forward_for;
public RequestDecorator(HttpServletRequest request){
remoteAddr = request.getRemoteAddr();
http_x_forward_for = request.getHeader("x-forwarded-for");
}
public String getRemoteIp(){
if ( remoteAddr.equals("127.0.0.1") ) {
if ( (http_x_forward_for!=null) && http_x_forward_for.length()==0 ){
remoteAddr = http_x_forward_for;
}
}
return remoteAddr;
}
}
呼び出し側は
String remoteAddr = new RequestDecorator(request).getRemoteIp();
な感じで。
参考URL
http://kapi.jp/kapi_blog/211
2009年02月06日
関連カテゴリ JAVA