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;
......
}
}

最新文章

  1. 使用hexo,如果换了电脑怎么更新博客?
  2. 快排查找第K小的数
  3. 数据库 MySQL安装图解
  4. JXL操作Excel
  5. CODE[VS] 1230 元素查找
  6. URAL 1346. Intervals of Monotonicity(DP)
  7. jsp中的<jsp:setProperty>中的param属性
  8. linux第9天 UDP
  9. wince下sources\sources.cmn\Makefile.def的相关作用
  10. Assigning Host USB device to a Guest VM
  11. 开源PaaS产品介绍
  12. ruby编程语言-学习笔记2(第4章 表达式和操作符)
  13. ElasticSearch(3)-原理
  14. AutoTile 自动拼接(六 大结局) 学习与实践
  15. Spring Boot 1.5.4集成Redis
  16. 关于tomcat部署应用的三种方式
  17. NewSQL和TiDB入门
  18. C++11 使用 std::async创建异步程序
  19. 谁能当IBM公司的CEO?
  20. C++编程基础练习

热门文章

  1. VUE3 之 表单元素
  2. 【刷题-LeetCode】207. Course Schedule
  3. 《手把手教你》系列技巧篇(五十九)-java+ selenium自动化测试 - 截图三剑客 -上篇(详细教程)
  4. manjaro20软件商店无法链接下载
  5. golang中的标准库flag
  6. lambda表达式的学习
  7. 在EntityFrameworkCore中记录EF修改日志,保存,修改字段的原始值,当前值,表名等信息
  8. Discriminative Learning of Deep Convolutional Feature Point Descriptors 论文阅读笔记
  9. Luogu_P2048
  10. ApacheCN Kali Linux 译文集 20211020 更新