美文网首页
Springboot2拦截器设置(不拦截静态资源)

Springboot2拦截器设置(不拦截静态资源)

作者: 扑火_Cyril | 来源:发表于2018-09-20 17:25 被阅读0次
    • 1 springboot2配置拦截器和Springboot配置有所不同,话不多说,showcode。
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @Configuration
    public class WebConfig implements WebMvcConfigurer {
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
    
            InterceptorRegistration interceptorRegistration = registry.addInterceptor(new SessionHandlerInterceptor());
            interceptorRegistration.excludePathPatterns("/error");
            interceptorRegistration.excludePathPatterns("/static/**");
            interceptorRegistration.excludePathPatterns("/login");
    
            interceptorRegistration.addPathPatterns("/**");
    
    
        }
    
    
        private class SessionHandlerInterceptor implements HandlerInterceptor {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
    
                Object user = request.getSession().getAttribute("user");
                if (user == null) {
                    try {
                        response.sendRedirect("/login");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return false;
    
                }
                return true;
            }
    
            @Override
            public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
                //controller方法处理完毕后,调用此方法
            }
    
            @Override
            public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
                //页面渲染完毕后调用此方法
            }
        }
    }
    
    • 2 重点在于
     interceptorRegistration.excludePathPatterns("/static/**");
    

    这句会忽略静态资源。

    相关文章

      网友评论

          本文标题:Springboot2拦截器设置(不拦截静态资源)

          本文链接:https://www.haomeiwen.com/subject/sipnnftx.html