1./**
2. * 取得客户端真实ip
3. *
4. * @param request
5. * @return 客户端真实ip
6. */
7.public String getIpAddr(HttpServletRequest request) {
8. String ip = request.getHeader("X-Forwarded-For");
9. logger.debug("1- X-Forwarded-For ip={}", ip);
10. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
11. ip = request.getHeader("Proxy-Client-IP");
12. logger.debug("2- Proxy-Client-IP ip={}", ip);
13. }
14. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
15. ip = request.getHeader("WL-Proxy-Client-IP");
16. logger.debug("3- WL-Proxy-Client-IP ip={}", ip);
17. }
18. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
19. ip = request.getHeader("HTTP_CLIENT_IP");
20. logger.debug("4- HTTP_CLIENT_IP ip={}", ip);
21. }
22. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
23. ip = request.getHeader("HTTP_X_FORWARDED_FOR");
24. logger.debug("5- HTTP_X_FORWARDED_FOR ip={}", ip);
25. }
26. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
27. ip = request.getRemoteAddr();
28. logger.debug("6- getRemoteAddr ip={}", ip);
29. }
30. logger.info("finally ip={}", ip);
31. return ip;
32.}
获得客户端IP
最新推荐文章于 2023-12-19 10:43:19 发布