當前位置: 首頁>>代碼示例>>Java>>正文


Java ServletRequest.getRemoteAddr方法代碼示例

本文整理匯總了Java中javax.servlet.ServletRequest.getRemoteAddr方法的典型用法代碼示例。如果您正苦於以下問題:Java ServletRequest.getRemoteAddr方法的具體用法?Java ServletRequest.getRemoteAddr怎麽用?Java ServletRequest.getRemoteAddr使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.servlet.ServletRequest的用法示例。


在下文中一共展示了ServletRequest.getRemoteAddr方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: doFilter

import javax.servlet.ServletRequest; //導入方法依賴的package包/類
/**
 * Resolves the requester hostname and delegates the request to the chain.
 * <p>
 * The requester hostname is available via the {@link #get} method.
 *
 * @param request servlet request.
 * @param response servlet response.
 * @param chain filter chain.
 *
 * @throws IOException thrown if an IO error occurrs.
 * @throws ServletException thrown if a servet error occurrs.
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  throws IOException, ServletException {
  try {
    String hostname;
    try {
      String address = request.getRemoteAddr();
      if (address != null) {
        hostname = InetAddress.getByName(address).getCanonicalHostName();
      } else {
        log.warn("Request remote address is NULL");
        hostname = "???";
      }
    } catch (UnknownHostException ex) {
      log.warn("Request remote address could not be resolved, {0}", ex.toString(), ex);
      hostname = "???";
    }
    HOSTNAME_TL.set(hostname);
    chain.doFilter(request, response);
  } finally {
    HOSTNAME_TL.remove();
  }
}
 
開發者ID:naver,項目名稱:hadoop,代碼行數:36,代碼來源:HostnameFilter.java

示例2: doFilter

import javax.servlet.ServletRequest; //導入方法依賴的package包/類
@Override
public void doFilter(ServletRequest reg, ServletResponse res, FilterChain chain) throws IOException,
        ServletException {
    RequestDispatcher reqDispatcher = reg.getRequestDispatcher("deny.html");
    String remoteIP = reg.getRemoteAddr();// 獲取訪問服務器的IP
    String[] ips = null;
    boolean isAllow = false;
    Pattern pattern = null;
    if (!StringUtils.isEmpty(notFilteredIPs)) {
        ips = notFilteredIPs.split(";");
        for (String ip : ips) {
            pattern = Pattern.compile(ip);
            if (pattern.matcher(remoteIP).find()) {
                isAllow = true;
                break;
            }
        }
    } else {
        isAllow = true;
    }
    if (isAllow) {
        // 如果不是要過濾的IP就放行,交給下一個Filter
        chain.doFilter(reg, res);
    } else {
        logger.error("訪問服務器的IP:" + remoteIP + " , 該IP地址無法訪問");
        // 如果是要過濾的IP,就通過reqDispatcher跳轉到錯誤頁麵。
        reqDispatcher.forward(reg, res);
    }
}
 
開發者ID:zhaoxi1988,項目名稱:sjk,代碼行數:30,代碼來源:IPFilter.java

示例3: serviceInvokeRequest

import javax.servlet.ServletRequest; //導入方法依賴的package包/類
private void serviceInvokeRequest(
        ServletRequest request,
        ServletResponse response,
        ObjectInputStream in,
        CopyOnReadInputStream copyOnReadInputStream) throws Exception
{
    this.methodInvocations++;
    //if (CAUSE_RANDOM_ERROR) if (Math.random() > ERROR_RATE) throw new IOException("Random error, for testing only!");
    RequestId requestId = (RequestId) in.readObject();
    Context invokeContext = ContextCache.getInstance().getOrCreateContext(requestId);
    String serviceInterface = (String) in.readObject();
    ServiceDefinition serviceDefinition = (ServiceDefinition) this.serviceMap.get(serviceInterface);
    if (serviceDefinition == null)
    {
        invokeContext.setReturnValue(new JrpipRuntimeException("JrpipServlet is not servicing "
                + serviceInterface), true);
    }
    else
    {
        OutputStreamBuilder outputStreamBuilder = serviceDefinition.getOutputStreamBuilder();
        DataOutputStream copyTo = outputStreamBuilder.newOutputStream();
        copyTo.writeByte(OutputStreamBuilder.REQUEST_HEADER);
        try
        {
            if (copyOnReadInputStream != null)
            {
                copyOnReadInputStream.startCopyingInto(copyTo);
            }
            invokeContext.setOutputStreamBuilder(outputStreamBuilder);
            boolean serviceRequest = true;
            if (serviceDefinition.isVmBound())
            {
                serviceRequest = this.checkVmBoundCall(requestId, invokeContext, serviceInterface, serviceRequest);
            }
            if (serviceRequest)
            {
                JrpipRequestContext requestContext = null;
                if (request instanceof HttpServletRequest && this.methodInterceptor != null)
                {
                    requestContext = new JrpipRequestContext(
                            requestId,
                            ((HttpServletRequest) request).getRemoteUser(),
                            request.getRemoteAddr(),
                            ((HttpServletRequest) request).getCookies());
                }

                new StreamBasedInvocator().invoke(in,
                        invokeContext,
                        serviceDefinition.getService(),
                        serviceDefinition.getMethodResolver(),
                        request.getRemoteAddr(),
                        requestId,
                        this.listeners,
                        copyTo,
                        this.methodInterceptor,
                        requestContext);
            }
        }
        finally
        {
            copyTo.close();
        }
    }
    invokeContext.writeAndLogResponse(response.getOutputStream(), requestId);
}
 
開發者ID:goldmansachs,項目名稱:jrpip,代碼行數:66,代碼來源:JrpipServlet.java

示例4: doFilter

import javax.servlet.ServletRequest; //導入方法依賴的package包/類
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

    // Only require authentication from an identity provider if the NiFi registry is running securely.
    if (!servletRequest.isSecure()) {
        // Otherwise, requests will be "authenticated" by the AnonymousIdentityFilter
        filterChain.doFilter(servletRequest, servletResponse);
        return;
    }

    if (identityProvider == null) {
        logger.warn("Identity Filter configured with NULL identity provider. Credentials will not be extracted.");
        filterChain.doFilter(servletRequest, servletResponse);
        return;
    }

    if (credentialsAlreadyPresent()) {
        logger.debug("Credentials already extracted for {}, skipping credentials extraction filter for {}",
                SecurityContextHolder.getContext().getAuthentication().getPrincipal(),
                identityProvider.getClass().getSimpleName());
        filterChain.doFilter(servletRequest, servletResponse);
        return;
    }

    logger.debug("Attempting to extract user credentials using {}", identityProvider.getClass().getSimpleName());

    try {
        AuthenticationRequest authenticationRequest = identityProvider.extractCredentials((HttpServletRequest)servletRequest);
        if (authenticationRequest != null) {
            Authentication authentication = new AuthenticationRequestToken(authenticationRequest, identityProvider.getClass(), servletRequest.getRemoteAddr());
            logger.debug("Adding credentials claim to SecurityContext to be authenticated. Credentials extracted by {}: {}",
                    identityProvider.getClass().getSimpleName(),
                    authenticationRequest);
            SecurityContextHolder.getContext().setAuthentication(authentication);
            // This filter's job, which is merely to search for and extract an identity claim, is done.
            // The actual authentication of the identity claim will be handled by a corresponding IdentityAuthenticationProvider
        }
    } catch (Exception e) {
        logger.debug("Exception occurred while extracting credentials:", e);
    }

    filterChain.doFilter(servletRequest, servletResponse);
}
 
開發者ID:apache,項目名稱:nifi-registry,代碼行數:44,代碼來源:IdentityFilter.java


注:本文中的javax.servlet.ServletRequest.getRemoteAddr方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。