1. import javax.servlet.http.HttpServletResponse;   

  2. import org.apache.struts2.ServletActionContext;   

  3. import com.huangt.bean.SystemUser;   

  4. /* session过期、登录有效性及操作的权限验证拦截器 */   

  5. @SuppressWarnings("serial")   

  6. public class LoginedCheckInterceptor extends AbstractInterceptor {   

  7. /** 拦截请求并进行登录有效性验证 */   

  8. public String intercept(ActionInvocation ai) throws Exception {   

  9. HttpServletRequest request = (HttpServletRequest)ai.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);  

  10. //取得请求的URL   

  11. String url = ServletActionContext.getRequest().getRequestURL().toString();   

  12. HttpServletResponse response = ServletActionContext.getResponse();   

  13. response.setHeader("Pragma","No-cache");   

  14. response.setHeader("Cache-Control","no-cache");   

  15. response.setHeader("Cache-Control""no-store");   

  16. response.setDateHeader("Expires",0);   

  17. //对登录与注销请求直接放行,不予拦截   

  18. if (url.indexOf("login.action")!=-1 || url.indexOf("logout.action")!=-1){   

  19. return ai.invoke();   

  20. }   

  21. else{  

  22. //验证Session是否过期   

  23. if(!ServletActionContext.getRequest().isRequestedSessionIdValid()){   

  24. //session过期,转向session过期提示页,最终跳转至登录页面   

  25. return "tologin";   

  26. }   

  27. else{   

  28. HttpSession session = request.getSession();  

  29. String userId = (String)session.getAttribute(SessionConstant.SESSION_USER)  

  30. //验证是否已经登录   

  31. if (userId == null){   

  32. //尚未登录,跳转至登录页面   

  33. return "tologin";   

  34. }else{   

  35. return ai.invoke();   

  36.   

  37. }   

  38. }   

  39. }   

  40. }   

  41. }