Filter IP拦截

简述:

对于部分IP进行授权访问,其余的IP拦截

参考文章:

    http://fackyou200.iteye.com/blog/1166043

    


知识点:

1. Filter配置

2. spring list属性注入

3. 页面返回

4. filter中页面重定向



说明:

1.  配置web.xml(ipFilter 对应的就是bean id)

  <filter>
      <filter-name>ipFilter</filter-name>
      <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
      <init-param>  
          <param-name>targetFilterLifecycle</param-name>  
          <param-value>true</param-value>  
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>ipFilter</filter-name>
      <url-pattern>/view/*</url-pattern>
  </filter-mapping>


2. applicationContext-filter.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="ipFilter" class="com.anialy.verload.filter.IPFilter">
		<property name="ipPermitedList">
			<list>
				<value>127.0.0.1</value>
			</list>
		</property>
	</bean>
</beans>


3. IPFilter.java (此处直接以打印形式返回页面)

import java.io.IOException;
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.http.HttpServletRequest;

public class IPFilter implements Filter {

	private List<String> ipPermitedList;
	
	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest httprequest = (HttpServletRequest)request;
		httprequest.setCharacterEncoding("utf-8");
		String ip = httprequest.getRemoteAddr();
		if(ipPermitedList.contains(ip)){
			chain.doFilter(request, response);
		}else{
			response.setCharacterEncoding("utf-8");
			response.getWriter().write("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html\"; charset=\"utf-8\"></head><body><center style='padding-top:100px'><span style='color:red'>404 错误请求 !</span></center></body></html>");
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
	}
	
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
	
	public void setIpPermitedList(List<String> ipPermitedList) {
		this.ipPermitedList = ipPermitedList;
	}
}


附注:

也可以将其配置为error_404.jsp页面

IPFilter.java

import java.io.IOException;
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.http.HttpServletRequest;

public class IPFilter implements Filter {

	private List<String> ipPermitedList;
	
	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest httprequest = (HttpServletRequest)request;
		String ip = httprequest.getRemoteAddr();
		if(ipPermitedList.contains(ip)){
			chain.doFilter(request, response);
		}else{
			httprequest.getRequestDispatcher("/view/error_404.jsp").forward(request, response);
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
	}
	
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
	
	public void setIpPermitedList(List<String> ipPermitedList) {
		this.ipPermitedList = ipPermitedList;
	}
}


erro_404.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<html>
  <head>
  </head>
  <body>
    <center style='padding-top:100px'>
      <span style='color:red'>404 错误请求 !</span>
    </center>
  </body>
</html>




效果:(apache tomcat 7 )

以ip, 192.168.0.199访问直接被拦截到错误页面

















你可能感兴趣的:(Filter IP拦截)