java获取http请求网络ip链条

java web服务端有时需要获取网络请求ip链条,用来做权限判断。

例如:1、只有请求经过某个固定ip链路才认为是合法的(必须是某个固定环境的请求)。2、请求一旦经过某个ip就认为非法(防非法代理挟持)。3、请求必须经过某个ip才认为合法(网络监控)。

代码如下:

public String getProxyIpLine(HttpServletRequest request) {
		StringBuilder ipLine = new StringBuilder("");
		String ip = request.getHeader("X-Real-IP");
		if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
			ipLine.append(ip).append(";");
		}
		ip = request.getHeader("X-Forwarded-For");
		if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
			ipLine.append(ip).append(";");
		}
		ip = request.getHeader("Proxy-Client-IP");
		if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
			ipLine.append(ip).append(";");
		}
		ip = request.getHeader("WL-Proxy-Client-IP");
		if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
			ipLine.append(ip).append(";");
		}
		ip = request.getHeader("HTTP_CLIENT_IP");
		if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
			ipLine.append(ip).append(";");
		}
		ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
			ipLine.append(ip).append(";");
		}
		ip = request.getRemoteAddr();
		if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
			ipLine.append(ip).append(";");
		}
		return ipLine.toString();
	}

每行代码含义参考:https://blog.csdn.net/javaee_gao/article/details/90212188 (不剽窃,只做个笔记)

 

author:蓝何忠

email:lanhezhong@163.com

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
通过tcping软件能够知道对应IP的某一端口是否能够连通。 -------------------------------------------------------------- tcping.exe by Eli Fulkerson Please see http://www.elifulkerson.com/projects/ for updates. -------------------------------------------------------------- Usage: C:\Users\dell1\Desktop\tcping64.exe [-flags] server-address [server-port] Usage (full): C:\Users\dell1\Desktop\tcping64.exe [-t] [-d] [-i interval] [-n ti mes] [-w ms] [-b n] [-r times] [-s] [-v] [-j] [-js size] [-4] [-6] [-c] [-g coun t] [-S source_address] [--file] [--tee filename] [-h] [-u] [--post] [--head] [-- proxy-port port] [--proxy-server server] [--proxy-credentials username:password] [-f] server-address [server-port] -t : ping continuously until stopped via control-c -n 5 : for instance, send 5 pings -i 5 : for instance, ping every 5 seconds -w 0.5 : for instance, wait 0.5 seconds for a response -d : include date and time on each line -b 1 : enable beeps (1 for on-down, 2 for on-up, 3 for on-change, 4 for always) -r 5 : for instance, relookup the hostname every 5 pings -s : automatically exit on a successful ping -v : print version and exit -j : include jitter, using default rolling average -js 5 : include jitter, with a rolling average size of (for instance) 5. --tee : mirror output to a filename specified after '--tee' -4 : prefer ipv4 -6 : prefer ipv6 -c : only show an output line on changed state --file : treat the "server-address" as a filename instead, loop through file li ne by line... 详细参数请查看”--help“

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值