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:ddf.security.common.audit.SecurityLogger.java

private static void requestIpAndPortAndUserMessage(Subject subject, Message message,
        StringBuilder messageBuilder) {
    String user = getUser(subject);
    messageBuilder.append(SUBJECT).append(user);
    appendConditionalAttributes(subject, messageBuilder);

    if (message == null) {
        messageBuilder.append(" ");
    } else {/*from w w w  . j a  v  a  2s.  c o m*/
        HttpServletRequest servletRequest = (HttpServletRequest) message
                .get(AbstractHTTPDestination.HTTP_REQUEST);
        // pull out the ip and port of the incoming connection so we know
        // who is trying to get access
        if (servletRequest != null) {
            messageBuilder.append(" Request IP: ").append(servletRequest.getRemoteAddr()).append(", Port: ")
                    .append(servletRequest.getRemotePort()).append(" ");
        } else if (MessageUtils.isOutbound(message)) {
            messageBuilder.append(" Outbound endpoint: ").append(message.get(Message.ENDPOINT_ADDRESS))
                    .append(" ");
        }
    }
}

From source file:com.shz.foundation.utils.Servlets.java

/**
 * ?ip??????ip??//from   w w  w .ja v a  2  s. c om
 * @param request
 * @return
 */
public static String getClientIP(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.getRemoteAddr();
    }
    return ip;
}

From source file:com.soolr.core.web.Servlets.java

public static String getIp(HttpServletRequest request) {
    String xforward = request.getHeader("x-forwarded-for");
    // TODO shall we use a more reliable solution?
    // compare xforward with haproxy ip
    if (StringUtils.isNotEmpty(xforward)) {
        String[] split = StringUtils.split(xforward, ".");
        if (split.length == 4) {
            return xforward;
        }/*from   w w  w .  j a  v  a  2 s  . co m*/
    }
    return request.getRemoteAddr();
}

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

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

From source file:de.metas.ui.web.login.LoginRestController.java

private static MSession createMSession(final Login loginService) {
    final HttpServletRequest httpRequest = ((ServletRequestAttributes) RequestContextHolder
            .currentRequestAttributes()).getRequest();
    final HttpSession httpSess = httpRequest.getSession();
    final String webSessionId = httpSess.getId();
    ////  ww w .  jav a  2s . co m
    // final WebBrowser webBrowser = Page.getCurrent().getWebBrowser();
    String remoteAddr = httpRequest.getRemoteAddr();
    String remoteHost = httpRequest.getRemoteHost();

    //
    // Check if we are behind proxy and if yes, get the actual client IP address
    // NOTE: when configuring apache, don't forget to activate reverse-proxy mode
    // see http://www.xinotes.org/notes/note/770/
    final String forwardedFor = httpRequest.getHeader("X-Forwarded-For");
    if (!Check.isEmpty(forwardedFor)) {
        remoteAddr = forwardedFor;
        remoteHost = forwardedFor;
    }

    final LoginContext ctx = loginService.getCtx();
    final MSession sessionPO = MSession.get(ctx.getSessionContext(), remoteAddr, remoteHost, webSessionId);

    // Set HostKey
    // FIXME: commented out because this one is not working when running over websockets (i.e. HttpServletResponse does not exists)
    // see https://dev.vaadin.com/ticket/11808
    // @formatter:off
    //      final I_AD_Session session = InterfaceWrapperHelper.create(sessionPO, I_AD_Session.class);
    //      HttpCookieHostKeyStorage.createUpdateHostKey();
    //      final String hostKey = hostKeyBL.getHostKey();
    //      session.setHostKey(hostKey);
    //      InterfaceWrapperHelper.save(session);
    // @formatter:on

    // Update Login helper
    loginService.setRemoteAddr(remoteAddr);
    loginService.setRemoteHost(remoteHost);
    loginService.setWebSession(webSessionId);

    return sessionPO;
}

From source file:com.gtwm.pb.servlets.ServletUtilMethods.java

public static AppUserInfo getPublicUserForRequest(HttpServletRequest request, AuthenticatorInfo authenticator)
        throws MissingParametersException, ObjectNotFoundException {
    String internalCompanyName = request.getParameter("c");
    if (internalCompanyName == null) {
        throw new MissingParametersException("c needed to identify company");
    }// w  w  w  . j  av  a2s .  c  o m
    String userName = "public";
    String forename = "Host " + request.getRemoteHost();
    String surname = "Addr " + request.getRemoteAddr();
    return new PublicUser(authenticator, internalCompanyName, userName, surname, forename);
}

From source file:com.baifendian.swordfish.common.utils.http.HttpUtil.java

/**
 *  http  ip ?//from  w ww  . j a va  2s .c o  m
 */
public static String getClientIpAddress(HttpServletRequest request) {
    String ip = request.getHeader("X-Forwarded-For");

    if (StringUtils.isNotEmpty(ip) && !StringUtils.equalsIgnoreCase("unKnown", ip)) {
        // ????? ip  ip ? 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) && !StringUtils.equalsIgnoreCase("unKnown", ip)) {
        return ip;
    }

    return request.getRemoteAddr();
}

From source file:com.inkubator.hrm.util.IpUtil.java

public static String getIpFromRequest(HttpServletRequest request) {

    String ip;// ww w  .  j av a  2s . c o m

    boolean found = false;

    if ((ip = request.getHeader("x-forwarded-for")) != null) {
        LOGGER.info("IP Number " + ip);
        StrTokenizer tokenizer = new StrTokenizer(ip, ",");

        while (tokenizer.hasNext()) {

            ip = tokenizer.nextToken().trim();

            if (isIPv4Valid(ip) && !isIPv4Private(ip)) {

                found = true;

                break;

            }

        }

    }

    if (!found) {
        LOGGER.info("IP Number " + ip);
        ip = request.getRemoteAddr();

    }

    return ip;

}

From source file:me.xhh.utils.Util.java

/**
 * @return the client IP address found from the given request. would find the IP data set by proxy first
 *//*from   w  w w.j  a  v  a 2  s .  c om*/
public static String getClientIP(HttpServletRequest request) {
    if (request == null)
        return null;

    final String UNKNOWN_TOKEN = "unknown";
    String ip = request.getHeader("x-forwarded-for");

    if (StringUtils.isBlank(ip) || UNKNOWN_TOKEN.equalsIgnoreCase(ip))
        ip = request.getHeader("Proxy-Client-IP");

    if (StringUtils.isBlank(ip) || UNKNOWN_TOKEN.equalsIgnoreCase(ip))
        ip = request.getHeader("WL-Proxy-Client-IP");

    if (StringUtils.isBlank(ip) || UNKNOWN_TOKEN.equalsIgnoreCase(ip))
        ip = request.getRemoteAddr();

    return ip;
}

From source file:it.reply.orchestrator.config.filters.CustomRequestLoggingFilter.java

private static String getRemoteAddr(HttpServletRequest request) {
    String remoteAddress = null;/*w ww .  j a v  a 2 s . c o  m*/
    String[] forwardedAddress = Strings.nullToEmpty(request.getHeader(HttpHeaders.X_FORWARDED_FOR))
            .split(", ?");
    if (ArrayUtils.isEmpty(forwardedAddress) || Strings.nullToEmpty(forwardedAddress[0]).trim().isEmpty()) {
        remoteAddress = request.getRemoteAddr();
    } else {
        remoteAddress = forwardedAddress[0];
    }
    return remoteAddress;
}