• spring boot 拦截器不拦截静态资源,只拦截controller的方法


    拦截器拦截静态资源问题

    实现拦截器的做法就是实现HandlerInterceptor
    然后再WebMvcConfigurer里配置拦截器

    这个时候,一般要手动添加过滤的静态资源路径,如果静态资源路径修改,则拦截器要修改

    如果有添加其他拦截/**的拦截器,也需要配置静过滤态资源路径

    @Configuration
    public class WebConfigurer implements WebMvcConfigurer {
    
        @Autowired
        private LoginInterceptor loginInterceptor;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            String []strs = {"/static/**","/project/**","/assets/**","/login","/login.do"};
            registry.addInterceptor(loginInterceptor).excludePathPatterns(strs);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    只拦截controller方

    利用HandlerInterceptor 接口的handler
    如果是controller的方法,handler为 HandlerMethod
    如果是资源类,handler为 org.springframework.web.servlet.resource.ResourceHttpRequestHandler

    public interface HandlerInterceptor {
        default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            return true;
        }
    	...
    
    • 1
    • 2
    • 3
    • 4
    • 5

    实现

    加一层抽象类,新增的拦截器继承该抽象类,并在controllerMethodPreHandle方法实现业务功能,即可忽视静态资源类

    public abstract class AbstrctControllerInterceptor implements HandlerInterceptor {
    
        protected abstract boolean controllerMethodPreHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException;
    
        @Override
        public final boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            //如果是controller的方法handler为HandlerMethod
            //如果是资源类则handler为org.springframework.web.servlet.resource.ResourceHttpRequestHandler
            if(handler instanceof HandlerMethod){
                return controllerMethodPreHandle(request,response,handler);
            }
            //否则不拦截
            return true;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    登录拦截器实现

    @Component
    public class LoginInterceptor extends AbstrctControllerInterceptor {
    
        @Override
        protected boolean controllerMethodPreHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
            Object user = request.getSession().getAttribute(ConstantValue.USER_CONTEXT_KEY);
            if(user == null){
                response.sendRedirect("/login");
                return false;
            }
            return true;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    对比之前的配置,可以忽视资源路径了

    @Configuration
    public class WebConfigurer implements WebMvcConfigurer {
    
        @Autowired
        private LoginInterceptor loginInterceptor;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
    //        String []strs = {"/static/**","/project/**","/assets/**","/login","/login.do"};
            String []strs = {"/login","/login.do"};
            registry.addInterceptor(loginInterceptor).excludePathPatterns(strs);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    面试之Java String 编码相关
    LDA代码训练报错记录
    日常 - UX 响应 Nginx 502 Bad Gateway
    PDE 中的先验估计是什么意思?
    分享5款小而精的实用软件
    Xilinx FPGA 7系列 GTX/GTH Transceivers (4) Aurora 8b10b 递增数收发验证
    Docker配置双栈网络引起的IPV6不通问题排查及解决
    零基础快速自学SQL,2天足矣。
    13.0、C语言——数据的存储(1)
    Gin:获取本机IP,获取访问IP
  • 原文地址:https://blog.csdn.net/qq_41653935/article/details/126855222