基于java配置nginx獲取真實IP代碼實例
更新時間:2020年09月18日 10:01:41 作者:小豬夫
這篇文章主要介紹了基于java配置nginx獲取真實IP代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
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;
# 靜態(tài)頁面目錄
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;
......
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringBoot利用Junit動態(tài)代理實現(xiàn)Mock方法
說到Spring Boot 單元測試主要有兩個主流集成分別是Mockito,Junit,這個各有特點,在實際開發(fā)中,我想要的測試框架應該是這個框架集成者,本文給大家介紹了SpringBoot利用Junit動態(tài)代理實現(xiàn)Mock方法,需要的朋友可以參考下2024-04-04
Spring Boot中利用JavaMailSender發(fā)送郵件的方法示例(附源碼)
這篇文章主要介紹了Spring Boot中利用JavaMailSender發(fā)送郵件的方法示例, 相信使用過Spring的眾多開發(fā)者都知道Spring提供了非常好用的JavaMailSender接口實現(xiàn)郵件發(fā)送。在Spring Boot的Starter模塊中也為此提供了自動化配置。需要的朋友可以參考借鑒。2017-02-02
詳解springboot+mybatis-plue實現(xiàn)內(nèi)置的CRUD使用詳情
這篇文章主要介紹了詳解springboot+mybatis-plue實現(xiàn)內(nèi)置的CRUD使用詳情,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
Spring Date jpa 獲取最新一條數(shù)據(jù)的實例代碼
這篇文章主要介紹了Spring Date jpa 獲取最新一條數(shù)據(jù)的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Mybatis使用collection標簽進行樹形結(jié)構(gòu)數(shù)據(jù)查詢時攜帶外部參數(shù)查詢
這篇文章主要介紹了Mybatis使用collection標簽進行樹形結(jié)構(gòu)數(shù)據(jù)查詢時攜帶外部參數(shù)查詢,需要的朋友可以參考下2023-10-10

