javax.servlet.Filter.doFilter()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(159)

本文整理了Java中javax.servlet.Filter.doFilter()方法的一些代码示例,展示了Filter.doFilter()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Filter.doFilter()方法的具体详情如下:
包路径:javax.servlet.Filter
类名称:Filter
方法名:doFilter

Filter.doFilter介绍

[英]The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.

A typical implementation of this method would follow the following pattern:

  1. Examine the request
  2. Optionally wrap the request object with a custom implementation to filter content or headers for input filtering
  3. Optionally wrap the response object with a custom implementation to filter content or headers for output filtering
    • Either invoke the next entity in the chain using the FilterChain object (chain.doFilter()),
      • or not pass on the request/response pair to the next entity in the filter chain to block the request processing
  • Directly set headers on the response after invocation of the next entity in the filter chain.
    [中]由于客户端请求链末端的资源,每次请求/响应对通过链时,容器都会调用筛选器的doFilter方法。传递到此方法的FilterChain允许筛选器将请求和响应传递给链中的下一个实体。
    此方法的典型实现将遵循以下模式:
    1.检查请求
    1.可选地使用自定义实现包装请求对象,以过滤输入过滤的内容或标题
    1.可选地使用自定义实现包装响应对象,以过滤输出过滤的内容或标题
    **使用FilterChain对象(chain.doFilter())调用链中的下一个实体,
    *或者不将请求/响应对传递给过滤器链中的下一个实体以阻止请求处理
    *在调用过滤器链中的下一个实体后,直接在响应上设置头。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Actually invoke the delegate Filter with the given request and response.
 * @param delegate the delegate Filter
 * @param request the current HTTP request
 * @param response the current HTTP response
 * @param filterChain the current FilterChain
 * @throws ServletException if thrown by the Filter
 * @throws IOException if thrown by the Filter
 */
protected void invokeDelegate(
    Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
    throws ServletException, IOException {
  delegate.doFilter(request, response, filterChain);
}

代码示例来源:origin: jenkinsci/jenkins

public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    if(position==filters.length) {
      // reached to the end
      chain.doFilter(request,response);
    } else {
      // call next
      filters[position++].doFilter(request,response,this);
    }
  }
}.doFilter(request,response);

代码示例来源:origin: jersey/jersey

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
  wrappedFilter.doFilter(request, response, chain);
}

代码示例来源:origin: org.springframework/spring-web

/**
 * Actually invoke the delegate Filter with the given request and response.
 * @param delegate the delegate Filter
 * @param request the current HTTP request
 * @param response the current HTTP response
 * @param filterChain the current FilterChain
 * @throws ServletException if thrown by the Filter
 * @throws IOException if thrown by the Filter
 */
protected void invokeDelegate(
    Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
    throws ServletException, IOException {
  delegate.doFilter(request, response, filterChain);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
  public void doFilter(final ServletRequest request, final ServletResponse response)
      throws IOException, ServletException {
    if (this.currentPosition == this.additionalFilters.size()) {
      this.originalChain.doFilter(request, response);
    }
    else {
      this.currentPosition++;
      Filter nextFilter = this.additionalFilters.get(this.currentPosition - 1);
      nextFilter.doFilter(request, response, this);
    }
  }
}

代码示例来源:origin: jenkinsci/jenkins

public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    if(itr.hasNext()) {
      // call next
      itr.next().doFilter(request, response, this);
    } else {
      // reached to the end
      chain.doFilter(request,response);
    }
  }
}.doFilter(request,response);

代码示例来源:origin: apache/incubator-druid

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  throws IOException, ServletException
{
 // If there's already an auth result, then we have authenticated already, skip this.
 if (request.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT) != null) {
  chain.doFilter(request, response);
 } else {
  delegate.doFilter(request, response, chain);
 }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Pass the call on to the Filter/Servlet.
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws ServletException, IOException {
  if (this.filter != null) {
    this.filter.doFilter(request, response, this.nextFilterChain);
  }
  else {
    Assert.state(this.servlet != null, "Neither a Filter not a Servlet set");
    this.servlet.service(request, response);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Invoke registered {@link Filter Filters} and/or {@link Servlet} also saving the
 * request and response.
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
  Assert.notNull(request, "Request must not be null");
  Assert.notNull(response, "Response must not be null");
  Assert.state(this.request == null, "This FilterChain has already been called!");
  if (this.iterator == null) {
    this.iterator = this.filters.iterator();
  }
  if (this.iterator.hasNext()) {
    Filter nextFilter = this.iterator.next();
    nextFilter.doFilter(request, response, this);
  }
  this.request = request;
  this.response = response;
}

代码示例来源:origin: jenkinsci/jenkins

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  LOGGER.entering(HudsonFilter.class.getName(), "doFilter");
  // this is not the best place to do it, but doing it here makes the patch smaller.
  ((HttpServletResponse)response).setHeader("X-Content-Type-Options", "nosniff");
  // to deal with concurrency, we need to capture the object.
  Filter f = filter;
  if(f==null) {
    // Hudson is starting up.
    chain.doFilter(request,response);
  } else {
    f.doFilter(request,response,chain);
  }
}

代码示例来源:origin: org.springframework/spring-web

@Override
  public void doFilter(final ServletRequest request, final ServletResponse response)
      throws IOException, ServletException {
    if (this.currentPosition == this.additionalFilters.size()) {
      this.originalChain.doFilter(request, response);
    }
    else {
      this.currentPosition++;
      Filter nextFilter = this.additionalFilters.get(this.currentPosition - 1);
      nextFilter.doFilter(request, response, this);
    }
  }
}

代码示例来源:origin: SonarSource/sonarqube

@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
 if (iterator == null) {
  iterator = filters.iterator();
 }
 if (iterator.hasNext()) {
  iterator.next().doFilter(request, response, this);
 } else {
  chain.doFilter(request, response);
 }
}

代码示例来源:origin: openzipkin/brave

@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
  FilterChain filterChain) throws IOException, ServletException {
 servletRequest.setAttribute(Call.Factory.class.getName(), callFactory);
 delegate.doFilter(servletRequest, servletResponse, filterChain);
}

代码示例来源:origin: apache/shiro

public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
  if (chain.hasNext()) {
    Filter filter = chain.next();
    filter.doFilter(request, response, this);
  } else if (!originalCalled) {
    originalCalled = true;
    originalChain.doFilter(request, response);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
    throws IOException, ServletException {
  HttpServletRequest httpRequest = (HttpServletRequest) request;
  String requestPath = urlPathHelper.getPathWithinApplication(httpRequest);
  if (matches(requestPath)) {
    this.delegate.doFilter(request, response, filterChain);
  }
  else {
    filterChain.doFilter(request, response);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Pass the call on to the Filter/Servlet.
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws ServletException, IOException {
  if (this.filter != null) {
    this.filter.doFilter(request, response, this.nextFilterChain);
  }
  else {
    Assert.state(this.servlet != null, "Neither a Filter not a Servlet set");
    this.servlet.service(request, response);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Invoke registered {@link Filter Filters} and/or {@link Servlet} also saving the
 * request and response.
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
  Assert.notNull(request, "Request must not be null");
  Assert.notNull(response, "Response must not be null");
  Assert.state(this.request == null, "This FilterChain has already been called!");
  if (this.iterator == null) {
    this.iterator = this.filters.iterator();
  }
  if (this.iterator.hasNext()) {
    Filter nextFilter = this.iterator.next();
    nextFilter.doFilter(request, response, this);
  }
  this.request = request;
  this.response = response;
}

代码示例来源:origin: perwendel/spark

@Override
public void doHandle(
    String target,
    Request baseRequest,
    HttpServletRequest request,
    HttpServletResponse response) throws IOException, ServletException {
  HttpRequestWrapper wrapper = new HttpRequestWrapper(request);
  filter.doFilter(wrapper, response, null);
  if (wrapper.notConsumed()) {
    baseRequest.setHandled(false);
  } else {
    baseRequest.setHandled(true);
  }
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void httpBasicUnauthorizedOnDefault() throws Exception {
  // @formatter:off
  loadContext("<http>\n" +
    "		<intercept-url pattern=\"/**\" access=\"hasRole('USER')\" />\n" +
    "		<http-basic />\n" +
    "	</http>\n" +
    "\n" +
    "	<authentication-manager />");
  // @formatter:on
  this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
  assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
  assertThat(this.response.getHeader("WWW-Authenticate")).isEqualTo("Basic realm=\"Realm\"");
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void testDontProfileStagemonitorServlet() throws Exception {
  Filter filter = new HttpRequestMonitorFilter();
  final CallStackElement total = Profiler.activateProfiling("total");
  filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain());
  Profiler.stop();
  assertEquals(0, total.getChildren().size());
}

相关文章

微信公众号

最新文章

更多