当前位置:   article > 正文

SpringBoot自定义拦截器

springboot自定义拦截器

1、创建自定义拦截器

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

2、配置拦截器

创建springmvc配置类

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

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

  • 修改配置文件

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

访问login,显示拦截

 

释放通行后,得到返回值 

 

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

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/716339
推荐阅读
相关标签
  

闽ICP备14008679号