Function
Can constitute a stack of interceptors to complete specific functions, such as logging, login judgment, permission check and so on.
Benefits
Interceptors also allow you to modularize generic code as reusable classes.
Applications of interceptors:
AOP; needs some business logic (need to inject beans, etc.)
Understand
An interceptor acts as a filter: it filters out content that is not wanted or displayed. Interceptors allow you to abstract away a piece of code that you can use to improve the original method. At the same time, it can reduce the code redundancy and improve the reuse rate.
For example, when logging in to a page, if you need to verify the user’s password, permissions, etc., you can use custom interceptors for password validation and permission restriction. Only the correct log-in is redirected to the correct page. This way, if you need to add new permissions, you don’t need to change any code in the action, you can do it directly in the interceptor.
Interceptor execution flow
- The program executes the preHandle() method first, and if the method returns true, the program will continue to execute the handler method; otherwise, it will not execute any further.
- After the request is processed by the business processor (Controller class), the postHandle() method is executed and the response is returned to the client via DispatcherServlet.
- The afterCompletion() method is executed after the DispatcherServlet has processed the request.
Configuration methods
1. Declare a custom interceptor implementing the HandlerInterceptor interface
package com.example.interpretor.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class HelloIntercepter implements HandlerInterceptor {
Logger log = LoggerFactory.getLogger(HelloIntercepter.class);
/**
* before entering the controller method
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
log.info("interceptor------------------prehandle");
// business process: judgment intercept
// true or false
return true;
}
/**
* after the internal processing of the method is complete, before the page is rendered
* @param request
* @param response
* @param handler
* @param modelAndView
* @throws Exception
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
log.info("postHandle");
}
/**
* once the page is rendered
* @param request
* @param response
* @param handler
* @param ex
* @throws Exception
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) throws Exception {
log.info("afterCompletion");
}
}
2. Create a Config to implement WebMvcConfigurer and register the custom interceptor
package com.example.interpretor.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class ConfigOne implements WebMvcConfigurer {
@Autowired
private HelloIntercepter loginInterceptor;
// This method is used to configure static assets such as html, js, css, etc
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
}
// This is where we register our own interceptors
@Override
public void addInterceptors(InterceptorRegistry registry) {
// Register interceptor
InterceptorRegistration ir=registry.addInterceptor(new HelloIntercepter());
// Configuring intercept paths
// Only with the interception path will the configured interceptor be used
// (for relevant business judgment)
ir.addPathPatterns("/**");
// Configure paths not to be intercepted
ir.excludePathPatterns("/login");
}
}
0 Comments