About setting IP Whitelist Some of the methods, collation, recorded a bit.
PackageCom.tools.iptool;Importjava.util.ArrayList;ImportJava.util.HashSet;Importjava.util.List;ImportJava.util.Set;ImportJava.util.regex.Pattern;/*** @ClassName: Ipwhitelist * @Function: IP Whitelist. * @Reason: About IP Whitelist related. * @Date: 2017-4-17 pm 02:49:08 *@authorHello_ Snoopy *@version */ Public classIpwhitelist {//the regular of the IP Private StaticPattern pattern =Pattern. Compile ((1\\d{1,2}|2[0-4]\\d|25[0-5]|\\d{1,2}) \ \. " + "(1\\d{1,2}|2[0-4]\\d|25[0-5]|\\d{1,2}) \ \." + "(1\\d{1,2}|2[0-4]\\d|25[0-5]|\\d{1,2}) \ \." + "(1\\d{1,2}|2[0-4]\\d|25[0-5]|\\d{1,2})"); /*** * Getavaliiplist: (Gets the list of available IPs based on IP whitelist settings). * * @date 2017-4-17 pm 02:50:20 *@paramIpConfig *@return */ Private StaticSet<string>getavaliiplist (String allowip) {Set<String> IPList =NewHashset<string>(); for(String allow:allowIp.replaceAll ("\\s", ""). Split (";")) { if(Allow.indexof ("*") >-1) {string[] IPs= Allow.split ("\ \.")); String[] from=NewString[] {"0", "0", "0", "0" }; String[] End=NewString[] {"255", "255", "255", "255" }; List<String> tem =NewArraylist<string>(); for(inti = 0; i < ips.length; i++) if(Ips[i].indexof ("*") >-1) {tem=Complete (Ips[i]); From[i]=NULL; End[i]=NULL; } Else{From[i]=Ips[i]; End[i]=Ips[i]; } stringbuffer FROMIP=NewStringBuffer (); StringBuffer EndIP=NewStringBuffer (); for(inti = 0; I < 4; i++) if(From[i]! =NULL) {fromip.append (From[i]). Append ("."); Endip.append (End[i]). Append ("."); } Else{fromip.append ("[*]."); Endip.append ("[*]."); } Fromip.deletecharat (Fromip.length ()-1); Endip.deletecharat (Endip.length ()-1); for(String s:tem) {string IP= Fromip.tostring (). replace ("[*]", S.split (";") [0]) + "-" + endip.tostring (). replace ("[*]", s.split (";") [1]); if(Validate (IP)) {iplist.add (IP)); } } } Else { if(Validate (Allow)) {Iplist.add (allow); } } } returnIPList; } /*** scoped to a single IP node * *@paramArg *@returnreturns the specified IP range, formatted as LIST[10;19, 100;199]*/ Private StaticList<string>Complete (String arg) {List<String> com =NewArraylist<string>(); if(arg.length () = = 1) {Com.add ("0;255"); } Else if(arg.length () = = 2) {String S1= Complete (ARG, 1); if(S1! =NULL) Com.add (S1); String S2= Complete (ARG, 2); if(S2! =NULL) Com.add (S2); } Else{String S1= Complete (ARG, 1); if(S1! =NULL) Com.add (S1); } returncom; } Private StaticString complete (String arg,intlength) {String from= ""; String End= ""; if(Length = = 1) { from= Arg.replace ("*", "0"); End= Arg.replace ("*", "9"); } Else{ from= Arg.replace ("*", "00"); End= Arg.replace ("*", "99"); } if(Integer.valueof (from) > 255) return NULL; if(Integer.valueof (end) > 255) End= "255"; returnfrom + ";" +end; } /*** Format Check when added to whitelist * *@paramIP *@return */ Private Static BooleanValidate (String IP) { for(String s:ip.split ("-")) if(!Pattern.matcher (s). Matches ()) { return false; } return true; } /*** * Checkloginip: (based on IP, and available IP list to determine whether the IP is included in the whitelist). * @date 2017-4-17 pm 03:01:03 *@paramIP *@paramIPList *@return */ Private Static BooleanCheckloginip (String IP, set<string>iplist) { if(Iplist.isempty () | |iplist.contains (IP))return true; Else { for(String allow:iplist) {if(Allow.indexof ("-") >-1) {string[] from= Allow.split ("-") [0].split ("\ \")); String[] End= Allow.split ("-") [1].split ("\ \")); String[] Tag= Ip.split ("\ \.")); //Step-by-step matching of IP from left to right BooleanCheck =true; for(inti = 0; I < 4; i++) { ints =integer.valueof (From[i]); intt =integer.valueof (Tag[i]); intE =integer.valueof (End[i]); if(! (s <= t && t <=e)) {check=false; Break; } } if(check) {return true; } } } } return false; } /*** * Checkloginip: (according to IP address, and IP whitelist settings rules to determine whether the IP is included in the whitelist). * @date 2017-4-17 pm 03:01:37 *@paramIP *@paramIpwhiteconfig *@return */ Public Static BooleanCheckloginip (String ip,string ipwhiteconfig) {Set<String> IPList =getavaliiplist (ipwhiteconfig); returncheckloginip (IP, iplist); } }
PackageCom.tools.iptool;/*** @ClassName: Ipwhitelisttest * @Function: Test code. * @Reason: Test the code. * @Date: 2017-4-17 pm 02:54:42 *@version */ Public classIpwhitelisttest {/*** Main: (). * @date 2017-4-17 pm 02:54:42 *@paramargs*/ Public Static voidMain (string[] args) {String Ipwhilte= "192.168.1.1;" +//set a whitelist for a single IP"192.168.2.*;" +//set the IP wildcard to match an IP segment"192.168.3.17-192.168.3.38";//set an IP range BooleanFlag = Ipwhitelist.checkloginip ("192.168.2.2", Ipwhilte); BooleanFlag2 = Ipwhitelist.checkloginip ("192.168.1.2", Ipwhilte); BooleanFlag3 = Ipwhitelist.checkloginip ("192.168.3.16", Ipwhilte); BooleanFlag4 = Ipwhitelist.checkloginip ("192.168.3.17", Ipwhilte); SYSTEM.OUT.PRINTLN (flag); //trueSystem.out.println (FLAG2);//falseSystem.out.println (FLAG3);//falseSystem.out.println (FLAG4);//true }}
Java IP Whitelist Related tools class