开发者

JBoss Netty getting User IP (Http Request)

开发者 https://www.devze.com 2023-03-16 08:35 出处:网络
I need to log the user ip ady\'s for every request to our JBoss Netty server. I thought: MessageEvent e;

I need to log the user ip ady's for every request to our JBoss Netty server. I thought:

MessageEvent e;
e.getChannel().getRemoteAddress();

was the correct answer, but this always returns 127.0.0.1 and I need the actual clien开发者_如何学Got ip. Coming from Rails I checked how they find out the ip, from the docu:

Determines originating IP address. REMOTE_ADDR is the standard but will fail if the user is behind a proxy. HTTP_CLIENT_IP and/or HTTP_X_FORWARDED_FOR are set by proxies so check for these if REMOTE_ADDR is a proxy. HTTP_X_FORWARDED_FOR may be a comma- delimited list in the case of multiple chained proxies; the last address which is not trusted is the originating IP.

So should I check for all the headers in Netty or is there an easier way?


Ok I have the answer. Using ChannelHandlerContext instead of MessageEvent does the trick.

SocketAddress remoteAddress = ctx.getChannel().getRemoteAddress();
0

精彩评论

暂无评论...
验证码 换一张
取 消