Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring 获取请求 IP 地址 #100

Open
chanshiyucx opened this issue Dec 23, 2019 · 0 comments
Open

Spring 获取请求 IP 地址 #100

chanshiyucx opened this issue Dec 23, 2019 · 0 comments

Comments

@chanshiyucx
Copy link
Owner

chanshiyucx commented Dec 23, 2019

在 Spring 中,获取客户端真实 IP 地址的方法是 request.getRemoteAddr(),这种方法在大部分情况下都是有效的,但是在通过了 Squid 等反向代理软件就无法工作。

如果使用了反向代理软件,将 http://192.168.1.110:2046/ 的 URL 反向代理为 http://www.abc.com/ 的 URL 时,用request.getRemoteAddr() 方法获取的 IP 地址是 127.0.0.1 或 192.168.1.110,而并不是客户端的真实 IP。

经过代理以后,由于在客户端和服务之间增加了中间层,因此服务器无法直接拿到客户端的 IP,服务器端应用也无法直接通过转发请求的地址返回给客户端。但是在转发请求的 HTTP 头信息中,增加了 X-FORWARDED-FOR 信息,用以跟踪原有的客户端 IP 地址和原来客户端请求的服务器地址。

当我们访问 http://www.abc.com 时,其实并不是我们浏览器真正访问到了服务器上,而是先由代理服务器去访问 http://192.168.1.110:2046,代理服务器再将访问到的结果返回给我们的浏览器,因为是代理服务器去访问真实服务器,所以通过 request.getRemoteAddr() 的方法获取的 IP 实际上是代理服务器的地址,并不是客户端的 IP 地址。

下面是一种在 Java 服务器中获取请求 ip 的常见方式:

package com.titan.toolcenter.utils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author SHIYU
 * @date 2019/12/23 9:02
 * @description 获取请求真实IP
 */
public class IpUtil {

    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }
        return ipAddress;
    }
}

食用方式:

@RestController
@RequestMapping("/pay")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "支付管理", tags = {"支付管理"})
public class PayOrderController {

    /**
     * 头部信息
     */
    private final HttpServletRequest servletRequest;

     /**
     * 支付订单
     */
    @PostMapping("/order")
    @ApiOperation(value = "订单详情", notes = "订单详情")
    public CommJSONResult order(@ApiParam(value = "参数", required = true) @RequestBody PayOrderParams bean) throws Exception {
        String ip = IpUtil.getIpAddr(servletRequest);
    }

}

这里解释下这些请求头的意思:

X-Forwarded-For

这是一个 Squid 开发的字段,只有在通过了 HTTP 代理或者负载均衡服务器时才会添加该项。

格式为 X-Forwarded-For:client1,proxy1,proxy2,一般情况下,第一个 ip 为客户端真实 ip,后面的为经过的代理服务器 ip。现在大部分的代理都会加上这个请求头。

Proxy-Client-IP/WL- Proxy-Client-IP

这个一般是经过 apache http 服务器的请求才会有,用 apache http 做代理时一般会加上 Proxy-Client-IP 请求头,而 WL-Proxy-Client-IP 是他的 weblogic 插件加上的请求头。

需要注意几点:

  • 这些请求头都不是 http 协议里的标准请求头,也就是说这是各个代理服务器自己规定的表示客户端地址的请求头。如果哪天有一个代理服务器软件用 xxx-client-ip 这个请求头代表客户端请求,那上面的代码就不行了。
  • 这些请求头不是代理服务器一定会带上的,网络上的很多匿名代理就没有这些请求头,所以获取到的客户端 ip 不一定是真实的客户端 ip。代理服务器一般都可以自定义请求头设置。
  • 即使请求经过的代理都会按自己的规范附上代理请求头,上面的代码也不能确保获得的一定是客户端 ip。不同的网络架构,判断请求头的顺序是不一样的。
  • 最重要的一点,请求头都是可以伪造的。如果一些对客户端校验较严格的应用(比如投票)要获取客户端 ip,应该直接使用 request.getRemoteAddr(),虽然获取到的可能是代理的 ip 而不是客户端的 ip,但这个获取到的 ip 基本上是不可能伪造的,也就杜绝了刷票的可能。
@chanshiyucx chanshiyucx added this to the 技术向 milestone Dec 23, 2019
@chanshiyucx chanshiyucx modified the milestones: 技术向, 代码库 May 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant