• SpringBoot(五)-- 整合Spring的拦截器


    一、步骤

    1.创建我们自己的拦截器类并实现 HandlerInterceptor 接口。 

    2.创建一个Java类继承WebMvcConfigurerAdapter,并重写 addInterceptors 方法。

    3.实例化我们自定义的拦截器,然后将对像手动添加到拦截器链中(在addInterceptors方法中添加)。

    二、实例

    1.创建自定义拦截器并实现HandlerInterceptor 接口

     1 package com.xsjt.interceptor;
     2 
     3 import javax.servlet.http.HttpServletRequest;
     4 import javax.servlet.http.HttpServletResponse;
     5 import org.slf4j.Logger;
     6 import org.slf4j.LoggerFactory;
     7 import org.springframework.web.servlet.HandlerInterceptor;
     8 import org.springframework.web.servlet.ModelAndView;
     9 
    10 /**  
    11  * ClassName:XbqInterceptor 
    12  * 自定义拦截器
    13  * Date:     2017年11月6日 下午4:56:27
    14  * @author   Joe  
    15  * @version    
    16  * @since    JDK 1.8
    17  */
    18 public class XbqInterceptor implements HandlerInterceptor{
    19     
    20     private static final Logger logger = LoggerFactory.getLogger(XbqInterceptor.class);
    21 
    22     @Override
    23     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    24         logger.info(this.getClass().getName() + "<---xbq--->preHandle");
    25         return true;
    26     }
    27 
    28     @Override
    29     public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    30         logger.info(this.getClass().getName() + "<---xbq--->postHandle");
    31     }
    32 
    33     @Override
    34     public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    35         logger.info(this.getClass().getName() + "<---xbq--->afterCompletion");
    36     }
    37 }
     1 package com.xsjt.interceptor;
     2 
     3 import javax.servlet.http.HttpServletRequest;
     4 import javax.servlet.http.HttpServletResponse;
     5 import org.slf4j.Logger;
     6 import org.slf4j.LoggerFactory;
     7 import org.springframework.web.servlet.HandlerInterceptor;
     8 import org.springframework.web.servlet.ModelAndView;
     9 
    10 /**  
    11  * ClassName:XbqInterceptor 
    12  * 自定义拦截器
    13  * Date:     2017年11月6日 下午4:56:27
    14  * @author   Joe  
    15  * @version    
    16  * @since    JDK 1.8
    17  */
    18 public class JoeInterceptor implements HandlerInterceptor{
    19     
    20     private static final Logger logger = LoggerFactory.getLogger(JoeInterceptor.class);
    21 
    22     @Override
    23     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    24         logger.info(this.getClass().getName() + "<---joe--->preHandle");
    25         return true;
    26     }
    27 
    28     @Override
    29     public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    30         logger.info(this.getClass().getName() + "<---joe--->postHandle");
    31     }
    32 
    33     @Override
    34     public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    35         logger.info(this.getClass().getName() + "<---joe--->afterCompletion");
    36     }
    37 }
    View Code

    2.创建一个Java类继承WebMvcConfigurerAdapter,并重写 addInterceptors 方法

    3.实例化我们自定义的拦截器,然后将对像手动添加到拦截器链中(在addInterceptors方法中添加)

     1 package com.xsjt.config;
     2 
     3 import org.springframework.context.annotation.Configuration;
     4 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
     5 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
     6 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
     7 import com.xsjt.interceptor.JoeInterceptor;
     8 import com.xsjt.interceptor.XbqInterceptor;
     9 
    10 /**  
    11  * ClassName:SpringInterceptorRegister 
    12  * Spring 拦截器 注册器
    13  * Date:     2017年11月6日 下午4:58:32
    14  * @author   Joe  
    15  * @version    
    16  * @since    JDK 1.8
    17  */
    18 @Configuration
    19 public class SpringInterceptorRegister extends WebMvcConfigurerAdapter{
    20 
    21     /**
    22      * TODO 添加spring中的拦截器.  
    23      * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addInterceptors(org.springframework.web.servlet.config.annotation.InterceptorRegistry)
    24      */
    25     @Override
    26     public void addInterceptors(InterceptorRegistry registry) {
    27         // 拦截所有路径
    28         registry.addInterceptor(new XbqInterceptor()).addPathPatterns("/**");
    29         // 拦截/freemarker后路径
    30         registry.addInterceptor(new JoeInterceptor()).addPathPatterns("/freemarker/**");
    31         super.addInterceptors(registry);
    32     }
    33 
    34     /**
    35      * TODO  注册静态文件的自定义映射路径  
    36      * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addResourceHandlers(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry)
    37      */
    38     @Override
    39     public void addResourceHandlers(ResourceHandlerRegistry registry) {
    40         registry.addResourceHandler("/image/**").addResourceLocations("classpath:/image/");
    41         registry.addResourceHandler("/picture/**").addResourceLocations("file:D:/picture/");
    42         super.addResourceHandlers(registry);
    43     }
    44 }

    4.源码下载

      https://gitee.com/xbq168/spring-boot-learn

  • 相关阅读:
    sql 自定义函数-16进制转10进制
    编写一个单独的Web Service for Delphi
    Web Service
    无需WEB服务器的WEBServices
    Svn总是提示输入账号密码
    阿里云服务器SQLSERVER 2019 远程服务器环境搭建
    svn客户端使用
    数据库设计规则(重新整理)
    数据库表字段命名规范
    怎样去掉DELPHI 10.3.3 启动后的 security alert 提示窗体
  • 原文地址:https://www.cnblogs.com/xbq8080/p/7774707.html
Copyright © 2020-2023  润新知