• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

SpringBoot自定义拦截器

武飞扬头像
朵朵宝
帮助1

1、创建自定义拦截器

学新通

  1.  
    package cn.woniu.myInterseption;
  2.  
     
  3.  
    import org.springframework.lang.Nullable;
  4.  
    import org.springframework.web.bind.annotation.ExceptionHandler;
  5.  
    import org.springframework.web.servlet.HandlerInterceptor;
  6.  
    import org.springframework.web.servlet.ModelAndView;
  7.  
     
  8.  
    import javax.servlet.http.HttpServletRequest;
  9.  
    import javax.servlet.http.HttpServletResponse;
  10.  
     
  11.  
    public class MyIntercepror implements HandlerInterceptor {
  12.  
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  13.  
    System.out.println("在controller之前执行!!!!");
  14.  
    return true;
  15.  
    //HandlerInterceptor.super.preHandle(request, response, handler);
  16.  
    }
  17.  
     
  18.  
    /**
  19.  
    * value: 只有抛出指定异常类型,才会被该方法捕捉到
  20.  
    * @return
  21.  
    */
  22.  
    @ExceptionHandler(value = {java.lang.ArithmeticException.class})
  23.  
    public ModelAndView mdv(){
  24.  
    ModelAndView modelAndView = new ModelAndView();
  25.  
    modelAndView.setViewName("error1");
  26.  
    return modelAndView;
  27.  
    }
  28.  
     
  29.  
    /**
  30.  
    * 在业务代码执行完了但是还没有执行试图解析器,意思就是还没有返回页面前执行
  31.  
    * * @param request
  32.  
    * @param response
  33.  
    * @param handler
  34.  
    * @param modelAndView
  35.  
    * @throws Exception
  36.  
    */
  37.  
    @Override
  38.  
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  39.  
    System.out.println("返回页面前");
  40.  
    HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
  41.  
    }
  42.  
     
  43.  
    /**
  44.  
    * 页面已经返回后执行
  45.  
    * @param request
  46.  
    * @param response
  47.  
    * @param handler
  48.  
    * @param ex
  49.  
    * @throws Exception
  50.  
    */
  51.  
    @Override
  52.  
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  53.  
    System.out.println("页面返回后!!!!");
  54.  
    HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
  55.  
    }
  56.  
    }
  57.  
     
  58.  
     
  59.  
     

2、配置拦截器

创建springmvc配置类

学新通

  1.  
    package cn.woniu.configuration;
  2.  
     
  3.  
    import cn.woniu.myInterseption.MyIntercepror;
  4.  
    import org.springframework.context.annotation.Configuration;
  5.  
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  6.  
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  7.  
    @Configuration
  8.  
    public class MyMvcHandler implements WebMvcConfigurer {
  9.  
    public void addInterceptors(InterceptorRegistry registry) {
  10.  
    registry.addInterceptor(new MyIntercepror())//将我们自定义拦截器
  11.  
    .addPathPatterns("/**")//所有路径都拦截
  12.  
    .excludePathPatterns("/","/login");//配置不需要拦截的url
  13.  
    }
  14.  
    }

配置拦截器后会造成页面静态资源无法加载的问题

  • 修改配置文件

    #配置mvc静态资源目录  不配置默认为"/**" spring.mvc.static-path-pattern=/static/**
    
  • 修改页面静态资源引用

要加上"/satic/"

  1.  
    <link href='/static/bootstrap/css/bootstrap.css' rel="stylesheet">
  2.  
    <script type="text/javascript" src='/static/js/jquery-3.5.1.js'></script>
  3.  
    <script type="text/javascript" src='/static/bootstrap/js/bootstrap.js'></script>
  • 修改拦截器注册方法

 返回类

  1.  
    package cn.woniu.controller;
  2.  
     
  3.  
    import cn.woniu.entity.Users;
  4.  
    import org.apache.ibatis.jdbc.Null;
  5.  
    import org.springframework.web.bind.annotation.RequestMapping;
  6.  
    import org.springframework.web.bind.annotation.RestController;
  7.  
     
  8.  
    @RestController
  9.  
    public class HandlerController {
  10.  
    @RequestMapping("/hhh")
  11.  
    public String testExcepition(){
  12.  
    int i =1/0;
  13.  
    // Users users=null;
  14.  
    // users.getId();
  15.  
    throw new NullPointerException();
  16.  
    //return "111";
  17.  
    }
  18.  
     
  19.  
     
  20.  
     
  21.  
    @RequestMapping(value="/login")
  22.  
    public String login(){
  23.  
     
  24.  
    return "login";
  25.  
    }
  26.  
    @RequestMapping(value="/index" )
  27.  
    public String index(){
  28.  
    System.out.println("执行controller方法");
  29.  
    return "index";
  30.  
    }
  31.  
    }

访问login,显示拦截

学新通 

释放通行后,得到返回值 

学新通 

我们在哪里控制放行和关闭,就是拦截器第一个方法的返回值,true为放行,false为关闭 

学新通 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgaaaee
系列文章
更多 icon
同类精品
更多 icon
继续加载