Normally , Interception of requests from browsers , It's using Filter Realized , It can be done in this way Bean Pretreatment , Post processing .
Spring MVC The interceptor of Filter All features of , It can also control the interception accuracy more accurately .

Spring For us org.springframework.web.servlet.handler.HandlerInterceptorAdapter This adapter , Inherit this class , Can be very convenient to implement their own interceptor . He has three ways :

public boolean preHandle(HttpServletRequest request, HttpServletResponse
response, Object handler)
throws Exception {
return true;
public void postHandle(
HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView)
throws Exception {
public void afterCompletion(
HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex)
throws Exception {

The preprocessing is realized separately , Post processing ( Called Service And return ModelAndView, But no page rendering ), Return processing ( The page has been rendered )
stay preHandle in , It can be coded , Security control, etc ;
stay postHandle in , Opportunity to modify ModelAndView;
stay afterCompletion in , According to ex Is it null Determine if an exception has occurred , Logging .

If based on xml Configuration use Spring
MVC, It can be used SimpleUrlHandlerMapping,BeanNameUrlHandlerMapping conduct Url mapping ( amount to struts Of path mapping ) And intercept requests ( injection interceptors), If used based on annotations Spring
MVC, have access to DefaultAnnotationHandlerMapping injection interceptors. Note that no matter based on xml Or is it based on annotations ,HandlerMapping
bean All need to be in xml Configured in .

One demo:
In this case , We assume that UserController The registration operation in the 9:00-12:00 to open up , You can use interceptors to do this .

public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter {
private int openingTime;
private int closingTime;
private String mappingURL;// Using regular mapping to the path to be intercepted
public void setOpeningTime(int openingTime) {
this.openingTime = openingTime;
public void setClosingTime(int closingTime) {
this.closingTime = closingTime;
public void setMappingURL(String mappingURL) {
this.mappingURL = mappingURL;
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String url=request.getRequestURL().toString();
if(mappingURL==null || url.matches(mappingURL)){
Calendar c=Calendar.getInstance();
c.setTime(new Date());
int now=c.get(Calendar.HOUR_OF_DAY);
if(now<openingTime || now>closingTime){
request.setAttribute("msg", " Registration opening hours :9:00-12:00");
request.getRequestDispatcher("/msg.jsp").forward(request, response);
return false;
return true;
return true;

xml to configure :

<bean id="timeBasedAccessInterceptor"
<property name="openingTime" value="9" />
<property name="closingTime" value="12" />
<property name="mappingURL" value=".*/user\.do\?action=reg.*" />
<property name="interceptors">
<ref bean="timeBasedAccessInterceptor"/>

Here we define a mappingURL attribute , Implementation using regular expression pair url Match , So as to intercept more finely . Of course, if you don't define it mappingURL, All pairs are blocked by default Controller Request for .


public class UserController{
private UserService userService;
public ModelAndView reg(Users user) throws Exception {
return new ModelAndView("profile","user",user);
// other option ...

this Controller amount to Struts Of DispatchAction

You can also configure multiple interceptors , Each interceptor has a different division of labor .

©2019-2020 Toolsou All rights reserved,
be based on STM32 Design of infrared obstacle avoidance car ( There is a code )elementui select obtain value Ai Wei also talks about project management , On grass roots management ajax get Request Chinese parameter garbled solution uboot set up nfs parameter ——linux kernel use nfs Mode to load the root file system rootfs( Six ) be based on RK3399 PWM Drive development Determine whether the current object is an array 4 Methods ( Essence )2020 year 6 month 26 day C# Class library Loop execution help class [RK3399][Android7.1] Learning notes DRM Driver development ( introduce ) Huawei Mate 40 Pro+ 5G exposure : Leica film lens , Ceramic body