SpringMVC the interceptor (Interceptor)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_44296929/article/details/102524596

A. SpringMVC the interceptor (Interceptor)

Below is a flow chart of SpringMVC a flowchart :( we look at the position of the interceptor performed)
Here Insert Picture Description
1. Role:
Interceptor is running DispatcherServletafter, in each of the Controllerprevious and operating results may choose to release or intercept! In addition, the interceptor will run Controllerafter about interceptors, when processing a request, perform up to three times! But, usually the most attention is the first execution, that is Controllerbefore that (that is, the position ④ figure above)!

2. Basic Use:
requires a custom class, named, for example LoginInterceptor, implement HandlerInterceptorthe interface, the abstract method overrides 3:

public class LoginInterceptor implements HandlerInterceptor {
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		System.out.println("LoginInterceptor.preHandle()");
		return false;
	}
	
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println("LoginInterceptor.postHandle()");
	}
	
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println("LoginInterceptor.afterCompletion()");
	}	
}

Interceptor needs after a Spring configuration file can take effect! So, we need to add configuration:

<!-- 配置拦截器链 -->
<mvc:interceptors>
	<!-- 配置第1个拦截器 -->
	<mvc:interceptor>
		<!-- 指定拦截路径,不在拦截路径之内的将不予处理,即拦截器根本就不运行 -->
		<mvc:mapping path="/user/info.do"/>
		<mvc:mapping path="/user/password.do"/>
		<!-- 指定拦截器类 -->
		<bean class="cn.tedu.spring.interceptor.LoginInterceptor" />
	</mvc:interceptor>
</mvc:interceptors>

Interceptor class, run Controllerbefore the preHandle()method returns trueshowing release, return falserepresents intercept!

Example: For login interceptor, the user can determine the data in the Session, if the data exists, considered to be logged, direct release, if no data is considered not login, you need to redirect and intercept:

public boolean preHandle(
		HttpServletRequest request, 
		HttpServletResponse response, Object handler)
		throws Exception {
	System.out.println("LoginInterceptor.preHandle()");
	// 判断Session中的数据,得知是否登录
	HttpSession session = request.getSession();
	if (session.getAttribute("username") == null) {
		// Session中没有用户名,即:没有登录,则:重定向,并拦截
		response.sendRedirect("../user/login.do");
		// 返回false表示拦截
		return false;
	}
	// 返回true表示放行
	return true;
}

When configuring the interceptor, The root node is <mvc:interceptors>used to configure the interceptor chain, i.e. a SpringMVC any item can have several interceptors, thereby forming a chain of interceptors intercept if a request matches the configuration of a plurality of interceptors, will be processed sequentially each interceptor, any interception will cause subsequent requests to not Controllerto deal!

In <mvc:interceptor>, the <bean>node is used to specify the interceptor class; <mvc:mapping>node, configured to intercept a request for a path, each interceptor may be arranged a plurality of the node, and, upon configuration, supports the use of the asterisk *as a wildcard, for example: <mvc:mapping path="/user/*"/>in order to avoid interception is too large, by <mvc:exclude-mapping />exclusion request path out configuration, can be understood as the white list , the node may be arranged a plurality of:

<!-- 配置拦截器链 -->
<mvc:interceptors>
	<!-- 配置第1个拦截器 -->
	<mvc:interceptor>
		<!-- 指定拦截路径,不在拦截路径之内的将不予处理,即拦截器根本就不运行 -->
		<mvc:mapping path="/user/*" />
		<!-- 指定白名单,列举的请求路径将不予处理,即拦截器根本就不运行 -->
		<mvc:exclude-mapping path="/user/reg.do" />
		<mvc:exclude-mapping path="/user/login.do" />
		<mvc:exclude-mapping path="/user/handle_reg.do" />
		<mvc:exclude-mapping path="/user/handle_login.do" />
		<!-- 指定拦截器类 -->
		<bean class="cn.tedu.spring.interceptor.LoginInterceptor" />
	</mvc:interceptor>
</mvc:interceptors>

Note: The above configuration when the black or white list, you can use the asterisk *as a wildcard, but it can only match 1 trail (or any resource can only wildcard)! For example configuration that /user/*can be matched /user/reg.door /user/login.do, it can not be matched to /user/a/reg.door /user/a/b/login.dosuch a path! If desired multilayer path match, two weeks may be used, i.e. **, for example, the configuration /user/**may be any of the above paths match!

3. interceptor and filter What is the difference:

1, the interceptor is Interceptor filter is the Filter;
2, the interceptor is SpringMVC components in the filter is in Java EE components;
3, the interceptor is arranged in the Spring of the configuration file, the filter is disposed in in web.xml;
4, the interceptor is run DispatcherServletafter, Controllerbefore and in the Controllerimplementation of the latter two methods will be invoked, and the filter is run at all Servletbefore;
5, interceptor configuration is very flexible, You can configure a number of blacklist, you can configure a number of whitelist filter configuration is very simple, you can configure only one filter path;
interceptor and filter, there are many similarities, such as: access can be rejected out some may be selected to release; chain may be formed.

In contrast, in the framework of a project using SpringMVC, the interceptor will be easy to use than some of the filters, however, due to the execution time of the node, it does not completely replace the filter!

If the article a little help to you, remember that a point like oh, thank you.

Guess you like

Origin blog.csdn.net/weixin_44296929/article/details/102524596