第一步:在nginx.conf中配置反向代理时把真实IP带上,例如:

server {
    listen 80;
    server_name  boyan.com;
    location ~ ^/(WEB-INF)/ {
        deny all;
     }

    location / {
      proxy_pass http://localhost:8888; 
      proxy_set_header  X-Real-IP  $remote_addr;
    }
  }

第二步:应用程序中用 String ip = request.getHeader("X-Real-IP");替代String ip = request.getRemoteAddr();即可