我正在使用以下代码获取JAX-WS单向方法调用的客户端IP地址:
protected HttpServletRequest getServletRequest() { MessageContext ctx = wsContext.getMessageContext(); return (HttpServletRequest) ctx.get( MessageContext.SERVLET_REQUEST ); } protected synchronized String getClientIp() { String clientIp = ""; HttpServletRequest request = getServletRequest(); if ( request != null ) { clientIp = request.getRemoteAddr(); // Handle proxy String header = request.getHeader( "x-forwarded-for" ); if( header != null && !header.isEmpty() ) { clientIp = header.split( "," )[0]; } } return clientIp; }
当多个客户端连接时,为请求检索的IP地址偶尔会从较早的请求中缓存不正确的值.获得单向呼叫发起人IP地址的可靠方法是什么?
解决方法
您可以使用以下代码段来查找客户端IP
Message message = PhaseInterceptorChain.getCurrentMessage(); HttpServletRequest request = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST); request.getRemoteAddr()