首页 > 代码库 > java检测http请求的ip地址

java检测http请求的ip地址


以后再用到的话,至少能起个参考作用
java实现,struts2的Action中,依赖HttpServletRequest
 
 

package net.dookoo.web.action;
 
import java.util.Enumeration;
 
import javax.servlet.http.HttpServletRequest;
 
import com.linktong.webframework.action.BaseAction;
 
/**
 * 检测系列操作
 *
 * @author sven
 * @version 1.0(新版本号)
 * @see 参考的JavaDoc
 */
public class CheckAction extends BaseAction {
 
      @Override
      public int getObjCount() {
            // TODO Auto-generated method stub
            return 0;
      }
 
      public String getIpAddrByRequest(HttpServletRequest request) {
            // String ip = request.getHeader("x-forwarded-for");
 
            String ip = null;
            String localIP = "127.0.0.1";
                  
            // 反向代理标识不区分大小写,防止配置问题
            Enumeration enumeration = null;
            
//          enumeration = request.getHeaderNames();
//          System.out.println("header----------------");
//          while (enumeration.hasMoreElements()) {
//                String headerName = (String) enumeration.nextElement();
//                System.out.println(headerName+"-"+request.getHeader(headerName));
//          }
//          System.out.println("header----------------");
            
            enumeration = request.getHeaderNames();
            while (enumeration.hasMoreElements()) {
                  String headerName = (String) enumeration.nextElement();
                  
                  if ("cdn-src-ip".equalsIgnoreCase(headerName)) {
                        ip = request.getHeader(headerName);
//                      System.out.println("headerName:"+headerName);
                        break;
                  }
            }
//          System.out.println("cdn-src-ip:" + ip);
            
            enumeration = request.getHeaderNames();
            if (ip == null || ip.length() == 0 || (ip.equalsIgnoreCase(localIP)) || "unknown".equalsIgnoreCase(ip)) {
                  while (enumeration.hasMoreElements()) {
                        String headerName = (String) enumeration.nextElement();
                        
                        if ("x-forwarded-for".equalsIgnoreCase(headerName)) {
                              ip = request.getHeader(headerName);
//                            System.out.println("headerName:"+headerName);
                              break;
                        }
                  }
            }
//          System.out.println("x-forwarded-for:" + ip);
            
            if (ip == null || ip.length() == 0 || (ip.equalsIgnoreCase(localIP)) || "unknown".equalsIgnoreCase(ip)) {
                  ip = request.getHeader("Proxy-Client-IP");
            }
//          System.out.println("Proxy-Client-IP:" + request.getHeader("Proxy-Client-IP"));
 
            
            if (ip == null || ip.length() == 0 || (ip.equalsIgnoreCase(localIP)) || "unknown".equalsIgnoreCase(ip)) {
                  ip = request.getHeader("WL-Proxy-Client-IP");
            }
//          System.out.println("WL-Proxy-Client-IP:" + request.getHeader("WL-Proxy-Client-IP"));
 
            
            if (ip == null || ip.length() == 0 || (ip.equalsIgnoreCase(localIP)) || "unknown".equalsIgnoreCase(ip)) {
                  ip = request.getRemoteAddr();
            }
//          System.out.println("RemoteAddr:" + request.getRemoteAddr());
 
            return ip;
      }
 
      public String checkAnonymityIP() {
            try {
                  String ipAddr = getIpAddrByRequest(getRequest());
                  this.debugTrace("check", "检测到请求的ip地址为" + ipAddr);
                  // System.out.println(ipAddr);
 
                  getRequest().setAttribute("ipAddr", ipAddr);
            } catch (Throwable e) {
                  this.errorTrace("check", e.getMessage(), e);
                  getRequest().setAttribute(this.ERRORMESSAGE, this.SYSTEMERROR);
                  return this.ERROR;
            }
            return this.SUCCESS;
      }
 
}

java检测http请求的ip地址