Java后台获取用户发送请求的一些信息

Scroll Down

Maven添加如下jar包:

<!—获取当前用户发送请求的一下信息—>
<dependency>
      <groupId>eu.bitwalker</groupId>
      <artifactId>UserAgentUtils</artifactId>
      <version>1.20</version>
</dependency>

代码:

import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import eu.bitwalker.useragentutils.Version;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/
  获取请求IP地址、浏览器相关信息
 /
public class IpAndAddrUtil {
    /
      获取发起请求的IP地址
     /
    public static String getIp(HttpServletRequest request) {
        String 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”) || ipAddress.equals(“0:0:0:0:0: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() = 15
            if(ipAddress.indexOf(“,”)>0){
                ipAddress = ipAddress.substring(0,ipAddress.indexOf(“,”));
            }
        }
        return ipAddress;
    }

    /
      获取发起请求的浏览器名称
     /
    public static String getBrowserName(HttpServletRequest request) {
        String header = request.getHeader(“User-Agent”);
        UserAgent userAgent = UserAgent.parseUserAgentString(header);
        Browser browser = userAgent.getBrowser();
        return browser.getName();
    }

    /
      获取发起请求的浏览器版本号
     /
    public static String getBrowserVersion(HttpServletRequest request) {
        String header = request.getHeader(“User-Agent”);
        UserAgent userAgent = UserAgent.parseUserAgentString(header);
        //获取浏览器信息
        Browser browser = userAgent.getBrowser();
        //获取浏览器版本号
        Version version = browser.getVersion(header);
        return version.getVersion();
    }

    /*
      获取发起请求的操作系统名称
     */
    public static String getOsName(HttpServletRequest request) {
        String header = request.getHeader(“User-Agent”);
        UserAgent userAgent = UserAgent.parseUserAgentString(header);
        OperatingSystem operatingSystem = userAgent.getOperatingSystem();
        return operatingSystem.getName();
    }
}