Example usage for javax.servlet ServletRequest getRemoteAddr

List of usage examples for javax.servlet ServletRequest getRemoteAddr

Introduction

In this page you can find the example usage for javax.servlet ServletRequest 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:org.hippoecm.frontend.util.RequestUtils.java

/**
 * Returns the remote host addresses related to this request.
 * If there's any proxy server between the client and the server,
 * then the proxy addresses are contained in the returned array.
 * The lowest indexed element is the farthest downstream client and
 * each successive proxy addresses are the next elements.
 * @param request wicket request/*from  www  . j a va2  s .c om*/
 * @return remote host addresses as non-null string array
 */
public static String[] getRemoteAddrs(final Request request) {
    if (request instanceof WebRequest) {
        WebRequest webRequest = (WebRequest) request;

        String headerName = getForwardedForHeaderName(webRequest);
        String headerValue = webRequest.getHeader(headerName);

        if (headerValue != null && headerValue.length() > 0) {
            String[] addrs = headerValue.split(",");
            for (int i = 0; i < addrs.length; i++) {
                addrs[i] = addrs[i].trim();
            }
            return addrs;
        } else if (webRequest.getContainerRequest() instanceof ServletRequest) {
            final ServletRequest servletRequest = (ServletRequest) webRequest.getContainerRequest();
            return new String[] { servletRequest.getRemoteAddr() };
        }
    }

    return ArrayUtils.EMPTY_STRING_ARRAY;
}

From source file:no.sesat.search.http.filters.SiteLocatorFilter.java

private static void logAccessRequest(final ServletRequest request) {

    final StringBuilder url = new StringBuilder();
    final String referer;
    final String method;
    final String ip = request.getRemoteAddr();
    final String userAgent;
    final String sesamId;
    final String sesamUser;

    if (request instanceof HttpServletRequest) {

        final HttpServletRequest req = (HttpServletRequest) request;
        url.append(req.getRequestURI() + (null != req.getQueryString() ? '?' + req.getQueryString() : ""));
        referer = req.getHeader("Referer");
        method = req.getMethod();/*from   www .jav a 2s  .  c o m*/
        userAgent = req.getHeader("User-Agent");
        sesamId = getCookieValue(req, "SesamID");
        sesamUser = getCookieValue(req, "SesamUser");

    } else {

        for (@SuppressWarnings("unchecked")
        Enumeration<String> en = request.getParameterNames(); en.hasMoreElements();) {

            final String param = en.nextElement();
            url.append(param + '=' + request.getParameter(param));
            if (en.hasMoreElements()) {
                url.append('&');
            }
        }
        referer = method = userAgent = sesamId = sesamUser = UNKNOWN;
    }

    ACCESS_LOG.info("<request>" + "<url method=\"" + method + "\">"
            + StringEscapeUtils.escapeXml(url.toString()) + "</url>"
            + (null != referer ? "<referer>" + StringEscapeUtils.escapeXml(referer) + "</referer>" : "")
            + "<browser ipaddress=\"" + ip + "\">" + StringEscapeUtils.escapeXml(userAgent) + "</browser>"
            + "<user id=\"" + sesamId + "\">" + sesamUser + "</user>" + "</request>");
}

From source file:org.apache.sling.discovery.base.connectors.ping.wl.SubnetWhitelistEntry.java

public boolean accepts(ServletRequest request) {
    final String remoteAddr = request.getRemoteAddr();
    return subnetInfo.isInRange(remoteAddr);
}

From source file:nl.ctrlaltdev.harbinger.filter.BlacklistFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (!ctx.isBlacklisted(request.getRemoteAddr(), Instant.now())) {
        chain.doFilter(request, response);
    } else if (response instanceof HttpServletResponse) {
        ((HttpServletResponse) response).sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
    }// w  w w . jav a 2  s. c  o  m
}

From source file:de.zib.gndms.gndms.security.LocalRequestSkippingChannelProcessingFilter.java

@Override
public void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain)
        throws IOException, ServletException {

    final String remoteAddr = req.getRemoteAddr();
    logger.info("request from: " + remoteAddr);
    if (remoteAddr.equals(req.getLocalAddr()))
        chain.doFilter(req, res);//from  w  w w  .  ja  va 2s  .co  m
    else
        super.doFilter(req, res, chain); // overriden method implementation
}

From source file:MyServlet.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    long start = System.currentTimeMillis();
    String address = request.getRemoteAddr();
    String file = ((HttpServletRequest) request).getRequestURI();

    chain.doFilter(request, response);/*ww  w  .  j  a va 2 s.  co m*/

    filterConfig.getServletContext().log("User access! " + " User IP: " + address + " Resource: " + file
            + " Milliseconds used: " + (System.currentTimeMillis() - start));
}

From source file:ro.cs.cm.web.filter.GeneralFilter.java

private void log(ServletRequest req) {
    logger.debug("-------------------------------------------------------------");
    logger.debug(req.getRemoteHost() + "(" + req.getRemoteAddr() + "):" + req.getRemotePort());
}

From source file:IPFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    String ip = request.getRemoteAddr();

    HttpServletResponse httpResp = null;

    if (response instanceof HttpServletResponse)
        httpResp = (HttpServletResponse) response;

    StringTokenizer toke = new StringTokenizer(ip, ".");
    int dots = 0;
    String byte1 = "";
    String byte2 = "";
    String client = "";

    while (toke.hasMoreTokens()) {

        ++dots;//w  w  w .  j  av  a  2 s . c  o  m

        //if we've reached the second dot, break and check out the indx
        // value
        if (dots == 1) {

            byte1 = toke.nextToken();

        } else {

            byte2 = toke.nextToken();
            break;
        }
    } //while

    //Piece together half of the client IP address so it can be compared
    // with
    //the forbidden range represented by IPFilter.IP_RANGE
    client = byte1 + "." + byte2;

    if (IP_RANGE.equals(client)) {

        httpResp.sendError(HttpServletResponse.SC_FORBIDDEN, "That means goodbye forever!");

    } else {

        chain.doFilter(request, response);
    }

}

From source file:org.viafirma.conector.security.SecurityServiceWebHandler.java

/**
 * Comprueba que las ips que acceden a la aplicacin son efectivamente ip
 * permitidas./*from   w w w  . j  a  v a 2 s  . c  o m*/
 * 
 * @see javax.xml.ws.handler.Handler#handleMessage(javax.xml.ws.handler.MessageContext)
 */
public boolean handleMessage(MessageHandlerContext context) {
    ServletRequest servletRequest = ((ServletRequest) context.get(MessageContext.SERVLET_REQUEST));
    String remoteAddres = servletRequest.getRemoteAddr();
    if (ipsAllowedList == null) {
        ipCacheMaker(context, servletRequest);
    }
    String auxRemoteAddres = "";
    String auxIpAllowed = "";
    boolean allow = false;
    for (String ipAllowed : ipsAllowedList) {
        if (ipAllowed.contains("*") && allow == false) {
            int astPosition = ipAllowed.indexOf("*");
            auxRemoteAddres = remoteAddres.substring(0, astPosition);
            auxIpAllowed = ipAllowed.substring(0, astPosition);
            if (auxIpAllowed.equals(auxRemoteAddres)) {
                if (log.isInfoEnabled())
                    log.info("Servicio Web solicitado desde ip: " + remoteAddres);
                allow = true;
            }
        } else {
            if (ipAllowed.equals(remoteAddres) && allow == false) {
                if (log.isInfoEnabled())
                    log.info("Servicio Web solicitado desde ip: " + remoteAddres);
                allow = true;
            }
        }
    }
    if (!allow) {
        log.error("Acceso denegado. La ip " + remoteAddres + " no tiene permiso para acceder a los WS.");
        throw new WebServiceException(
                "Acceso denegado. La ip " + remoteAddres + " no tiene permiso para acceder a los WS.");
    }

    return true;

}

From source file:org.everit.jetty.server.ecm.tests.EchoRemoteInfoServlet.java

@Override
public void service(final ServletRequest req, final ServletResponse res) throws ServletException, IOException {
    PrintWriter writer = res.getWriter();

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("remoteAddr", req.getRemoteAddr());
    jsonObject.put("remoteHost", req.getRemoteHost());
    jsonObject.put("remotePort", req.getRemotePort());
    jsonObject.put("serverName", req.getServerName());
    jsonObject.put("serverPort", req.getServerPort());
    jsonObject.put("protocol", req.getProtocol());
    jsonObject.put("secure", req.isSecure());

    writer.write(jsonObject.toString());

}