-
Notifications
You must be signed in to change notification settings - Fork 645
/
IPForge.java
47 lines (39 loc) · 1.28 KB
/
IPForge.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package org.joychou.controller;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* Java get real ip. More details: https://joychou.org/web/how-to-get-real-ip.html
*
* @author JoyChou @ 2017-12-29
*/
@RestController
@RequestMapping("/ip")
public class IPForge {
// no any proxy
@RequestMapping("/noproxy")
public static String noProxy(HttpServletRequest request) {
return request.getRemoteAddr();
}
/**
* proxy_set_header X-Real-IP $remote_addr;
* proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for
* if code used X-Forwarded-For to get ip, the code must be vulnerable.
*/
@RequestMapping("/proxy")
@ResponseBody
public static String proxy(HttpServletRequest request) {
String ip = request.getHeader("X-Real-IP");
if (StringUtils.isNotBlank(ip)) {
return ip;
} else {
String remoteAddr = request.getRemoteAddr();
if (StringUtils.isNotBlank(remoteAddr)) {
return remoteAddr;
}
}
return "";
}
}