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:com.aistor.modules.cms.web.front.FrontGuestbookController.java

/**
 * ?-??//from w  w w .j ava  2 s  .c o m
 */
@RequestMapping(value = "guestbook", method = RequestMethod.POST)
public String guestbookSave(Guestbook guestbook, String validateCode, HttpServletRequest request,
        HttpServletResponse response, RedirectAttributes redirectAttributes) {
    if (StringUtils.isNotBlank(validateCode)) {
        if (ValidateCodeServlet.validate(request, validateCode)) {
            guestbook.setIp(request.getRemoteAddr());
            guestbook.setCreateDate(new Date());
            guestbook.setStatus(Guestbook.STATUS_AUDIT);
            guestbookService.save(guestbook);
            addMessage(redirectAttributes, "????");
        } else {
            addMessage(redirectAttributes, "???");
        }
    } else {
        addMessage(redirectAttributes, "???");
    }
    return "redirect:" + Global.FRONT_PATH + "/guestbook";
}

From source file:com.hongqiang.shop.modules.cms.web.front.FrontGuestbookController.java

/**
 * ?-??//  w w w.  j a va 2  s . c om
 */
@RequestMapping(value = "guestbook", method = RequestMethod.POST)
public String guestbookSave(Guestbook guestbook, String validateCode, HttpServletRequest request,
        HttpServletResponse response, RedirectAttributes redirectAttributes) {
    if (StringUtils.isNotBlank(validateCode)) {
        if (ValidateCodeServlet.validate(request, validateCode)) {
            guestbook.setIp(request.getRemoteAddr());
            guestbook.setCreateDate(new Date());
            guestbook.setDelFlag(Guestbook.DEL_FLAG_AUDIT);
            guestbookService.save(guestbook);
            addMessage(redirectAttributes, "????");
        } else {
            addMessage(redirectAttributes, "???");
        }
    } else {
        addMessage(redirectAttributes, "???");
    }
    return "redirect:" + Global.getFrontPath() + "/guestbook";
}

From source file:edu.internet2.middleware.shibboleth.idp.StatusServlet.java

/**
 * Checks whether the client is authenticated.
 * //from www. j a va 2 s  .  c o  m
 * @param request client request
 * 
 * @return true if the client is authenticated, false if not
 */
protected boolean isAuthenticated(HttpServletRequest request) throws ServletException {
    log.debug("Attempting to authenticate client '{}'", request.getRemoteAddr());
    try {
        InetAddress clientAddress = InetAddress.getByName(request.getRemoteAddr());

        for (IPRange range : allowedIPs) {
            if (range.contains(clientAddress)) {
                return true;
            }
        }

        return false;
    } catch (UnknownHostException e) {
        throw new ServletException(e);
    }
}

From source file:org.uaa.security.core.LoggerManager.java

public void logBefore(HttpServletRequest request) {
    res_uri = request.getPathInfo();/*from   w ww .j  a v a 2  s  . c  om*/
    if (res_uri.startsWith("/logs"))
        return;

    String res_action = request.getMethod();
    request_time = new Timestamp(System.currentTimeMillis());
    String addr = request.getRemoteAddr();
    String client = request.getHeader("User-Agent");

    log = new Apilog();
    log.setAddr(addr);
    log.setClient(client);
    log.setRequest_time(request_time.getTime());
    log.setRes_action(res_action);
    log.setRes_uri(res_uri);

    securityService.insertLog(log);
}

From source file:com.green.modules.cms.web.front.FrontGuestbookController.java

/**
 * ?-??//from  w  ww .  j av  a  2s . c  o m
 */
@RequestMapping(value = "", method = RequestMethod.POST)
public String guestbookSave(Guestbook guestbook, String validateCode, HttpServletRequest request,
        HttpServletResponse response, RedirectAttributes redirectAttributes) {
    if (StringUtils.isNotBlank(validateCode)) {
        if (ValidateCodeServlet.validate(request, validateCode)) {
            guestbook.setIp(request.getRemoteAddr());
            guestbook.setCreateDate(new Date());
            guestbook.setDelFlag(Guestbook.DEL_FLAG_AUDIT);
            guestbookService.save(guestbook);
            addMessage(redirectAttributes, "????");
        } else {
            addMessage(redirectAttributes, "???");
        }
    } else {
        addMessage(redirectAttributes, "???");
    }
    return "redirect:" + Global.getFrontPath() + "/guestbook?type=" + guestbook.getType();
}

From source file:com.starit.diamond.server.aop.OperationLogAspect.java

private String getRemoteIp(HttpServletRequest request) {
    String remoteIP = request.getHeader("X-Real-IP");
    if (remoteIP == null || remoteIP.isEmpty()) {
        remoteIP = request.getRemoteAddr();
    }//from w  w w  .j  av  a2  s.co m
    return remoteIP;
}

From source file:com.jaspersoft.jasperserver.api.security.externalAuth.custom.CustomAuthenticationProcessingFilter.java

public final Authentication attemptAuthentication(final HttpServletRequest request)
        throws AuthenticationException {

    if (obtainUsername(request) != null || obtainPassword(request) != null) {
        return super.attemptAuthentication(request);
    } else {/*from w ww  .  j av  a2s .c  o m*/

        String ip = request.getRemoteAddr();
        // doing this replace since we have a bug winding folders that look like IP addresses. the get folder does not find it. see bug 31104
        ip = ip.replace(".", "_");

        GrantedAuthority grantedAuthority;

        grantedAuthority = new GrantedAuthorityImpl("ROLE_CUSTOM_AUTH_IP");

        List<GrantedAuthority> authorities = new LinkedList<GrantedAuthority>();
        authorities.add(grantedAuthority);

        UserDetails ud = new ExternalUserDetails(ip, "", authorities.toArray(new GrantedAuthority[0]));

        final CustomAuthenticationToken authToken = new CustomAuthenticationToken(ud, "");
        return this.getAuthenticationManager().authenticate(authToken);
    }
}

From source file:com.binarybirchtree.filters.IpFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {
    // Do not block localhost.
    if (request.getRemoteAddr().equals("127.0.0.1")) {
        filterChain.doFilter(request, response);
        return;/*from   w  w w  .j ava 2 s .co m*/
    }

    boolean allowed = true;

    // Refuse connections that circumvented Cloudflare.
    // Checking the latest IP from the X-Forwarded-For header on Heroku, since request.getRemoteAddr() seems to return an IP in Heroku's internal network.
    if (!ipIsInList(request.getRemoteAddr(), internalIps) || (getForwardedIp(request, 1) != null
            && !ipIsInList(getForwardedIp(request, 1), bypassCloudflareIps)
            && !ipIsInList(getForwardedIp(request, 1), cloudflareIps)))
        allowed = false;

    if (allowed) {
        // Check if the IP before Cloudflare is blacklisted.
        String proxiedIp = getForwardedIp(request, 2);

        if (proxiedIp != null) {
            for (String ip : blacklistIps) {
                SubnetUtils subnet = new SubnetUtils(ip);
                subnet.setInclusiveHostCount(true);
                if (!subnet.getInfo().isInRange(proxiedIp)) {
                    allowed = false;
                    break;
                }
            }
        }
    }

    // If the request failed one of the tests, send an error response and do not continue processing the request.
    if (!allowed) {
        response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        return;
    }

    // If the request passed the tests, allow it to be processed normally.
    filterChain.doFilter(request, response);
}

From source file:com.github.wxiaoqi.gate.ratelimit.filters.RateLimitFilter.java

private String getRemoteAddr(final HttpServletRequest request) {
    if (properties.isBehindProxy() && request.getHeader(HttpHeaders.X_FORWARDED_FOR) != null) {
        return request.getHeader(HttpHeaders.X_FORWARDED_FOR);
    }//from  w  w  w.jav  a2  s.c o m
    return request.getRemoteAddr();
}

From source file:com.enonic.cms.business.log.LogServiceImpl.java

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public LogEntryKey storeNew(StoreNewLogEntryCommand command) {
    Assert.notNull(command.getType(), "type cannot be nul");
    Assert.notNull(command.getUser(), "user cannot be nul");
    Assert.notNull(command.getTitle(), "title cannot be nul");

    HttpServletRequest httpRequest = ServletRequestAccessor.getRequest();
    String clientInetAddress = null;
    if (httpRequest != null) {
        clientInetAddress = httpRequest.getRemoteAddr();
    }/*from w  w w. j a v a2  s.  com*/

    UserEntity user = userDao.findByKey(command.getUser());

    LogEntryEntity logEntry = new LogEntryEntity();
    logEntry.setType(command.getType().asInteger());
    logEntry.setTimestamp(timeService.getNowAsDateTime().toDate());
    logEntry.setInetAddress(clientInetAddress);
    logEntry.setUser(user);
    logEntry.setTableKey(command.getTable().asInteger());
    logEntry.setKeyValue(command.getTableKeyValue());
    logEntry.setTitle(command.getTitle());

    // logEntry.setSite( .. );

    logEntry.setPath(enshurePathWithinBoundary(command.getPath()));

    if (command.getXmlData() != null) {
        logEntry.setXmlData(command.getXmlData());
    }

    else

    {
        logEntry.setXmlData(createEmptyXmlData());
    }

    logEntryDao.storeNew(logEntry);
    return logEntry.getKey();
}