一、在applicationContext.xml中配置拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*/*" />
<mvc:exclude-mapping path="/login/" />
<mvc:exclude-mapping path="/login/login" />
<mvc:exclude-mapping path="/cAppInterface/autoLogin" />
<mvc:exclude-mapping path="/cAppInterface/login" />
<bean class="com.nis.common.interceptor.ActionInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
二、实现拦截器
package com.nis.common.interceptor;
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;
/**
* @ClassName: ActionInterceptor
* @Description: action 拦截器
* @author: xu
* @date: 2017年08月03日 16:55:17
*
*/
public class ActionInterceptor extends HandlerInterceptorAdapter {
private static final Logger logger = LoggerFactory.getLogger(ActionInterceptor.class);
private final String USERSESSION = "userSession";
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {
Object sessionObj = arg0.getSession().getAttribute(USERSESSION);
if (arg0.getRequestURI().indexOf("/sAppInterface/") > 0 || arg0.getRequestURI().indexOf("/mAppInterface/") > 0 || arg0.getRequestURI().indexOf("/cAppInterface/") > 0) {
return true;
}
if (sessionObj == null) {
arg0.getRequestDispatcher("/index.jsp").forward(arg0, arg1);
return false;
}
return true;
}
}