我正在使用以下代码来获取 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 地址的可靠方法是什么?