forked from JoyChou93/java-sec-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPForge.java
More file actions
49 lines (41 loc) · 1.37 KB
/
IPForge.java
File metadata and controls
49 lines (41 loc) · 1.37 KB
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
48
49
package org.joychou.controller;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
/**
* @author: JoyChou (joychou@joychou.org)
* @date: 2017.12.29
* @desc: Java获取IP安全代码
* @detail: 关于获取IP不安全代码,详情可查看https://joychou.org/web/how-to-get-real-ip.html
*/
@Controller
@RequestMapping("/ip")
public class IPForge {
// no any proxy
@RequestMapping("/noproxy")
@ResponseBody
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 "";
}
}