获取客户端(访问者)ip地址

 1  /**
 2      * 获取客户端ip地址(可以穿透代理)
 3      4      * @param request
 5      * @return
 6      */
 7 
 8     @RequestMapping("getRemoteAddr")
 9     public String getRemoteAddr(HttpServletRequest request) {
10         String ip = request.getHeader("X-Forwarded-For");
11         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
12             ip = request.getHeader("Proxy-Client-IP");
13         }
14         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
15             ip = request.getHeader("WL-Proxy-Client-IP");
16         }
17         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
18             ip = request.getHeader("HTTP_CLIENT_IP");
19         }
20         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
21             ip = request.getHeader("HTTP_X_FORWARDED_FOR");
22         }
23         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
24             ip = request.getRemoteAddr();
25         }
26         return ip;
27     }

 

posted @ 2023-05-16 14:27  勤快的懒羊羊  阅读(26)  评论(0编辑  收藏  举报