Interceptor and Filter

1.Filter meaning

Depends on the servlet container. Based on function callback in implementation, almost all requests can be filtered, but the disadvantage is that a filter instance can only be called once when the container is initialized. The purpose of using filters is to do some filtering operations to obtain the data we want to obtain, such as: web.xml custom encodingFilter filter
so that the data encoding submitted by the front end has been submitted to the background in utf-8 format
Insert picture description here

2. The meaning of interceptor:

Depends on the web framework, in SpringMVC, it depends on the SpringMVC framework. In terms of implementation, the reflection mechanism based on Java is an application of aspect-oriented programming (AOP). Because the interceptor is based on web framework calls, Spring's dependency injection (DI) can be used to perform some business operations, and an interceptor instance can be called multiple times during the life cycle of a controller. But the disadvantage is that it can only intercept controller requests. Other requests such as direct access to static resources cannot be intercepted. For example, some pages must be logged in before they can be displayed, otherwise some people know the access address of the internal page directly. If you access by address without interception, you can modify the internal data at will.
#3. springmvc configuration
Insert picture description here

package cn.com.mvc.interceptor;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Intercepter;

import cn.com.mvc.model.Teacher;
public class LoginInterceptor implements HandlerInterceptor {
	 /**
	   * 在执行Hander(调用方法函数时)前执行,验证正确返回ture,放行,不正确返回boolean
	   * 决定程序是否继续执行,只有返回true后,postHandle,afterCompletion才有执行的资格
	   */
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		//1、用户登录session、登录环境库列表信息存在且未超时,放行
		Teacher teacher = (Teacher) request.getSession().getAttribute("globalTeach");	
		if(teacher!=null){
			System.out.print("hello");
			request.getRequestDispatcher("/w/m/loginOutTime").forward(request, response);
			return false;		
		}
		return true;	
	}
  /**
   * 在执行Hander(调用方法函数时)后执行,同一的异常或者日志处理
   */
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}
	 /**
	   * 在执行Hander(调用方法函数时)后返回modelandview(视图渲染)前执行,用户统一处理返回的视图,例如将
	   * 公用的模型数据(导航菜单)添加到视图中
	   */
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

[Reference Document]
[1]:https://blog.csdn.net/andyzhaojianhui/article/details/78769920

Guess you like

Origin blog.csdn.net/weixin_40620651/article/details/102545058