AuthFilter
package com.zbb.filter; 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.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet Filter implementation class AuthFilter */ @WebFilter("/AuthFilter") public class AuthFilter implements Filter { /** * Default constructor. */ public AuthFilter() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub // place your code here HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; // 获取根目录所对应的绝对路径 /jumper/index.jsp String currentURL = request.getRequestURI(); // 截取到当前文件名用于比较 /index.jsp String targetURL = currentURL.substring(currentURL.indexOf("/", 1), currentURL.length()); // System.out.println(targetURL); // 如果session不为空就返回该session,如果为空就返回null HttpSession session = request.getSession(false); boolean canLogin = true; // 登录地址不过滤 if (currentURL.contains("jumper/jsp/admin/login/login.jsp")) { canLogin = false; } if (currentURL.contains("jumper/admin/login/validateLogin")) { canLogin = false; } // 静态文件不过滤 if (currentURL.contains("jumper/static")) { canLogin = false; } // ui页面不过滤 if (currentURL.contains("jumper/jsp/ui/") || currentURL.contains("jumper/index.jsp")) { canLogin = false; } if (canLogin) { // 判断当前页面是否是重顶次昂后的登陆页面页面,如果是就不做session的判断,防止死循环 if (session == null || session.getAttribute("user") == null) { // 如果session为空表示用户没有登陆就重定向到login.jsp页面 // System.out.println("request.getContextPath()=" + // request.getContextPath()); response.sendRedirect(request.getContextPath() + "/jsp/admin/login/login.jsp"); return; } } // 继续向下执行 chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>AuthFilter</filter-name> <filter-class>com.zbb.filter.AuthFilter</filter-class> </filter> <filter-mapping> <filter-name>AuthFilter</filter-name> <url-pattern>*</url-pattern> </filter-mapping> </web-app>