1. 程式人生 > >Java獲取使用者ip

Java獲取使用者ip

Java 如何獲取客戶端IP呢?
下面是我總結的幾種方法:

/**
     * 獲取客戶端ip地址(可以穿透代理)
     * 
     * @param request
     * @return
     */
    public static String getRemoteAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP"
); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); } if
(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; } private
static final String[] HEADERS_TO_TRY = { "X-Forwarded-For", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_X_FORWARDED_FOR", "HTTP_X_FORWARDED", "HTTP_X_CLUSTER_CLIENT_IP", "HTTP_CLIENT_IP", "HTTP_FORWARDED_FOR", "HTTP_FORWARDED", "HTTP_VIA", "REMOTE_ADDR", "X-Real-IP"}; /*** * 獲取客戶端ip地址(可以穿透代理) * @param request * @return */ public static String getClientIpAddress(HttpServletRequest request) { for (String header : HEADERS_TO_TRY) { String ip = request.getHeader(header); if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) { return ip; } } return request.getRemoteAddr(); } /*** * 獲取客戶端ip地址(可以穿透代理) * @param request * @return */ public static String getClientIpAddr(HttpServletRequest request) { String ip = request.getHeader("X-Forwarded-For"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_CLUSTER_CLIENT_IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_FORWARDED_FOR"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_FORWARDED"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_VIA"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("REMOTE_ADDR"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; } public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("X-Real-IP"); if (null != ip && !"".equals(ip.trim()) && !"unknown".equalsIgnoreCase(ip)) { return ip; } ip = request.getHeader("X-Forwarded-For"); if (null != ip && !"".equals(ip.trim()) && !"unknown".equalsIgnoreCase(ip)) { // get first ip from proxy ip int index = ip.indexOf(','); if (index != -1) { return ip.substring(0, index); } else { return ip; } } return request.getRemoteAddr(); }

我使用的是spring MVC框架,測試的控制器程式碼如下:

package com.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.util.SystemHWUtil;
import com.common.util.WebServletUtil;
import com.string.widget.util.ValueWidget;
import com.util.JSONPUtil;
/***
 * 用於測試跨域
 * @author huangweii
 * 2015年5月29日
 */
@Controller
@RequestMapping("/cors")
public class CORSController {
    @ResponseBody
    @RequestMapping(value = "/simple",produces=SystemHWUtil.RESPONSE_CONTENTTYPE_JSON_UTF)
    public String corsJsonSimple(HttpServletRequest request,String callback){
        String content;
        Map map=new HashMap();
        map.put("username", "黃威");
        map.put("age", "27");
        map.put("address", "beijing");
        content=HWJacksonUtils.getJsonP(map, callback);
        System.out.println("getIpAddr:"+WebServletUtil.getIpAddr(request));
        System.out.println("getRemoteAddr:"+WebServletUtil.getRemoteAddr(request));
        System.out.println("getClientIpAddr:"+WebServletUtil.getClientIpAddr(request));
        System.out.println("getClientIpAddress:"+WebServletUtil.getClientIpAddress(request));
        return content;
    }

}

測試結果:
獲取客戶端IP

json 工具類:
json 工具類

相關推薦

JAVA獲取公網ip

for 百度搜索 writer 不能 cep length 是我 沒有 form 在ipv4地址稀缺的今天,分配到公網ip幾乎是不可能的,但是我撥號之後的ip竟然是公網IP。 將自己的電腦作為服務器·,做點好玩的程序,就成為了可能。 由於運營商的ip是動態分配的公網ip的所

Java獲取使用者IP地址的工具類

package com.gcloud.common; import javax.servlet.http.HttpServletRequest; import java.net.InetAddress; import java.net.NetworkInterface; import java.n

多級反向代理java獲取真實IP地址

public static String getIpAddress(HttpServletRequest request){ String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.l

java獲取使用者ip地址,處理0:0:0:0:0:0:0:1

/** * 獲得使用者遠端地址 */ public static String getRemoteAddr(HttpServletRequest request){ /*String remoteAddr = request.getHeader("X-Real-IP"); if (is

JAVA獲取主機IP地址

JAVA獲取主機IP地址 java 獲取主機IP地址工具類 import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.contex

Java獲取伺服器IP、MAC地址

@Resource     private WebServiceContext webServiceContext;     public String getLoginUser() throws Exception {  

Java獲取真實IP地址

public String getRemortIP(HttpServletRequest request) { if (request.getHeader("x-forwarded-for") == null) { return request.getRemoteAddr(); } ret

Java-獲取當前IP歸屬地

知道IP,獲取當前IP歸屬地的Java程式:   package Main; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.i

Java獲取本地IP地址

import java.net.*; publicclass ip5 { publicstaticvoid main(String args[]) throws Exception { String ip = InetAddress.getLoc

使用JAVA獲取請求IP(訪問者的IP

/** * 獲取當前網路ip * @param request * @return */ public String getIpAddr(HttpServletRequest request){

Java獲取本地IP和計算機名

package com.bjsxt.base.bhz.nio.test; import java.net.InetAddress; import java.net.UnknownHostException; public class Test {public static

java獲取本地IP和伺服器IP

java獲取IP地址 最近公司在做日誌報警服務,並將異常資訊推送到簡聊,需要在異常資訊上新增IP地址,用於快速定位異常位置。總結以下知識點: 獲取本地IP地址 String address = InetAddress.getLocalHost().ge

Java獲取使用者ip

Java 如何獲取客戶端IP呢? 下面是我總結的幾種方法: /** * 獲取客戶端ip地址(可以穿透代理) * * @param request * @return */ public stat

java獲取本地ip

package com.tianjian.property.jieshuns.controller; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.In

JAVA獲取真實IP地址:你獲取IP地址真實嗎?

想必大家對這段程式碼並不陌生: public String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if (ip == null

JAVA 獲取請求 IP 用於白名單驗證

 獲取IP  public static String getIpAddress(ServletRequest request) {         String ip = ((HttpServletRequest) request).getHeader("x-forwar

Java獲取客戶端IP

不為 pty 獲取 code 中間 clas 代理服務器 gin servle 在開發工作中,我們常常需要獲取客戶端的IP。一般獲取客戶端的IP地址的方法是:request.getRemoteAddr();但是在通過了Apache,Squid等反向代理軟件就不能獲取到客戶端

java獲取ip

servlet req ddr 取ip地址 unknown ignorecas remote logs eth //獲取ip地址 public String getIpAddr(HttpServletRequest request) {

Java獲取服務器IP和客戶端IP

audio sta ignorecas als style param servlet controls cal 服務器IP: String addr = InetAddress.getLocalHost().getHostAddress(); 說明:很明顯上面

Java 獲取當前環境Windows/Linux 真實IP

system class AD span gpo etc ack img network public static String getCurrentIp() { try { Enumeration<NetworkInte