SpringMVC中的拦截器配置

拦截器的配置:

http://blog.csdn.net/ye_sheng/article/details/48395663

  1. 在spring-mvc.xml中配置

   

    

//

/log/*

   

      

 

2.编写接口类

package org.tonny.Interceptor;

 

import java.util.List;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import org.springframework.web.util.UrlPathHelper;

import org.tonny.model.UserInfo;

 

public class LoginInterceptor extends HandlerInterceptorAdapter{

   private List exclusiveUrl ;

    // URL辅助工具类  

   private UrlPathHelper urlPathHelper = new UrlPathHelper();  

   private final Logger log = LoggerFactory.getLogger(LoginInterceptor.class);  

   @Override  

   public void afterCompletion(HttpServletRequest request,  

           HttpServletResponse response, Object handler, Exception ex)  

           throws Exception {  

       super.afterCompletion(request, response, handler, ex);  

   }  

 

   @Override  

   public void postHandle(HttpServletRequest request,  

           HttpServletResponse response, Object handler,  

           ModelAndView modelAndView) throws Exception {  

       super.postHandle(request, response, handler, modelAndView);  

   }  

 

   @Override  

   public boolean preHandle(HttpServletRequest request,  

           HttpServletResponse response, Object handler) throws Exception {  

   System.out.println(exclusiveUrl);

       UserInfo user = (UserInfo) request.getSession().getAttribute("user");  

       String path=request.getServletPath();

       String requestUri = request.getRequestURI(); //请求完整路径,可用于登陆后跳转  

       String contextPath = request.getContextPath();  //项目下完整路径  

       String url = requestUri.substring(contextPath.length()); //请求页面  

       if(user == null){  

           request.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(request, response);  

           return false;  

       }else{

        if (request.getHeader("x-requested-with") != null){   

        response.setHeader("sessionstatus", "timeout"); // 响应头设置session状态   

        log.info("这是ajax请求!");  

        return true;

        }  

       }

       return super.preHandle(request, response, handler);  

   }

 

public List getExclusiveUrl() {

return exclusiveUrl;

}

 

public void setExclusiveUrl(List exclusiveUrl) {

this.exclusiveUrl = exclusiveUrl;

}  

你可能感兴趣的:(SpringMVC)