基于java配置nginx获取真实IP代码实例
作者:小猪夫 时间:2023-07-15 12:32:46
1、java代码
/** 获取客户端IP */
public static final String getClientIp(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)|| "127.0.0.1".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)|| "127.0.0.1".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)|| "127.0.0.1".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)|| "127.0.0.1".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
if (StringUtils.isBlank(ip) ||"127.0.0.1".equals(ip)|| ip.indexOf(":") > -1) {
try {
ip = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
ip = null;
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ip != null && ip.length() > 15) {
if (ip.indexOf(",") > 0) {
ip = ip.substring(0, ip.indexOf(","));
}
}
return ip;
}
2、nginx需要进行相应修改,重点 proxy_set_header
server {
listen xxxx;
server_name 127.0.0.1;
# 静态页面目录
root xxxxxxxxxx;
# 默认首页
index login.html;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET,POST';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
#proxy_cookie_path /* /*;
client_max_body_size 100m;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
......
}
}
来源:https://www.cnblogs.com/xiufengd/p/13625361.html
标签:java,获取,IP
0
投稿
猜你喜欢
Android倒计时的开始与停止 剩余时分秒的展示
2023-07-20 03:11:31
Java特性之注解和异常 Throwable
2023-04-22 13:14:53
MyBatisPlus+Lombok实现分页功能的方法详解
2021-07-04 06:34:57
Java中将File转化为MultipartFile的操作
2021-07-05 21:25:32
基于synchronized修饰静态和非静态方法
2021-10-30 06:58:03
Android实现记住账号密码功能
2021-10-02 01:51:24
Android仿微信朋友圈实现滚动条下拉反弹效果
2023-02-21 15:48:58
MyBatis动态SQL特性详解
2022-10-03 03:21:50
Spring Validation方法实现原理分析
2023-09-04 17:11:55
Java 爬虫工具Jsoup详解
2022-04-11 03:46:16
spring boot入门之诞生背景及优势影响
2021-09-16 06:31:57
Kotlin如何直接使用控件ID原理详析
2022-05-01 16:45:42
Java中lambda表达式实现aop切面功能
2022-12-02 09:44:59
java 读取excel文件转换成json格式的实例代码
2023-09-11 13:07:28
解决idea check out 切换分支时找不到需要的分支问题
2023-04-04 09:12:57
一个简单的toolabar结合drawlayout使用方法
2023-05-12 15:01:16
Java实现分布式系统限流
2022-05-31 22:38:05
深入C#中get与set的详解
2022-05-29 21:04:10
android layout 按比例布局的代码
2022-10-11 13:02:40
Android P实现静默安装的方法示例(官方Demo)
2022-04-05 20:06:13