spring mvc 獲取nginx下用(use)戶真實ip

  1. 新聞資訊
  2. 技術百科
行業動态 公司新聞 案例分享 技術百科

spring mvc 獲取nginx下用(use)戶真實ip

來(Come)源:奇站網絡 浏覽量:2007 發布日期: 2020-04-27

spring mvc 獲取nginx下用(use)戶真實ip

如果經過多台服務器Nginx轉發,獲取的(of)ip内容類似這(this)樣218.5.70.170, 192.168.1.1,所以(by)過濾掉空格取第1個(indivual)就是(yes)。

  1. private String getRemortIP(HttpServletRequest request) {
  2. String ip = request.getHeader("x-forwarded-for");
  3. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  4. ip = request.getHeader("Proxy-Client-IP");
  5. }
  6. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  7. ip = request.getHeader("WL-Proxy-Client-IP");
  8. }
  9. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  10. ip = request.getRemoteAddr();
  11. }
  12. ip = ip.replace(" ", "").trim();
  13. if (StringUtils.isNotBlank(ip)) {
  14. String[] ips = ip.split(",");
  15. ip = ips[0];
  16. }
  17. logService.log(request.getRequestURL().toString(), request.getMethod(), ip, 0);
  18. return ip;
  19. }
标簽: #nginx

廈門極極網絡科技有限公司

電話:13313868605

QQ:3413772931

地(land)址:廈門集美區軟件園三期


                    掃一(one)掃加我(I)咨詢