Newer
Older
huludao / src / main / java / com / newfiber / led / business / util / HttpUtil.java
package com.newfiber.led.business.util;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description
 * @Auher: Administrator
 * @Data: 2021/5/7 0007 17:12
 */
public class HttpUtil {


    private HttpUtil() {

    }

    /**
     * 请求成功码
     */
    public static final String successCode = "200";
    /**
     * 用户新建或修改数据成功
     */
    public static final String saveOrUpdate = "201";
    /**
     * 请求失败码
     */
    public static final String failCode = "500";
    /**
     * 重新登陆码
     */
    public static final String re_loginCode = "100";
    /**
     * 没有权限
     */
    public static final String unauth = "301";

    /**
     * 验证码为空
     */
    public static final String CodeNull = "302";


    /**
     * 账号不存在
     */
    public static final String NoUser = "304";


    /**
     * 密码错误
     */
    public static final String PasswordErro = "305";


    /**
     * 验证码错误
     */
    public static final String CodeErro = "306";

    /**
     * 无效Token
     */
    public static final String NoToken = "307";

    /**
     * Token格式错误
     */
    public static final String TokenErr = "308";

    /**
     * 获取ip地址
     *
     * @param request
     * @return
     */
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
//      String ip = request.getHeader("Proxy-Client-IP");//处理IP
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            //X-Real-IP:nginx服务代理
            ip = request.getHeader("X-Real-IP");
        }
        if (ip != null) {
            ip = ip.split(",")[0];
//          if(ip.indexOf(",")>0){
//              ip=ip.substring(0,ip.indexOf(","));
//          }
        }
        return ip;
    }

}