All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.hetianyi.common.util.HeaderUtil Maven / Gradle / Ivy

package com.github.hetianyi.common.util;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * http header通用操作工具类
 *
 * @author Jason He
 * @version 1.0.0
 * @since 1.0.0
 * @date 2020-01-18
 */
public class HeaderUtil {

    /**
     * 判断请求是否包含某个头部
     * @param request HttpServletRequest
     * @param name header name
     * @return
     */
    public static boolean containsHeader(HttpServletRequest request, String name) {
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            if (Objects.equals(headerNames.nextElement(), name)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据HttpServletRequest解析一些关于客户端IP的信息
     * @param request
     * @return
     */
    public static RealClientInfo parseIpInfo(HttpServletRequest request) {
        RealClientInfo info = new RealClientInfo();
        info.setRemoteAddress(request.getRemoteAddr());
        Enumeration headers = request.getHeaderNames();
        while (headers.hasMoreElements()) {
            info.setHost(request.getHeader("host"));
            info.setXRealIP(StringUtil.isNullOrEmptyTrimed(request.getHeader("x-real-ip")) ? null :
                    StringUtil.trim(request.getHeader("x-real-ip")));

            String xForwardedFor = request.getHeader("x-forwarded-for");
            if (StringUtil.isNullOrEmptyTrimed(xForwardedFor)) {
                info.setXForwardedFor(new String[]{});
            } else {
                if (!xForwardedFor.contains(",")) {
                    info.setXForwardedFor(new String[]{xForwardedFor.trim()});
                } else {
                    List splitList = Arrays.asList(xForwardedFor.split(","))
                                    .stream()
                                    .map(v -> v.trim())
                                    .filter(v -> !StringUtil.isNullOrEmpty(v))
                                    .collect(Collectors.toList());
                    info.setXForwardedFor(splitList.toArray(new String[]{}));
                }
            }
        }
        return info;
    }

    /**
     * http请求中和客户端真实ip相关的一些ip信息,根据系统自身的部署架构情况选择最真实的IP
     */
    public static class RealClientInfo {

        /**
         * 调用方远程地址
         */
        private String remoteAddress;

        /**
         * host
         */
        private String host;

        /**
         * 紧挨着的上游服务器或客户端的地址
         */
        private String xRealIP;

        /**
         * 反向代理调用链
         */
        private String[] xForwardedFor;

        public String getRemoteAddress() {
            return remoteAddress;
        }

        public void setRemoteAddress(String remoteAddress) {
            this.remoteAddress = remoteAddress;
        }

        public String getHost() {
            return host;
        }

        public void setHost(String host) {
            this.host = host;
        }

        public String getXRealIP() {
            return this.xRealIP;
        }

        public void setXRealIP(String xRealIP) {
            this.xRealIP = xRealIP;
        }

        public String[] getXForwardedFor() {
            return this.xForwardedFor;
        }

        public void setXForwardedFor(String[] xForwardedFor) {
            this.xForwardedFor = xForwardedFor;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy