java web项目过滤器Filter实现

1,在web.xml中配置一个Filter标签

 	
		Security Filter
		com.xx.xx.xx.xxFilter
		
			noSessionChkUrl
			
				
			
		
	
	
		Security Filter
		*.do
	 

2,编写一个实现Filter的类

public class xxFilter implements Filter
{
    private FilterConfig config;//读取xml中配置参数
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
    
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain filterChain) throws IOException, ServletException
    {
        
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse res = (HttpServletResponse)response;
        String sessionid = req.getParameter("sessionid");
        String token = req.getParameter("sessiontoken");
        String pagetheme = req.getParameter("pagetheme");
        
 		String requestUrl = req.getRequestURL().toString();
        String requestType = req.getHeader("X-Requested-With");  // for judging ajax request
        
        HttpSession session = req.getSession();
        
        session.setAttribute("pagetheme", (null == pagetheme ? "xrj" : pagetheme));
        
        if (null == Constants.ctx){
            Constants.ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(req.getSession().getServletContext());
        }
        
        SessionInfo si = (SessionInfo)session.getAttribute(Constants.SESSION_INFO);
        
        String chkUrls = config.getInitParameter("noSessionChkUrl");
        if ( this.checkContain(requestUrl, chkUrls) ){   //是免登录的请求
        	// do nothing
        } else {// 不是免登录的url请求,在下面进行必要的拦截逻辑处理
        	
            if (null == si) {
                si = SSOLoginHelper.SSOLogin(sessionid, token);
                if (null == si) {//登录失败
                	// 判断是否是ajax请求
                	if(StringUtil.isNotBlank(requestType) && requestType.equalsIgnoreCase("XMLHttpRequest"))
                	{
                		
                	}else{
                        res.sendRedirect(req.getContextPath() + "/jsp/403.jsp");
                	}
                    
                    return;
                }
                
                session.setAttribute(Constants.SESSION_INFO, si);
                session.setAttribute("pagetheme", si.getUsertheme());
                
            } else {
            	// 暂不对Session Info不为空时进行校验
                LoginSession loginSession = SSOLoginHelper.obtainLoginSession(sessionid, token);
            	if (null != loginSession){
    	            if (判断条件)
    	            {
    	                si = SSOLoginHelper.SSOLogin(sessionid, token);
    	                session.setAttribute(Constants.SESSION_INFO, si);
    	                
    	                session.setAttribute("pagetheme", si.getUsertheme());
    	            }
            	} else { // 如果没有LoginSession,则报错
                	// 判断是否是ajax请求
                	if(StringUtil.isNotBlank(requestType) && requestType.equalsIgnoreCase("XMLHttpRequest")) {
                    	

                	} else {
                        res.sendRedirect(req.getContextPath() + "/jsp/common/timeout.jsp");
                	}
                    
                    return;
            	}
            }
        }
        
        
        try {
            filterChain.doFilter(request, response);
        }
        catch (Exception e) {
            res.sendRedirect(req.getContextPath() + "/jsp/common/500.jsp");
            logger.error("session校验异常",e);
            throw new ServletException("Unknown Problem!");
        }
        
    }
    
    public void destroy() {
        
    }
    
    public void init(FilterConfig arg0) throws ServletException {
        this.config = arg0;
    }
}

这样启动的就是就可以实现对没有登录的用户或者其他设计session的情景进行拦截处理。增加系统的可靠性

你可能感兴趣的:(java)