拦截器+ajax拦截和普通拦截



@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
log.info(">>>SessionTokenInterceptor");
//获取request域中信息  
HttpServletRequest request = ServletActionContext.getRequest();
//获得请求类型
String type = request.getHeader("X-Requested-With");  
String token = request.getParameter("token");
String url = request.getRequestURI();
log.info(">>>token:"+token);
log.info(">>>url:"+url);
Object sessioninfo = null;
String goto_url = new String();
if(!"".equals(token) && null!=token){
sessioninfo = cacheGet(token+CommonConfig.USER);
goto_url = setToGoingURL(request);
}else{
           request.setAttribute(GOTO_URL_KEY, "");   
}
//拦截
if(null==sessioninfo || "null".equals(sessioninfo)){
//ajax请求 会话过期时 返回字符串
if ("XMLHttpRequest".equalsIgnoreCase(type)){
PrintWriter printWriter = ServletActionContext.getResponse().getWriter();  
printWriter.print(AJAX_RELOGIN+"@"+goto_url);
printWriter.flush();
printWriter.close();
log.info("SessionTokenInterceptor<<<");
return null;  
}else{//普通http请求 直接返回页面 
log.info("SessionTokenInterceptor<<<");
return  "relogin";

}else{//放行
cacheSet(token+CommonConfig.USER,sessioninfo, new Date(OUT_TIME));
log.info(">>>刷新登录用户会话");
//设置request客户信息参数
request.setAttribute("sessioninfo", sessioninfo);
}
//执行action
String retStr = actionInvocation.invoke();
log.info("SessionTokenInterceptor<<<");
return retStr;
}

你可能感兴趣的:(java)