本文整理了Java中org.springframework.web.servlet.HandlerInterceptor
类的一些代码示例,展示了HandlerInterceptor
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HandlerInterceptor
类的具体详情如下:
包路径:org.springframework.web.servlet.HandlerInterceptor
类名称:HandlerInterceptor
[英]Workflow interface that allows for customized handler execution chains. Applications can register any number of existing or custom interceptors for certain groups of handlers, to add common preprocessing behavior without needing to modify each handler implementation.
A HandlerInterceptor gets called before the appropriate HandlerAdapter triggers the execution of the handler itself. This mechanism can be used for a large field of preprocessing aspects, e.g. for authorization checks, or common handler behavior like locale or theme changes. Its main purpose is to allow for factoring out repetitive handler code.
In an asynchronous processing scenario, the handler may be executed in a separate thread while the main thread exits without rendering or invoking the postHandle and afterCompletion callbacks. When concurrent handler execution completes, the request is dispatched back in order to proceed with rendering the model and all methods of this contract are invoked again. For further options and details see org.springframework.web.servlet.AsyncHandlerInterceptor
Typically an interceptor chain is defined per HandlerMapping bean, sharing its granularity. To be able to apply a certain interceptor chain to a group of handlers, one needs to map the desired handlers via one HandlerMapping bean. The interceptors themselves are defined as beans in the application context, referenced by the mapping bean definition via its "interceptors" property (in XML: a <list> of <ref>).
HandlerInterceptor is basically similar to a Servlet Filter, but in contrast to the latter it just allows custom pre-processing with the option of prohibiting the execution of the handler itself, and custom post-processing. Filters are more powerful, for example they allow for exchanging the request and response objects that are handed down the chain. Note that a filter gets configured in web.xml, a HandlerInterceptor in the application context.
As a basic guideline, fine-grained handler-related preprocessing tasks are candidates for HandlerInterceptor implementations, especially factored-out common handler code and authorization checks. On the other hand, a Filter is well-suited for request content and view content handling, like multipart forms and GZIP compression. This typically shows when one needs to map the filter to certain content types (e.g. images), or to all requests.
[中]允许自定义处理程序执行链的工作流接口。应用程序可以为某些处理程序组注册任意数量的现有或自定义拦截器,以添加公共预处理行为,而无需修改每个处理程序实现。
在适当的HandlerAdapter触发处理程序本身的执行之前,会调用HandlerInterceptor。此机制可用于大量预处理方面,例如授权检查,或常见的处理程序行为,如区域设置或主题更改。它的主要目的是允许分解出重复的处理程序代码。
在异步处理场景中,当主线程退出时,处理程序可以在单独的线程中执行,而不呈现或调用postHandle和afterCompletion回调。当并发处理程序执行完成时,请求将被发回以继续呈现模型,并再次调用此契约的所有方法。有关更多选项和详细信息,请参阅org。springframework。网状物servlet。异步句柄拦截器
通常,拦截器链是根据HandlerMappingbean定义的,共享其粒度。为了能够将某个拦截器链应用于一组处理程序,需要通过一个HandlerMapping bean映射所需的处理程序。拦截器本身被定义为应用程序上下文中的bean,映射bean定义通过其“拦截器”属性(在XML:a<ref>的<list>中)引用拦截器。
HandlerInterceptor基本上类似于Servlet过滤器,但与后者不同的是,它只允许自定义预处理,并可以选择禁止执行处理程序本身,以及自定义后处理。过滤器功能更强大,例如,它们允许交换传递链中的请求和响应对象。请注意,过滤器是在web中配置的。xml,应用程序上下文中的HandlerInterceptor。
作为一项基本指导原则,与细粒度处理程序相关的预处理任务是HandlerInterceptor实现的候选任务,特别是考虑了常见处理程序代码和授权检查。另一方面,过滤器非常适合于请求内容和视图内容处理,如多部分表单和GZIP压缩。这通常显示何时需要将筛选器映射到某些内容类型(例如图像)或所有请求。
代码示例来源:origin: spring-projects/spring-framework
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable ModelAndView modelAndView) throws Exception {
this.interceptor.postHandle(request, response, handler, modelAndView);
}
代码示例来源: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: spring-projects/spring-framework
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable Exception ex) throws Exception {
this.interceptor.afterCompletion(request, response, handler, ex);
}
代码示例来源:origin: geoserver/geoserver
for (Iterator i = interceptors.iterator(); i.hasNext(); ) {
HandlerInterceptor interceptor = (HandlerInterceptor) i.next();
interceptor.preHandle(request, response, dispatcher);
interceptor.postHandle(request, response, dispatcher, null);
代码示例来源: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: geoserver/geoserver
for (Iterator i = interceptors.iterator(); i.hasNext(); ) {
HandlerInterceptor interceptor = (HandlerInterceptor) i.next();
interceptor.preHandle(request, response, dispatcher);
interceptor.postHandle(request, response, dispatcher, null);
代码示例来源:origin: apache/geode
.isEmpty();
interceptor.preHandle(request, null, null);
interceptor.afterCompletion(request, null, null, null);
代码示例来源: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 postHandle methods of registered interceptors.
*/
void applyPostHandle(HttpServletRequest request, HttpServletResponse response, @Nullable ModelAndView mv)
throws Exception {
HandlerInterceptor[] interceptors = getInterceptors();
if (!ObjectUtils.isEmpty(interceptors)) {
for (int i = interceptors.length - 1; i >= 0; i--) {
HandlerInterceptor interceptor = interceptors[i];
interceptor.postHandle(request, response, this.handler, mv);
}
}
}
代码示例来源:origin: org.tinygroup/org.tinygroup.springmvc
for (int i = 0; i < interceptors.length; i++) {
HandlerInterceptor interceptor = interceptors[i];
if (!interceptor.preHandle(processedRequest, response,
mappedHandler.getHandler())) {
triggerAfterCompletion(mappedHandler,
for (int i = interceptors.length - 1; i >= 0; i--) {
HandlerInterceptor interceptor = interceptors[i];
interceptor.postHandle(processedRequest, response,
mappedHandler.getHandler(), mv);
代码示例来源:origin: org.springframework/spring-webmvc
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable Exception ex) throws Exception {
this.interceptor.afterCompletion(request, response, handler, ex);
}
代码示例来源: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
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable ModelAndView modelAndView) throws Exception {
this.interceptor.postHandle(request, response, handler, modelAndView);
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Trigger afterCompletion callbacks on the mapped HandlerInterceptors.
* Will just invoke afterCompletion for all interceptors whose preHandle invocation
* has successfully completed and returned true.
*/
void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, @Nullable Exception ex)
throws Exception {
HandlerInterceptor[] interceptors = getInterceptors();
if (!ObjectUtils.isEmpty(interceptors)) {
for (int i = this.interceptorIndex; i >= 0; i--) {
HandlerInterceptor interceptor = interceptors[i];
try {
interceptor.afterCompletion(request, response, this.handler, ex);
}
catch (Throwable ex2) {
logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);
}
}
}
}
代码示例来源: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: org.springframework/spring-webmvc
/**
* Apply postHandle methods of registered interceptors.
*/
void applyPostHandle(HttpServletRequest request, HttpServletResponse response, @Nullable ModelAndView mv)
throws Exception {
HandlerInterceptor[] interceptors = getInterceptors();
if (!ObjectUtils.isEmpty(interceptors)) {
for (int i = interceptors.length - 1; i >= 0; i--) {
HandlerInterceptor interceptor = interceptors[i];
interceptor.postHandle(request, response, this.handler, mv);
}
}
}
代码示例来源:origin: org.springframework/spring-webmvc
/**
* Trigger afterCompletion callbacks on the mapped HandlerInterceptors.
* Will just invoke afterCompletion for all interceptors whose preHandle invocation
* has successfully completed and returned true.
*/
void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, @Nullable Exception ex)
throws Exception {
HandlerInterceptor[] interceptors = getInterceptors();
if (!ObjectUtils.isEmpty(interceptors)) {
for (int i = this.interceptorIndex; i >= 0; i--) {
HandlerInterceptor interceptor = interceptors[i];
try {
interceptor.afterCompletion(request, response, this.handler, ex);
}
catch (Throwable ex2) {
logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);
}
}
}
}
代码示例来源: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
@Test
public void postHandle() throws Exception {
HandlerInterceptor interceptor = mock(HandlerInterceptor.class);
MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { "/**" }, interceptor);
mappedInterceptor.postHandle(mock(HttpServletRequest.class), mock(HttpServletResponse.class),
null, mock(ModelAndView.class));
then(interceptor).should().postHandle(any(), any(), any(), any());
}
代码示例来源:origin: apache/geode
@Test
public void afterCompletion_logsOut() throws Exception {
HttpServletRequest request = request().build();
interceptor.afterCompletion(request, null, null, null);
verify(securityService, times(1)).logout();
}
内容来源于网络,如有侵权,请联系作者删除!