The interceptor
The basic concept
The Interceptor Interceptor in SpringMVC is also very important and useful. Its main function is to intercept user requests and process them accordingly. For example, through it to carry out permission verification, or to judge whether the user login operations. There are two ways to define springMVC interceptors:
Implementing an interface: org. Springframework. Web. Servlet. HandlerInterceptor
Inherit the adapter org. Springframework. Web. Servlet. Handler. HandlerInterceptorAdapter
Interceptor implementation
Implement the HandlerInterceptor interface
- Interface implementation class
public class MyInterceptor1 implements HandlerInterceptor{
/** * preHandle executes before request method interception * returns true to permit the current request */
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("MyInterceptor1-->preHandle");
return true; // Continue with the action
}
/** * after the request is executed, execute */ before the view is generated
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("MyInterceptor1-->postHandle after the handler method and before generating the view...");
}
/** * intercepts the request method after execution */
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("When the handler method is finished, execute MyInterceptor1-->afterCompletion..."); }}Copy the code
- Effect the interceptor XML configuration
<! -- Configuration Mode 1 -->
<mvc:interceptors>
<! An Interceptor directly below the MVC :interceptors root will intercept all requests.
<bean class="com.xxxx.springmvc.interceptors.MyInterceptor1" />
</mvc:interceptors>
Copy the code
<! -- Configuration Mode 2 -->
<mvc:interceptors>
<! MVC :interceptor Intercepting all requests starting with the test address -->
<mvc:interceptor>
<mvc:mapping path="/test/*.do" />
<bean class="com.xxxx.springmvc.interceptors.MyInterceptor1" />
</mvc:interceptor>
</mvc:interceptors>
Copy the code
Inheritance HandlerInterceptorAdapter
In fact, the HandlerInterceptor interface is ultimately implemented.
- Subclass implementation class
public class MyInterceptor2 extends HandlerInterceptorAdapter{
/** * Overwrite the preHandle request before executing */
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("MyInterceptor2-->preHandle");
return true; }}Copy the code
- Effect the interceptor XML configuration
<! -- Configuration Mode 2 -->
<mvc:interceptors>
<! MVC :interceptor Intercepting all requests starting with the test address -->
<mvc:interceptor>
<mvc:mapping path="/test/*.do" />
<bean class="com.xxxx.springmvc.interceptors.MyInterceptor2" />
</mvc:interceptor>
</mvc:interceptors>
Copy the code
Multiple interceptor implementations
The SpringMvc framework supports multiple interceptor configurations to form chains of interceptors that intercept client requests multiple times.
- Interceptor code implementation
Refer to MyInterceptor1 and MyInterceptor2 here
- Effect the interceptor XML configuration
<mvc:interceptors>
<mvc:interceptor>
<! -- Intercept all requests -->
<mvc:mapping path="/ * *" />
<bean class="com.xxxx.springmvc.interceptors.MyInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<! -- Intercept all requests -->
<mvc:mapping path="/ * *" />
<bean class="com.xxxx.springmvc.interceptors.MyInterceptor2" />
</mvc:interceptor>
</mvc:interceptors>
Copy the code
Interceptor application – Illegal request interception processing
Use interceptor to authenticate user login request
UserController -UserController definition
/ * * * *@authorAdministrator * emulates user operations */
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/userLogin")
public ModelAndView userLogin(HttpServletRequest request){
ModelAndView mv=new ModelAndView();
User user=new User();
user.setUserName("admin");
user.setUserPwd("123456");
request.getSession().setAttribute("user", user);
mv.setViewName("success");
return mv;
}
@RequestMapping("/addUser")
public ModelAndView addUser(a){
System.out.println("Add user record...");
ModelAndView mv=new ModelAndView();
mv.setViewName("success");
return mv;
}
@RequestMapping("/delUser")
public ModelAndView delUser(a){
ModelAndView mv=new ModelAndView();
mv.setViewName("success");
return mv;
}
@RequestMapping("/updateUser")
public ModelAndView updateUser(a){
ModelAndView mv=new ModelAndView();
mv.setViewName("success");
returnmv; }}Copy the code
Illegal request interceptor definition
public class LoginInterceptor extends HandlerInterceptorAdapter{
/** * executes */ before method interception
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
User user= (User) request.getSession().getAttribute("user");
/** * Check whether the session user is empty */
if(null==user){
response.sendRedirect(request.getContextPath()+"/login.jsp");
return false;
}
return true; }}Copy the code
The interceptor configuration takes effect
<! -- Intercept all requests -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/ * *" />
<! -- Allow user login request -->
<mvc:exclude-mapping path="/user/userLogin"/>
<bean class="com.xxxx.springmvc.interceptors.LoginInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
Copy the code