如果是判断是否登录。建议使用filter [code="java"]package com.ccsu.common.filter;
import com.ccsu.common.constants.SessionConstants;
import com.ccsu.entity.userSystem.User;
import com.ccsu.util.userSystem.ProsReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
- Created with IntelliJ IDEA.
- User: Ambitor springMVC拦截器 判断session中用户是否过期
- Date: 13-6-27
- Time: 下午7:31
- To change this template use File | Settings | File Templates.
*
- @author 罗涛
*/
public class SystemFilter implements Filter {
Logger logger = LoggerFactory.getLogger(SystemFilter.class);
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
if (!(servletRequest instanceof HttpServletRequest) || !(servletResponse instanceof HttpServletResponse)) {
throw new ServletException("OncePerRequestFilter just supports HTTP requests");
}
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
HttpSession session = httpRequest.getSession(true);
StringBuffer url = httpRequest.getRequestURL();
String[] strs = ProsReader.getString("INDICATION_APP_NAME").split("\\|");
if (strs != null && strs.length > 0) {
for (String str : strs) {
if (url.indexOf(str) >= 0) {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
}
}
Object object = session.getAttribute(SessionConstants.SESSION_USER);
User user = object == null ? null : (User) object;
if (user == null) {
boolean isAjaxRequest = isAjaxRequest(httpRequest);
if (isAjaxRequest) {
httpResponse.setCharacterEncoding("UTF-8");
httpResponse.sendError(HttpStatus.UNAUTHORIZED.value(),
"您已经太长时间没有操作,请刷新页面");
}
httpResponse.sendRedirect("/page/login/user_login");
return;
}
filterChain.doFilter(servletRequest, servletResponse);
return;
}
/**
* 判断是否为Ajax请求
*
* @param request HttpServletRequest
* @return 是true, 否false
*/
public static boolean isAjaxRequest(HttpServletRequest request) {
return request.getRequestURI().startsWith("/api");
// String requestType = request.getHeader("X-Requested-With");
// return requestType != null && requestType.equals("XMLHttpRequest");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
/*如果需要注入,请取消注释*/
// ServletContext servletContext = filterConfig.getServletContext();
// WebApplicationContext applicationContext = (WebApplicationContext) servletContext.
// getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
// if (null == topConstantsImpl) {
// //从Spring AC 中加载app configuration对象
// topConstantsImpl = applicationContext.getBean(TopConstantsImpl.class);
// }
}
@Override
public void destroy() {
//To change body of implemented methods use File | Settings | File Templates.
}
}
[/code]
[code="java"] <!--<!– 判断用户session 是否过期,如果过期,让用户重新登录–>-->
<!---->
<!--SystemFilter-->
<!--com.yitaosoft.common.filter.SystemFilter-->
<!---->
<!---->
<!--SystemFilter-->
<!--/*-->
<!---->[/code]