Spring MVC拦截器HandlerInterceptorAdapter,HandlerInterceptor示例

时间:2020-02-23 14:35:55  来源:igfitidea点击:

Spring Interceptor用于拦截客户端请求并进行处理。
有时我们想截取HTTP请求并进行一些处理,然后再将其移交给控制器处理程序方法。
这就是Spring MVC拦截器派上用场的地方。

Spring 拦截器

就像拥有Struts2拦截器一样,我们可以通过实现org.springframework.web.servlet.HandlerInterceptor接口或者覆盖抽象类org.springframework.web.servlet.handler.HandlerInterceptorAdapter来创建自己的Spring拦截器HandlerInterceptor接口的基本实现。

Spring 拦截器– HandlerInterceptor

Spring HandlerInterceptor根据我们想要拦截HTTP请求的位置声明了三个方法。

  • boolean preHandle(HttpServletRequest请求,HttpServletResponse响应,对象处理程序):此方法用于在将请求移交给处理程序方法之前对其进行拦截。
    这个方法应该返回'true'来让Spring知道通过另一个Spring拦截器处理请求,或者在没有其他Spring拦截器的情况下将其发送到处理程序方法。
    Spring 拦截器本身,无需进一步处理。
    在这种情况下,我们应该使用response对象向客户端请求发送响应.Object handler是选择的处理程序对象来处理请求。
    此方法也可能引发Exception,在这种情况下,Spring MVC Exception Handling对于将错误页面作为响应发送应该很有用。

  • void postHandle(HttpServletRequest请求,HttpServletResponse响应,对象处理程序,ModelAndView和ModelAndView):当HandlerAdapter调用处理程序但DispatcherServlet尚未呈现视图时,将调用此HandlerInterceptor拦截器方法。
    此方法可用于将附加属性添加到要在视图页面中使用的ModelAndView对象。
    我们可以使用这种Spring拦截器方法来确定处理程序方法处理客户端请求所花费的时间。

  • afterCompletion(HttpServletRequest请求,HttpServletResponse响应,对象处理程序,异常异常):这是一个HandlerInterceptor回调方法,该方法在执行处理程序并呈现视图后立即被调用。

如果配置了多个Spring 拦截器,则preHandle()方法将按照配置顺序执行,而postHandle()和afterCompletion()方法将以相反的顺序调用。

让我们创建一个简单的Spring MVC应用程序,其中我们将配置一个Spring Interceptor来记录控制器处理程序方法的时序。

我们最终的Spring Interceptor示例项目将如下图所示,我们将研究感兴趣的组件。

Spring 拦截器–控制器类

package com.theitroad.spring;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@RequestMapping(value = "/home", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		//adding some time lag to check interceptor execution
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		logger.info("Before returning view page");
		return "home";
	}
	
}

我只是在处理程序方法的执行中添加了一些处理时间,以检查我们的spring拦截器方法是否有效。

Spring MVC拦截器– HandlerInterceptorAdapter实现

为简单起见,我扩展了抽象类HandlerInterceptorAdapter
HandlerInterceptorAdapter是HandlerInterceptor接口的抽象适配器类,用于简化仅前/后仅拦截器的实现。

package com.theitroad.spring;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class RequestProcessingTimeInterceptor extends HandlerInterceptorAdapter {

	private static final Logger logger = LoggerFactory
			.getLogger(RequestProcessingTimeInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		long startTime = System.currentTimeMillis();
		logger.info("Request URL::" + request.getRequestURL().toString()
				+ ":: Start Time=" + System.currentTimeMillis());
		request.setAttribute("startTime", startTime);
		//if returned false, we need to make sure 'response' is sent
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println("Request URL::" + request.getRequestURL().toString()
				+ " Sent to Handler :: Current Time=" + System.currentTimeMillis());
		//we can add attributes in the modelAndView and use that in the view page
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		long startTime = (Long) request.getAttribute("startTime");
		logger.info("Request URL::" + request.getRequestURL().toString()
				+ ":: End Time=" + System.currentTimeMillis());
		logger.info("Request URL::" + request.getRequestURL().toString()
				+ ":: Time Taken=" + (System.currentTimeMillis() - startTime));
	}

}

逻辑非常简单,我只是记录处理程序方法执行的时间以及处理请求(包括渲染视图页面)所花费的总时间。

Spring MVC拦截器配置

我们必须将Spring拦截器连接到请求,我们可以使用mvc:interceptors元素来连接所有拦截器。
我们还可以通过映射元素提供URI模式以进行匹配,然后再为请求添加Spring 拦截器。

我们最终的spring bean配置文件(spring.xml)如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="https://www.springframework.org/schema/mvc"
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:beans="https://www.springframework.org/schema/beans"
	xmlns:context="https://www.springframework.org/schema/context"
	xsi:schemaLocation="https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing 
		infrastructure -->

	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven 

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
		up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" 

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
		in the /WEB-INF/views directory -->
	<beans:bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" 
		<beans:property name="suffix" value=".jsp" 
	</beans:bean>

	<!-- Configuring interceptors based on URI -->
	<interceptors>
		<interceptor>
			<mapping path="/home" 
			<beans:bean class="com.theitroad.spring.RequestProcessingTimeInterceptor"></beans:bean>
		</interceptor>
	</interceptors>

	<context:component-scan base-package="com.theitroad.spring" 

</beans:beans>

我将不解释该Web应用程序的所有其他组件,因为我们对它们不感兴趣,并且它们没有任何与Spring拦截器相关的特定配置。

Spring MVC拦截器应用测试

只需将应用程序部署在servlet容器中并调用home控制器,您将看到logger的输出,如下所示。

INFO : com.theitroad.spring.RequestProcessingTimeInterceptor - Request URL::https://localhost:9090/SpringInterceptors/home:: Start Time=1396906442086
INFO : com.theitroad.spring.HomeController - Welcome home! The client locale is en_US.
INFO : com.theitroad.spring.HomeController - Before returning view page
Request URL::https://localhost:9090/SpringInterceptors/home Sent to Handler :: Current Time=1396906443098
INFO : com.theitroad.spring.RequestProcessingTimeInterceptor - Request URL::https://localhost:9090/SpringInterceptors/home:: End Time=1396906443171
INFO : com.theitroad.spring.RequestProcessingTimeInterceptor - Request URL::https://localhost:9090/SpringInterceptors/home:: Time Taken=1085

输出确认Spring 拦截器方法已按定义的顺序执行。