1. 程式人生 > >【Spring Boot】--攔截器配置

【Spring Boot】--攔截器配置

實現HandlerInterceptor介面

/**
 * @Auther: chisj [email protected]
 * @Date: 2018-12-03 19:09
 * @Description:
 */
@Slf4j
public class AuthInterceptor implements HandlerInterceptor {

    /**
     * 預處理方法,在呼叫控制器方法之前呼叫
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        log.info("preHandle is run...");
        return true;
    }

    /**
     * 處理後方法,在呼叫控制器方法之後呼叫
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,
                      Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        log.info("postHandle is run...");
    }

    /**
     * 整個請求處理完畢呼叫
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, @Nullable Exception ex) throws Exception {
        log.info("afterCompletion is run...");
    }

}

實現WebMvcConfigurer介面

/**
 * @Auther: chisj [email protected]
 * @Date: 2018-12-03 19:02
 * @Description:
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public AuthInterceptor authInterceptor() {
        return new AuthInterceptor();
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 新增攔截器
        registry.addInterceptor(authInterceptor()).addPathPatterns("/**");
    }

}

測試結果

2018-12-04 09:52:00.439  INFO 12104 --- [nio-8989-exec-1] o.a.c.c.C.[.[localhost].[/WeightSystem]  : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-12-04 09:52:00.440  INFO 12104 --- [nio-8989-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-12-04 09:52:00.457  INFO 12104 --- [nio-8989-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 17 ms
2018-12-04 09:52:00.462  INFO 12104 --- [nio-8989-exec-1] org.chisj.weight.config.CorsFilter       : *********************************過濾器被使用**************************
2018-12-04 09:52:00.466  INFO 12104 --- [nio-8989-exec-1] org.chisj.weight.config.AuthInterceptor  : preHandle is run...
pageNum = 1
pageSize = 3
2018-12-04 09:52:00.557  INFO 12104 --- [nio-8989-exec-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2018-12-04 09:52:00.697  INFO 12104 --- [nio-8989-exec-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2018-12-04 09:52:00.776  INFO 12104 --- [nio-8989-exec-1] org.chisj.weight.config.AuthInterceptor  : postHandle is run...
2018-12-04 09:52:00.777  INFO 12104 --- [nio-8989-exec-1] org.chisj.weight.config.AuthInterceptor  : afterCompletion is run...

參考部落格

Spring Boot 攔截器無效,不起作用
spring-boot 如何新增攔截器配置(自定義)