• 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
  • 相关阅读:
    婴儿摇椅出口亚马逊欧盟CE认证检测标准
    Ubuntu 缩减磁盘空间
    将CSV、Excel、XML文件转换为MySQL数据库
    面对史上最难求职季,哪些测试技能更容易拿到offer?
    MySQL1
    CDC实战:MySQL实时同步数据到Elasticsearch之数组集合(array)如何处理【CDC实战系列十二】
    【家具建模三剑客】3DMAX一键生成软包插件BreadMaker面包机插件使用教程
    数据结构之初始泛型
    @敏捷组织从业者,开放敏捷架构O-AA™标准考试及认证项目重磅上线!
    带你十天轻松搞定 Go 微服务之大结局(分布式事务)
  • 原文地址:https://blog.csdn.net/qq_41653935/article/details/126855222