Example usage for javax.servlet.http HttpServletRequest getRemoteAddr

List of usage examples for javax.servlet.http HttpServletRequest getRemoteAddr

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getRemoteAddr.

Prototype

public String getRemoteAddr();

Source Link

Document

Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.

Usage

From source file:de.thorstenberger.taskmodel.view.AJAXSaveStudentAnnotationAction.java

public static void logPostData(HttpServletRequest request, Tasklet tasklet) {
    Map vars = request.getParameterMap();
    StringBuffer parameters = new StringBuffer();
    Iterator keys = vars.keySet().iterator();
    while (keys.hasNext()) {
        String key = (String) keys.next();
        parameters.append(key + "=" + ((String[]) vars.get(key))[0] + "\n");
    }/*w  w  w .  java 2 s . com*/
    tasklet.logPostData("StudentAnnotation: posted parameters:\n" + parameters.toString(),
            request.getRemoteAddr());
}

From source file:com.lm.lic.manager.util.GenUtil.java

/**
 * @param request/*from w w  w  .  j a  v  a 2 s  .  c  o  m*/
 * @return
 */
public static String findIpAddress(HttpServletRequest request) {
    String rip = request.getRemoteAddr();
    return rip;
}

From source file:com.iflytek.edu.cloud.frame.utils.ServiceUtil.java

public static String getRemoteAddr(HttpServletRequest request) {
    String remoteIp = request.getHeader(X_REAL_IP); //nginx????  
    if (StringUtils.hasText(remoteIp)) {
        return remoteIp;
    } else {//from ww w.  j  a  v a2  s .  com
        remoteIp = request.getHeader(X_FORWARDED_FOR);//apache???  
        if (StringUtils.hasText(remoteIp)) {
            String[] ips = remoteIp.split(",");
            for (String ip : ips) {
                if (!"null".equalsIgnoreCase(ip)) {
                    return ip;
                }
            }
        }
        return request.getRemoteAddr();
    }
}

From source file:com.mashape.galileo.agent.common.IPAddressParserUtil.java

/**
 * Returns the true IP address of the client from specified request after parsing the header's value in the following order
 *  Forwarded//ww w  .ja  v  a 2s  .  c o m
 *  X-Real-IP 
 *  X-FORWARDED-FOR
 *  Fastly-Client-IP
 *  CF-Connecting-IP
 *  X-Cluster-Client-IP
 *  X-Cluster-Client-IP
 *  Z-Forwarded-For
 *  WL-Proxy-Client-IP
 *  Proxy-Client-IP
 *  
 *  if none of above headers set fall back to remote address
 *  
 * @param request
 * @return a <code>String</code> containing the the client IP address
 */
public static String getClientIPAddress(HttpServletRequest request) {
    for (String header : headers) {
        String ipAddress = request.getHeader(header);
        if (ipAddress == null)
            break;
        if ("Forwarded".equals(header))
            ipAddress = findForwadedClientIpAddress(ipAddress);
        else if ("X-FORWARDED-FOR".equals(header))
            ipAddress = findXFFClientIpAddress(ipAddress);
        if (isValidIp(ipAddress)) {
            return ipAddress;
        }
    }
    return request.getRemoteAddr();
}

From source file:com.gdo.project.model.SessionStcl.java

/**
 * Creates and store the session stencil in HTTP session.
 * //from  ww  w  . j a v a 2s .  c  o m
 * The session is stored as a stencil and not as a plugged stencil as it
 * implements the HttpSessionBindingListener interface.
 * 
 * @param stclContext
 *            the stencil context.
 */
public static void createSessionStcl(StclContext stclContext) {
    HttpSession session = stclContext.getSession();
    HttpServletRequest request = stclContext.getRequest();

    // checks another one wasn't created before
    Stcl sessionStcl = getSessionStcl(stclContext);
    if (sessionStcl != null) {
        logError("A session stencil (%s) already exists in session %s (ip %s)", sessionStcl, session.getId(),
                request.getRemoteAddr());
    } else {
        StclFactory factory = (StclFactory) stclContext.getStencilFactory();
        sessionStcl = factory.createStencil(stclContext, SessionStcl.class);
    }

    // bounds it to the session
    ((SessionStcl) sessionStcl).createListener(stclContext, session);
}

From source file:com.djt.utils.RequestUtils.java

/**
 * ?IP?/*  w ww.j a v a  2  s .c om*/
 */
public static String getRequestIp(HttpServletRequest request) {
    String ip = request.getHeader("X-Forwarded-For");
    if (StringUtils.isNotEmpty(ip) && "unKnown".equalsIgnoreCase(ip)) {
        //?????IPIP?IP
        int index = ip.indexOf(", ");
        if (index != -1) {
            return ip.substring(0, index);
        } else {
            return ip;
        }
    }
    ip = request.getHeader("X-Real-IP");
    if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
        return ip;
    }
    return request.getRemoteAddr();
}

From source file:com.evolveum.midpoint.security.api.SecurityUtil.java

private static String getRemoteHostAddress(HttpServletRequest request) {
    for (String headerName : remoteHostAddressHeaders) {
        String header = request.getHeader(headerName);
        if (header != null) {
            return getAddressFromHeader(headerName, header);
        }//from   w  ww.j ava  2s  . c om
    }
    return request.getRemoteAddr();
}

From source file:org.jresponder.service.SubscriberService.java

/**
 * Default log entry props/*from   ww w. jav  a 2 s.  c  o  m*/
 * @return
 */
private static Map<String, Object> defaultLogEntryProps() {

    Map<String, Object> myRet = new HashMap<String, Object>();

    try {
        // use some Spring magic to get the current request
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest();
        myRet.put("ip_address", req.getRemoteAddr());
    } catch (IllegalStateException e) {
        LoggerFactory.getLogger(SubscriberService.class).debug(
                "defaultLogEntryProps() got IllegalStateException - this is normal if testing outside of web env");
    }

    return myRet;
}

From source file:com.lll.util.SpringSecurityUtils.java

public static String getIpAddr(HttpServletRequest request) {
    String ip = request.getHeader("x-forwarded-for");
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
        //System.out.println(ip);
    }//from   w w  w  . j  a va2  s  .c  om
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("WL-Proxy-Client-IP");
        //System.out.println(ip);
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
        //System.out.println(ip);
    }
    return ip;
}

From source file:com.zq.fin.seckill.web.BaseController.java

 /**
 * ??ip//from  www .ja v  a 2 s.c  om
 * @param request
 * @return
 */
public static String getIpAddr(HttpServletRequest request){
      
   String ipAddress = request.getHeader("x-forwarded-for");
   if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getHeader("Proxy-Client-IP");
   }
   if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getHeader("WL-Proxy-Client-IP");
   }
   if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getRemoteAddr();
      if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){
         //????IP
         InetAddress inet=null;
         try {
            inet = InetAddress.getLocalHost();
         } catch (UnknownHostException e) {
            e.printStackTrace();
         }
         ipAddress= inet.getHostAddress();
      }
   }
   //?IPIP,IP','
   if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
      if(ipAddress.indexOf(",")>0){
         ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
      }
   }
      
   return ipAddress; 
}