日期:2014-05-19 浏览次数:20738 次
package com.bolo.examples; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils; import com.bolo.examples.base.entity.User; public class MyFilter implements Filter { private static final String FILTERED_REQUEST = "@@session_context_filtered_request"; private static final String[] INHERENT_ESCAPE_URIS = { "/index.jsp", "/index.html", "/login.jsp" }; public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException { if (request != null && request.getAttribute(FILTERED_REQUEST) != null) { chain.doFilter(request, response); } else { request.setAttribute(FILTERED_REQUEST, Boolean.TRUE); HttpServletRequest httpRequest = (HttpServletRequest) request; User userContext = getSessionUser(httpRequest); if (userContext == null && !isURILogin(httpRequest.getRequestURI(), httpRequest)) { String toUrl = httpRequest.getRequestURL().toString(); if (!StringUtils.isEmpty(httpRequest.getQueryString())) { toUrl += "?" + httpRequest.getQueryString(); } httpRequest.getSession().setAttribute(LOGIN_TO_URL, toUrl); request.getRequestDispatcher("/index.jsp").forward(request,response); return; } chain.doFilter(request, response); } } //@Override public void init(FilterConfig filterConfig) throws ServletException { } /** * 当前URI资源是否需要登录才能访问 * @param requestURI * @param request * @return */ private boolean isURILogin(String requestURI, HttpServletRequest request) { if (request.getContextPath().equalsIgnoreCase(requestURI)||(request.getContextPath() + "/").equalsIgnoreCase(requestURI)) return true; for (String uri : INHERENT_ESCAPE_URIS) { if (requestURI != null && requestURI.indexOf(uri) >= 0) { return true; } } return false; } protected User getSessionUser(HttpServletRequest request) { return (User) request.getSession().getAttribute(USER_CONTEXT); } //@Override public void destroy() { } }