Examples of articles --springboot custom interceptor

First, create your own interceptors, by implementing HandlerInterceptor interfaces, override three methods interface.

  The preHandle (), pre-callback method, to achieve pre-processor, such as: log checks are performed prior to the request controller layer,

  Before The postHandle (), after treatment callback method, to achieve post-processing processor, it is to be rendered in the view

  afterCompletion (), the entire request is processed the callback method, to callback after the entire view is rendered and

  

Import com.impte.study.domain.po.User;
 Import org.springframework.lang.Nullable;
 Import org.springframework.stereotype.Component;
 Import org.springframework.web.servlet.HandlerInterceptor;
 Import the org.springframework.web.servlet .ModelAndView; 
 
Import the javax.servlet.http.HttpServletRequest;
 Import javax.servlet.http.HttpServletResponse;
 Import the javax.servlet.http.HttpSession; 
 
@Component 
public  class LoginInterceptor the implements HandlerInterceptor from { 
 
    // this method is performed before the access interface, we just need to log in here to write verify the status of the business logic, you can verify the status of the landed before the user invokes the specified interface 
    public  booleanpreHandle (Request the HttpServletRequest, HttpServletResponse the Response, Object Handler) throws Exception {
         // each project to achieve a logical distinction has landed, here I use the most simple extraction User Session to validate landing. 
        Session = the HttpSession request.getSession ();
         // here User is placed into the session when landing 
        User the User = (User) session.getAttribute ( "the User" );
         // If the session is no user, means no landing 
        IF (the User == null ) {
             // this method returns false to ignore the current request, if a user invokes the need to login to use interface, if he had not landed here directly ignore
             // of course, you can use the response to the user to return some tips and told him not landed 
            return  false ; 
        } the else{
             Return  to true ;     // if the session there user, indicating that the user is logged in, release, users can continue to call the interface they need 
        } 
    } 
 
    public  void postHandle (Request the HttpServletRequest, HttpServletResponse the Response, Object Handler, @Nullable ModelAndView ModelAndView) throws {Exception 
    } 
 
    public  void afterCompletion (the HttpServletRequest Request, Response the HttpServletResponse, Object Handler, @Nullable Exception EX) throws Exception { 
    } 
}

Second, the registered interceptors that you create, implement WebConfigurer Interface

import com.impte.study.config.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
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 WebConfigurer implements WebMvcConfigurer {
 
  @Autowired
  private LoginInterceptor loginInterceptor;
 
  //This method is used to configure the static resources, such as html, js, css, etc. 
  @Override
   public  void addResourceHandlers (ResourceHandlerRegistry Registry) { 
  } 
 
  // This method is used to register the interceptor, the interceptor write our own need here adding to register to take effect 
  @Override
   public  void addInterceptors (InterceptorRegistry Registry) {
     // addPathPatterns ( "/ **") represents the intercept all requests,
     // excludePathPatterns ( "/ the login", "/ the Register") said that apart from landing and registration In addition, registration is not required because the landing landing can also access 
    registry.addInterceptor (loginInterceptor) .addPathPatterns ( "/ **") excludePathPatterns ( "/ the login", "/ the Register." ); 
  } 
}

Original link:

https://blog.csdn.net/qq_30745307/article/details/80974407

  

Guess you like

Origin www.cnblogs.com/zzb-yp/p/11512126.html