public class adminFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request= (HttpServletRequest) servletRequest; HttpServletResponse response= (HttpServletResponse) servletResponse; String uri=request.getRequestURI();//获取相对路径 User user= (User) request.getSession().getAttribute("user"); if(uri.endsWith("/login.jsp")||uri.endsWith("/login.do")){ filterChain.doFilter(request,response); return; } if(user!=null){ filterChain.doFilter(request,response); }else{ response.sendRedirect("/admin/error/privilege.jsp"); } } }
adminFilter,web.xml中的配置
<filter> <filter-name>adminFilter</filter-name> <filter-class>com.bookstore.utils.adminFilter</filter-class> </filter> <filter-mapping> <filter-name>adminFilter</filter-name> <url-pattern>/admin/login/*</url-pattern> <url-pattern>/admin/notices/*</url-pattern> <url-pattern>/admin/orders/*</url-pattern> <url-pattern>/admin/products/*</url-pattern> </filter-mapping>