配置类

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.asiainfo.group.filter.LoginFilter;
import com.asiainfo.group.interceptor.LoginInterceptor;

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{
	
	
	@Bean
    public FilterRegistrationBean filterRegist() {
        FilterRegistrationBean frBean = new FilterRegistrationBean();
        
        frBean.setFilter(new LoginFilter());
        frBean.addUrlPatterns("/*");
        
        return frBean;
    }
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		
		registry.addInterceptor(new LoginInterceptor())
			.addPathPatterns("/*")
			.excludePathPatterns("/login")
			.excludePathPatterns("/user_login.html");
	}
	
	
}

拦截器类

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		
		System.err.println("----afterCompletion被调用----");
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		
		System.err.println("----postHandle被调用----");
	}

	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object obj) throws Exception {
		
		System.err.println("----preHandle被调用----");
		Object username = req.getSession().getAttribute("username");
		
		if(username==null){
			resp.sendRedirect("/user_login.html");
			return false;
		}
		
		return true;
	}

}

过滤器类

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.HttpServletResponse;

public class LoginFilter implements Filter {
	//定义白名单
	public static final String IGNORE_URL="/login;/user_login.html;";
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request=(HttpServletRequest)req;
		HttpServletResponse response=(HttpServletResponse)resp;
		//获取路径
		String path = request.getRequestURI().substring(request.getContextPath().length()).
				replaceAll("[/]+$", "");
		
		//判断是不是js文件,是则不拦截
		if(path.endsWith(".js")){
			chain.doFilter(req, resp);
		}
		//如果白名单包含当前路径,则不拦截
		if(IGNORE_URL.contains(path)){
			chain.doFilter(req, resp);
		}
		
		Object username = request.getSession().getAttribute("username");
		if(username!=null){
			chain.doFilter(req, resp);
		}else{
			response.sendRedirect("/user_login.html");
			return;
		}
		
		
		
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}
	
	
}