java 过滤器配置方法

web.xml配置

  <!--配置过滤器-->
  <filter>
    <filter-name>SystemInterceptor</filter-name>
    <filter-class>com.xxxx.common.Interceptor.SystemInterceptor</filter-class>
  </filter>
  <!--映射过滤器-->
  <filter-mapping>
    <filter-name>SystemInterceptor</filter-name>
    <!--“/*”表示拦截所有的请求 -->
    <url-pattern>/*</url-pattern>
  </filter-mapping>

 <!-- 安全方面应用 -->
<security-constraint>
	<!-- 对哪些请求方式进行拦截 -->
    <web-resource-collection>
      <web-resource-name>project</web-resource-name>
      <url-pattern>/*</url-pattern>
      <http-method>HEAD</http-method>
      <http-method>PUT</http-method>
      <http-method>TRACE</http-method>
      <http-method>DELETE</http-method>
      <http-method>OPTIONS</http-method>
    </web-resource-collection>
    <auth-constraint>
      <description>project</description>
      <role-name>All Role</role-name>
    </auth-constraint>
    <user-data-constraint>
      <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
  </security-constraint>

SystemInterceptor.java 继承 Filter

package com.xxxx.common.Interceptor;

import com.xxxx.model.SysUser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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;

@WebFilter
public class SystemInterceptor  implements Filter {
	private List<String> notCheckUrls = new ArrayList<String>();

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)arg0;
		HttpServletResponse response = (HttpServletResponse) arg1;
		response.setCharacterEncoding("UTF-8");
		String lastChars = request.getRequestURI().substring(request.getRequestURI().lastIndexOf("/") + 1);
		
		if (lastChars.equals("") ||isOther(lastChars)){
			//不拦截的特殊方法日志写入
			arg2.doFilter(arg0, arg1);//继续执行,.js,css等不需验证
			return;
		}

		SysUser user = (SysUser) request.getSession().getAttribute("User");
		if(user == null){//未登录
			response.sendRedirect(request.getContextPath() + "/login.html");
			return;
		}
		
		arg2.doFilter(arg0, arg1);//继续执行
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		notCheckUrls.add("login.html");
		notCheckUrls.add("axios");
		notCheckUrls.add("userlogin");
		notCheckUrls.add(".js");
		notCheckUrls.add(".png");
		notCheckUrls.add(".jpg");
		notCheckUrls.add(".gif");
		notCheckUrls.add(".css");
		notCheckUrls.add(".JPG");
		notCheckUrls.add(".PNG");
		notCheckUrls.add(".ico");
		notCheckUrls.add(".map");
		notCheckUrls.add(".vbs");
		notCheckUrls.add(".xml");
		notCheckUrls.add(".json");
		notCheckUrls.add(".geojson");
		notCheckUrls.add(".ttf");
		notCheckUrls.add(".pdf");
		notCheckUrls.add(".doc");
		notCheckUrls.add(".docx");
		notCheckUrls.add(".cur");
		notCheckUrls.add(".rar");
		notCheckUrls.add(".zip");
		notCheckUrls.add(".db");
		notCheckUrls.add(".txt");
		notCheckUrls.add(".xlsx");
		notCheckUrls.add(".xls");

	}
	
	/**
	 * 检查是否是不拦截的url
	 * @param path
	 * @return
	 */
	public boolean isOther(String path){
		boolean flag = false;
		int ncsize = notCheckUrls.size();
		for(int i=0;i<ncsize;i++){
			String str = notCheckUrls.get(i);
			if(path.endsWith(str)){
				flag = true;
				break;
			}
		}
		return flag;
	}
}


版权声明:本文为weixin_44957818原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。