美文网首页
2,SessionInterceptor

2,SessionInterceptor

作者: 滔滔逐浪 | 来源:发表于2019-01-11 14:45 被阅读2次
    package com.yuqiyu.jpf.config;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    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("/api/login")||request.getRequestURI().equals("/api/loginout") )
            {
                return true;
            }
        //验证session是否存在
    
            String username= (String) request.getSession().getAttribute("username");
            if(username == null)
            {
                response.sendRedirect("/api/loginout");
                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 {
    
        }
    }
    

    相关文章

      网友评论

          本文标题:2,SessionInterceptor

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