2016-04-26 55 views
0

我正在使用Netty構建相互驗證的TLS連接。對等身份驗證失敗時,如何獲得對等方(客戶端)的IP地址?在Java/Netty中SSL對等驗證失敗時獲取對等IP地址

我將SslHandler添加到服務器的通道管道中,並嘗試在通道激活時獲取它。失敗發生在getPeerPrincipal()被調用並拋出異常時。我嘗試打印出對等主機並在處理異常時調用getPeerHost()。但我只得到空。

有關此的任何提示。非常感謝!

public void channelActive(final ChannelHandlerContext ctx) { 
    ctx.pipeline().get(SslHandler.class).handshakeFuture() 
      .addListener(new GenericFutureListener<Future<Channel>>() { 
       @Override 
       public void operationComplete(Future<Channel> future) { 
        try { 
         String peerX = ctx.pipeline().get(SslHandler.class).engine().getSession().getPeerPrincipal() 
           .getName(); 
        } catch (Exception e) { 
         String peerHost = ctx.pipeline().get(SslHandler.class).engine().getSession().getPeerHost(); 
         logger.error("Failed: with" + peerHost); 
         logger.error("Error in activating receiving channel", e); 
        } 
       } 
      }); 
} 

回答

1

你可以在客戶端的IP是這樣的:

Channel ch = ctx.channel(); 
String peerHost = ((java.net.InetSocketAddress)ch.remoteAddress()).getAddress().getHostAddress(); 
+0

它非常好。謝謝! – user3674571

+0

@ user3674571高興地幫助,它可以幫助我,如果你點擊接受答案:) – taoxiaopang

相關問題