美文网首页
springboot集成拦截器

springboot集成拦截器

作者: 要不再等等 | 来源:发表于2019-09-21 23:45 被阅读0次
    public class SessionInterceptor implements HandlerInterceptor
    {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
            System.out.println(request.getRequestURI());
            //登录不做拦截
            if(request.getRequestURI().equals("/user/login") || request.getRequestURI().equals("/user/login_view"))
            {
                return true;
            }
            //验证session是否存在
            Object obj = request.getSession().getAttribute("_session_user");
            if(obj == null)
            {
                response.sendRedirect("/user/login_view");
                return false;
            }
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    
        }
    
        @Override
        public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    
        }
    }
    
    @Configuration
    public class SessionConfiguration extends WebMvcConfigurerAdapter
    {
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new SessionInterceptor()).addPathPatterns("/**");
        }
    }
    

    相关文章

      网友评论

          本文标题:springboot集成拦截器

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