当前位置:   article > 正文

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

springboot过滤器不过滤静态资源

拦截器拦截静态资源问题

实现拦截器的做法就是实现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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/972897
推荐阅读
相关标签
  

闽ICP备14008679号