package cc.smtweb.framework.core.mvc.controller.binder.bean;

import cc.smtweb.framework.core.mvc.controller.binder.param.AbstractRequestEditor;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/***
 * 获取客户端IP地址;这里通过了Nginx获取;X-Real-IP
 */
public class SwIpAddrEditor extends AbstractRequestEditor {
    @Override
    public Object getValue(HttpServletRequest request) {
        String ip = request.getHeader("X-Real-IP");

        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }

        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }

        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        return ip;
    }
}
