日期:2014-05-17 浏览次数:20720 次
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 javax.servlet.http.HttpSession; public class RoleIntercepter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String uri = ((HttpServletRequest)request).getRequestURI(); //System.out.println(uri); HttpSession session = ((HttpServletRequest)request).getSession(); if(uri.contains("manager/") && uri.contains(xxx)) { User user = (User)session.getAttribute("curuser"); if(user != null && "admin".equals(user.getName())) { //System.out.println(user.getName() + "-进入管理"); }else { request.setAttribute("errmsg", "错误!您不能进行此操作!"); request.getRequestDispatcher("/error.jsp").forward(request, response); return; } } chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { } }
------解决方案--------------------
web.xml
<filter> <filter-name>roleIntercepter</filter-name> <filter-class>com.xxx.bean.web.common.RoleIntercepter</filter-class> </filter> <filter-mapping> <filter-name>roleIntercepter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>