IP暴露接口IP白名单设置
暴露接口IP白名單設置
?
?
?
暴露接口IP白名單設置
CrazyL- 2018-01-03 14:36:15 ?4797 ?收藏 1
展開
String realIp = IPUtil.getIpAddr(request); ? ? ? ? ?
if(!"0:0:0:0:0:0:0:1".equals(realIp)){
? ? List<String> ipList = Resources.readLines(Resources.getResource("ipWhiteList.txt"),Charset.forName("utf-8"));
? ? if(!IPWhiteListUtil.checkIpList(realIp, ipList)){
? ? ? ? retMap.put("code", ILLEGAL_IP);
? ? ? ? retMap.put("msg", "非法IP,請聯(lián)系管理員");
? ? ? ? return retMap;
? ? }
}
?
工具類:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
/**
?* @ClassName:IPWhiteList
?* @Function: IP 白名單.
?* @Reason:關于IP白名單相關.
?* @Date: 2017-4-17 下午02:49:08
?* @author hello_史努比
?* @version
?*/
public class IPWhiteListUtil {
? ? // IP的正則
? ? private static Pattern 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:(根據(jù)IP白名單設置獲取可用的IP列表).
? ? ?*?
? ? ?* @date 2017-4-17 下午02:50:20
? ? ?* @param ipConfig
? ? ?* @return
? ? ?*/
? ? private static Set<String> getAvaliIpList(String allowIp) {
? ? ? ? Set<String> ipList = new HashSet<String>();
? ? ? ? for (String allow : allowIp.replaceAll("\\s", "").split(";")) {
? ? ? ? ? ? if (allow.indexOf("*") > -1) {
? ? ? ? ? ? ? ? String[] ips = allow.split("\\.");
? ? ? ? ? ? ? ? String[] from = new String[] { "0", "0", "0", "0" };
? ? ? ? ? ? ? ? String[] end = new String[] { "255", "255", "255", "255" };
? ? ? ? ? ? ? ? List<String> tem = new ArrayList<String>();
? ? ? ? ? ? ? ? for (int i = 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 = new StringBuffer();
? ? ? ? ? ? ? ? StringBuffer endIP = new StringBuffer();
? ? ? ? ? ? ? ? for (int i = 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);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return ipList;
? ? }
? ? private static Set<String> getAvaliIpList(Set<String> ipSet) {
? ? ? ? Set<String> ipList = new HashSet<String>();
? ? ? ? for (String allow : ipSet) {
? ? ? ? ? ? if (allow.indexOf("*") > -1) {
? ? ? ? ? ? ? ? String[] ips = allow.split("\\.");
? ? ? ? ? ? ? ? String[] from = new String[] { "0", "0", "0", "0" };
? ? ? ? ? ? ? ? String[] end = new String[] { "255", "255", "255", "255" };
? ? ? ? ? ? ? ? List<String> tem = new ArrayList<String>();
? ? ? ? ? ? ? ? for (int i = 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 = new StringBuffer();
? ? ? ? ? ? ? ? StringBuffer endIP = new StringBuffer();
? ? ? ? ? ? ? ? for (int i = 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);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return ipList;
? ? }
? ? /**
? ? ?* 對單個IP節(jié)點進行范圍限定
? ? ?*?
? ? ?* @param arg
? ? ?* @return 返回限定后的IP范圍,格式為List[10;19, 100;199]
? ? ?*/
? ? private static List<String> complete(String arg) {
? ? ? ? List<String> com = new ArrayList<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);
? ? ? ? }
? ? ? ? return com;
? ? }
? ? private static String complete(String arg, int length) {
? ? ? ? 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";
? ? ? ? return from + ";" + end;
? ? }
? ? /**
? ? ?* 在添加至白名單時進行格式校驗
? ? ?*?
? ? ?* @param ip
? ? ?* @return
? ? ?*/
? ? private static boolean validate(String ip) {
? ? ? ? for (String s : ip.split("-"))
? ? ? ? ? ? if (!pattern.matcher(s).matches()) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? return true;
? ? }
? ? /**
? ? ?*?
? ? ?* checkLoginIP:(根據(jù)IP,及可用Ip列表來判斷ip是否包含在白名單之中).
? ? ?* @date 2017-4-17 下午03:01:03
? ? ?* @param ip
? ? ?* @param ipList
? ? ?* @return
? ? ?*/
? ? private static boolean checkLoginIP(String ip, Set<String> ipList) {
? ? ? ? if (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("\\.");
? ? ? ? ? ? ? ? ? ? // 對IP從左到右進行逐段匹配
? ? ? ? ? ? ? ? ? ? boolean check = true;
? ? ? ? ? ? ? ? ? ? for (int i = 0; i < 4; i++) {
? ? ? ? ? ? ? ? ? ? ? ? int s = Integer.valueOf(from[i]);
? ? ? ? ? ? ? ? ? ? ? ? int t = Integer.valueOf(tag[i]);
? ? ? ? ? ? ? ? ? ? ? ? int e = Integer.valueOf(end[i]);
? ? ? ? ? ? ? ? ? ? ? ? if (!(s <= t && t <= e)) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? check = false;
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if (check) {
? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return false;
? ? }
? ? /**
? ? ?*?
? ? ?* checkLoginIP:(根據(jù)IP地址,及IP白名單設置規(guī)則判斷IP是否包含在白名單).
? ? ?* @date 2017-4-17 下午03:01:37
? ? ?* @param ip
? ? ?* @param ipWhiteConfig
? ? ?* @return
? ? ?*/
? ? public static boolean checkLoginIP(String ip,String ipWhiteConfig){
? ? ? ? Set<String> ipList = getAvaliIpList(ipWhiteConfig);
? ? ? ? return checkLoginIP(ip, ipList);
? ? }
? ? /**
? ? ?*?
? ? * ip在ipList中,則返回true?
? ? * @param ip
? ? * @param ipList
? ? * @return
? ? * @see
? ? ?*/
? ? public static boolean checkIpList(String ip,List<String> ipList){
? ? ? ? Set<String> ipSet = new HashSet<String>();
? ? ? ? for(String ipStr : ipList){
? ? ? ? ? ? if(!ipStr.trim().startsWith("#")){
? ? ? ? ? ? ? ? ipSet.add(ipStr.trim());
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? ipSet = getAvaliIpList(ipSet);
? ? ? ? return checkLoginIP(ip, ipSet);
? ? }
? ? public static void main(String[] args) {
? ? ? ? String ipWhilte = "192.168.1.1;" + ? ? ? ? ? ? ? ? //設置單個IP的白名單
? ? ? ? ? ? ? ? ? ? ? ? ? "192.168.2.*;" + ? ? ? ? ? ? ? ? //設置ip通配符,對一個ip段進行匹配
? ? ? ? ? ? ? ? ? ? ? ? ? "192.168.3.17-192.168.3.38"; ? ? //設置一個IP范圍
? ? ? ? boolean flag = checkLoginIP("192.168.2.2",ipWhilte);
? ? ? ? boolean flag2 = checkLoginIP("192.168.1.2",ipWhilte);
? ? ? ? boolean flag3 = checkLoginIP("192.168.3.16",ipWhilte);
? ? ? ? boolean flag4 = checkLoginIP("192.168.3.17",ipWhilte);
? ? ? ? System.out.println(flag); ?//true
? ? ? ? System.out.println(flag2); ?//false
? ? ? ? System.out.println(flag3); ?//false
? ? ? ? System.out.println(flag4); ?//true
? ? }
}
1
?
import javax.servlet.http.HttpServletRequest;
public class IPUtil {
? ? /**?
? ? ?* 獲取用戶真實IP地址,不使用request.getRemoteAddr()的原因是有可能用戶使用了代理軟件方式避免真實IP地址,?
? ? ?* 可是,如果通過了多級反向代理的話,X-Forwarded-For的值并不止一個,而是一串IP值?
? ? ?* ?
? ? ?* @return ip
? ? ?*/
? ? public static String getIpAddr(HttpServletRequest request) {
? ? ? ? String ip = request.getHeader("x-forwarded-for");?
? ? ? ? if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) { ?
? ? ? ? ? ? // 多次反向代理后會有多個ip值,第一個ip才是真實ip
? ? ? ? ? ? if( ip.indexOf(",")!=-1 ){
? ? ? ? ? ? ? ? ip = ip.split(",")[0];
? ? ? ? ? ? }
? ? ? ? } ?
? ? ? ? if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ?
? ? ? ? ? ? ip = request.getHeader("Proxy-Client-IP"); ?
? ? ? ? } ?
? ? ? ? if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ?
? ? ? ? ? ? ip = request.getHeader("WL-Proxy-Client-IP"); ?
? ? ? ? } ?
? ? ? ? if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ?
? ? ? ? ? ? ip = request.getHeader("HTTP_CLIENT_IP"); ?
? ? ? ? } ?
? ? ? ? if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ?
? ? ? ? ? ? ip = request.getHeader("HTTP_X_FORWARDED_FOR"); ?
? ? ? ? } ?
? ? ? ? if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ?
? ? ? ? ? ? ip = request.getHeader("X-Real-IP"); ?
? ? ? ? } ?
? ? ? ? if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ?
? ? ? ? ? ? ip = request.getRemoteAddr(); ?
? ? ? ? }?
? ? ? ? return ip; ?
? ? }
}
————————————————
版權聲明:本文為CSDN博主「CrazyL-」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/lovelovelovelovelo/article/details/78960913
總結
以上是生活随笔為你收集整理的IP暴露接口IP白名单设置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解锁密码忘了电脑如何锁密码忘了怎么办
- 下一篇: java通过poi读取excel中的日期