`
wandejun1012
  • 浏览: 2693524 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

netty 取IP

 
阅读更多

 

核心代码:

public class ServerHandler extends SimpleChannelInboundHandler<HttpObject> {
	@Override
	public void channelRead0(ChannelHandlerContext ctx, HttpObject msg)
			throws Exception {
		if (msg instanceof HttpRequest) {
			HttpRequest mReq = (HttpRequest) msg;
			String clientIP = mReq.headers().get("X-Forwarded-For");
			if (clientIP == null) {
				InetSocketAddress insocket = (InetSocketAddress) ctx.channel()
						.remoteAddress();
				clientIP = insocket.getAddress().getHostAddress();
			}
		}
	}
}

 

 

refurl:http://www.bubuko.com/infodetail-373799.html

 

http://stackoverflow.com/questions/6520624/jboss-netty-getting-user-ip-http-request

 

http://my.oschina.net/penngo/blog/215807?fromerr=nHCixuYR

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics