org.springframework.web.servlet.HandlerInterceptor.preHandle()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(7.9k)|赞(0)|评价(0)|浏览(186)

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

HandlerInterceptor.preHandle介绍

[英]Intercept the execution of a handler. Called after HandlerMapping determined an appropriate handler object, but before HandlerAdapter invokes the handler.

DispatcherServlet processes a handler in an execution chain, consisting of any number of interceptors, with the handler itself at the end. With this method, each interceptor can decide to abort the execution chain, typically sending a HTTP error or writing a custom response.

Note: special considerations apply for asynchronous request processing. For more details see org.springframework.web.servlet.AsyncHandlerInterceptor.

The default implementation returns true.
[中]拦截处理程序的执行。在HandlerMapping确定适当的处理程序对象之后,但在HandlerAdapter调用处理程序之前调用。
DispatcherServlet处理执行链中的处理程序,该执行链由任意数量的拦截器组成,处理程序本身位于末尾。使用此方法,每个拦截器可以决定中止执行链,通常发送HTTP错误或编写自定义响应。
注意:特殊注意事项适用于异步请求处理。有关更多详细信息,请参阅org。springframework。网状物servlet。AsyncHandlerInterceptor。
默认实现返回true。

代码示例

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

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {
  return this.interceptor.preHandle(request, response, handler);
}

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

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {
  return this.interceptor.preHandle(request, response, handler);
}

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

/**
 * Apply preHandle methods of registered interceptors.
 * @return {@code true} if the execution chain should proceed with the
 * next interceptor or the handler itself. Else, DispatcherServlet assumes
 * that this interceptor has already dealt with the response itself.
 */
boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
  HandlerInterceptor[] interceptors = getInterceptors();
  if (!ObjectUtils.isEmpty(interceptors)) {
    for (int i = 0; i < interceptors.length; i++) {
      HandlerInterceptor interceptor = interceptors[i];
      if (!interceptor.preHandle(request, response, this.handler)) {
        triggerAfterCompletion(request, response, null);
        return false;
      }
      this.interceptorIndex = i;
    }
  }
  return true;
}

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

/**
 * Apply preHandle methods of registered interceptors.
 * @return {@code true} if the execution chain should proceed with the
 * next interceptor or the handler itself. Else, DispatcherServlet assumes
 * that this interceptor has already dealt with the response itself.
 */
boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
  HandlerInterceptor[] interceptors = getInterceptors();
  if (!ObjectUtils.isEmpty(interceptors)) {
    for (int i = 0; i < interceptors.length; i++) {
      HandlerInterceptor interceptor = interceptors[i];
      if (!interceptor.preHandle(request, response, this.handler)) {
        triggerAfterCompletion(request, response, null);
        return false;
      }
      this.interceptorIndex = i;
    }
  }
  return true;
}

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

private void verifyWebInterceptor(HandlerInterceptor interceptor,
    TestWebRequestInterceptor webInterceptor) throws Exception {
  assertTrue(interceptor instanceof WebRequestHandlerInterceptorAdapter);
  interceptor.preHandle(this.request, this.response, null);
  assertTrue(webInterceptor.preHandleInvoked);
}

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

private HandlerExecutionChain getHandler(HandlerMapping hm, MockHttpServletRequest req) throws Exception {
  HandlerExecutionChain hec = hm.getHandler(req);
  HandlerInterceptor[] interceptors = hec.getInterceptors();
  if (interceptors != null) {
    for (HandlerInterceptor interceptor : interceptors) {
      interceptor.preHandle(req, null, hec.getHandler());
    }
  }
  return hec;
}

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

private HandlerExecutionChain getHandler(MockHttpServletRequest req) throws Exception {
  HandlerExecutionChain hec = hm.getHandler(req);
  HandlerInterceptor[] interceptors = hec.getInterceptors();
  if (interceptors != null) {
    for (HandlerInterceptor interceptor : interceptors) {
      interceptor.preHandle(req, null, hec.getHandler());
    }
  }
  return hec;
}

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

@Test
public void preHandle_returnsTrue() throws Exception {
 HttpServletRequest request = request().build();
 assertThat(interceptor.preHandle(request, null, null))
   .isTrue();
}

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

@Test
public void preHandle_logsInWithUserNameAndPasswordFromRequestHeaders() throws Exception {
 HttpServletRequest request = request()
   .withHeader(USER_NAME, "expected user-name")
   .withHeader(PASSWORD, "expected password")
   .build();
 interceptor.preHandle(request, null, null);
 Properties expectedLoginProperties = new Properties();
 expectedLoginProperties.setProperty(USER_NAME, "expected user-name");
 expectedLoginProperties.setProperty(PASSWORD, "expected password");
 verify(securityService, times(1)).login(expectedLoginProperties);
}

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

for (Iterator i = interceptors.iterator(); i.hasNext(); ) {
  HandlerInterceptor interceptor = (HandlerInterceptor) i.next();
  interceptor.preHandle(request, response, dispatcher);

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

for (Iterator i = interceptors.iterator(); i.hasNext(); ) {
  HandlerInterceptor interceptor = (HandlerInterceptor) i.next();
  interceptor.preHandle(request, response, dispatcher);

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

@Test
public void preHandle_createsNewEnvironmentInstance() throws Exception {
 HttpServletRequest request = request().build();
 Map<String, String> environmentBeforePreHandle = LoginHandlerInterceptor.getEnvironment();
 interceptor.preHandle(request, null, null);
 assertThat(LoginHandlerInterceptor.getEnvironment())
   .isNotSameAs(environmentBeforePreHandle);
}

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

@Test
public void afterCompletion_clearsTheEnvironment() throws Exception {
 HttpServletRequest request = request()
   .withParameter(ENVIRONMENT_VARIABLE_REQUEST_PARAMETER_PREFIX + "variable", "value")
   .build();
 // Call preHandle() to put values into the environment
 interceptor.preHandle(request, null, null);
 interceptor.afterCompletion(request, null, null, null);
 assertThat(LoginHandlerInterceptor.getEnvironment())
   .isEmpty();
}

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

.isEmpty();
interceptor.preHandle(request, null, null);

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

@Test
public void preHandle() throws Exception {
  HandlerInterceptor interceptor = mock(HandlerInterceptor.class);
  MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/**" }, interceptor);
  mappedInterceptor.preHandle(mock(HttpServletRequest.class), mock(HttpServletResponse.class), null);
  then(interceptor).should().preHandle(any(HttpServletRequest.class), any(HttpServletResponse.class), any());
}

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

@Test
public void preHandle_copiesPrefixedRequestParametersIntoEnvironment() throws Exception {
 HttpServletRequest request = request()
   .withParameter(ENVIRONMENT_VARIABLE_REQUEST_PARAMETER_PREFIX + "prefixed", "prefixed value")
   .withParameter("not-prefixed", "not-prefixed value")
   .build();
 interceptor.preHandle(request, null, null);
 assertThat(LoginHandlerInterceptor.getEnvironment())
   .containsOnly(entry("prefixed", "prefixed value"))
   .doesNotContain(entry("not-prefixed", "not-prefixed value"));
}

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

interceptor.preHandle(request, response, chain.getHandler());

代码示例来源:origin: stackoverflow.com

handlerMapping.getHandler(request).getInterceptors();
for (HandlerInterceptor interceptor : interceptors) {
  final boolean carryOn = interceptor.preHandle(request, response, controller);
  if (!carryOn) {
    return null;

代码示例来源:origin: apache/servicemix-bundles

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {
  return this.interceptor.preHandle(request, response, handler);
}

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

assertTrue(handlerInterceptor.preHandle(request, response, handler));
assertTrue(handlerInterceptor.preHandle(request, response, handler));

相关文章