A simple example of adding an interceptor to spring boot

There are two main types of interceptors in spring, one is HandlerInterceptor and the other is MethodInterceptor

一、HandlerInterceptor

HandlerInterceptor is an interceptor in the springMVC project. The target it intercepts is the requested address, which is executed before MethodInterceptor.

 

1. Create our own interceptor class and implement the HandlerInterceptor interface or inherit HandlerInterceptorAdapter.
2. Create a Java class that inherits WebMvcConfigurerAdapter and override the addInterceptors method.
3. Instantiate our custom interceptor, and then manually add the object to the interceptor chain (in the addInterceptors method).

package com.qicheshetuan.backend.web.interceptor;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

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

/**
 * Created by 15117 on 2018/4/27.
 */
public class TestInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println( "Called before the controller executes" );
        Boolean flag = true;
        if(flag){
            System.out.println(request.getMethod());
            return true;
        }else{
            System.out.println(request.getMethod());
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println( "Called after the backend controller is executed" );
         super .postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println( "Called after the entire request is completed" );
         super .afterCompletion(request, response, handler, ex);
    }
}
package com.qicheshetuan.backend.web.interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Created by 15117 on 2018/4/27.
 */
@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
}

二、MethodInterceptor

MethodInterceptor is an interceptor in AOP projects, it intercepts the target method, even if it is not the method in the controller. Implementing MethodInterceptor interceptors are roughly divided into two types, one is to implement the MethodInterceptor interface, and the other is to use AspectJ's annotations or configurations.

Annotation-based AspectJ approach

package com.qicheshetuan.backend.web.interceptor;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * Created by 15117 on 2018/4/27.
 */
@Component
@Aspect
public  class AspectJInterceptor {

    @Around("execution(* com.qicheshetuan.backend.web.controller.AutoCommunityController.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println( "Before the method is executed" );
        Object proceed = pjp.proceed();
        System.out.println( "After the method is executed" );
         return proceed;
    }
}
package com.qicheshetuan.backend.web.interceptor;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * Created by 15117 on 2018/4/27.
 */
@Component
@Aspect
public  class AspectJInterceptor {

    @Pointcut("execution(* com.qicheshetuan.backend.web.controller..*.*(..))")
    public void execMethod(){

    }
    @Before("execMethod()")
    public void beforeMethod(JoinPoint joinPoint){
        System.out.println( "Execute before method" );
        System.out.println(joinPoint.getSignature().getName());
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325026779&siteId=291194637