Implement IP range filtering instances in java

Source: Internet
Author: User
Tags regular expression

The first reflection of OneCoder is to implement a filter, configure accessible IP rules, and match through regular expressions. You can access the system normally. If you do not use redirect, you can access the specified welcome page.

The entire code has no difficulty. The core is an IP rule validation function. Considering the simplicity of the configuration, that is, you are generally familiar with 192.168.1. * and 192.168.2.1-23. This configuration method supports full matching and range matching. OneCoder decides to use the sharding and resolution matching method. Rules are separated by semicolons. The code is implemented as follows:

The code is as follows: Copy code
/*** IP rule regular expression processing tool class ** @ author OneCoder * @ date 1:23:09 on January 1, October 23, 2014 */public class IPRegexUtil {

/**
* Check whether the given IP address meets the specified rule
  *
* @ Author li_hongzhe @ nhn.com
* @ Date 1:38:37, January 1, October 23, 2014
* @ Param ipStr
* IP address to be verified
* @ Param ipPattern
* IP address matching rules. Supports * matching all and-matching ranges. Separate them with semicolons & lt; br & gt;
* Example: 10.34.163. *; 10.34.162.1-128
* @ Return
*/
Public static boolean validateIP (String ipStr, String ipPattern ){
If (ipStr = null | ipPattern = null ){
Return false;
      }
String [] patternList = ipPattern. split (";");
For (String pattern: patternList ){
If (passValidate (ipStr, pattern )){
Return true;
          }
      }
Return false;
 }

Private static boolean passValidate (String ipStr, String pattern ){
String [] ipStrArr = ipStr. split (".");
String [] patternArr = pattern. split (".");
If (ipStrArr. length! = 4 | patternArr. length! = 4 ){
Return false;
      }
Int end = ipStrArr. length;
If (patternArr [3]. contains ("-")){
End = 3;
String [] rangeArr = patternArr [3]. split ("-");
Int from = Integer. valueOf (rangeArr [0]). intValue ();
Int to = Integer. valueOf (rangeArr [1]). intValue ();
Int value = Integer. valueOf (ipStrArr [3]). intValue ();
If (value & lt; from | value & gt; ){
Return false;
          }
      }
For (int I = 0; I & lt; end; I ++ ){
If (patternArr [I]. equals ("*")){
Continue;
          }
If (! PatternArr [I]. equalsIgnoreCase (ipStrArr [I]) {
Return false;
          }
      }
Return true;
 }

}



The implementation filter is as follows:

The code is as follows: Copy code
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;

/*** IP address filter. If the IP address is within the filter range, access is allowed. <Br> * if it is not within the specified range, the page is displayed. ** @ Author OneCoder * @ date October 24, 2014 9:54:33 */public class IPFilter implements Filter {

Private String ipPattern;

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

 }

@ Override
Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
String ip = request. getRemoteHost ();
String reqUrl = (HttpServletRequest) request). getRequestURI ();
If (reqUrl. contains ("forbidden ")){
FilterChain. doFilter (request, response );
Return;
      }
If (IPRegexUtil. validateIP (ip, ipPattern )){
FilterChain. doFilter (request, response );
} Else {
(HttpServletResponse) response). sendRedirect ("/shurnim/forbidden ");
      }
 }

@ Override
Public void init (FilterConfig filterConfig) throws ServletException {
This. ipPattern = filterConfig. getInitParameter ("ip-pattern ");
 }

Public String getIpPattern (){
Return ipPattern;
 }

Public void setIpPattern (String ipPattern ){
This. ipPattern = ipPattern;
 }

}



Web. the xml configuration is as follows: <pre class = "brush: xml"> <filter-name> IPFilter </filter-name> <filter-class> com. coderli. web. filter. ip. IPFilter </filter-class> <init-param> <param-name> ip-pattern </param-name> <param-value> 10.34.163.169; 10.34.163.1-254 </param-value> </init-param> </filter> <filter-mapping> <filter-name> IPFilter </filter-name> <url-pattern >/* </url-pattern> </filter-mapping> </pre>

The code does not seem to be well explained. At present, it seems that the requirements are met.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.