HttpContextUtils.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package com.nosum.common.util;
  2. import eu.bitwalker.useragentutils.Browser;
  3. import eu.bitwalker.useragentutils.OperatingSystem;
  4. import eu.bitwalker.useragentutils.UserAgent;
  5. import eu.bitwalker.useragentutils.Version;
  6. import org.springframework.web.context.request.RequestContextHolder;
  7. import org.springframework.web.context.request.ServletRequestAttributes;
  8. import javax.servlet.http.HttpServletRequest;
  9. import java.net.InetAddress;
  10. import java.net.UnknownHostException;
  11. public class HttpContextUtils {
  12. public static HttpServletRequest getHttpServletRequest() {
  13. return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  14. }
  15. /**
  16. * 获取当前网络ip
  17. */
  18. public static String getIpAddr(HttpServletRequest request){
  19. String ipAddress = request.getHeader("x-forwarded-for");
  20. if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  21. ipAddress = request.getHeader("Proxy-Client-IP");
  22. }
  23. if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  24. ipAddress = request.getHeader("WL-Proxy-Client-IP");
  25. }
  26. if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  27. ipAddress = request.getRemoteAddr();
  28. if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){
  29. //根据网卡取本机配置的IP
  30. InetAddress inet=null;
  31. try {
  32. inet = InetAddress.getLocalHost();
  33. } catch (UnknownHostException e) {
  34. e.printStackTrace();
  35. }
  36. ipAddress= inet.getHostAddress();
  37. }
  38. }
  39. //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
  40. if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
  41. if(ipAddress.indexOf(",")>0){
  42. ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
  43. }
  44. }
  45. return ipAddress;
  46. }
  47. /**
  48. * 获取发起请求的浏览器名称
  49. */
  50. public static String getBrowserName(HttpServletRequest request) {
  51. try{
  52. String header = request.getHeader("User-Agent");
  53. UserAgent userAgent = UserAgent.parseUserAgentString(header);
  54. Browser browser = userAgent.getBrowser();
  55. return browser.getName();
  56. }catch (Exception e){
  57. return "not found name";
  58. }
  59. }
  60. /**
  61. * 获取发起请求的浏览器版本号
  62. */
  63. public static String getBrowserVersion(HttpServletRequest request) {
  64. try{
  65. String header = request.getHeader("User-Agent");
  66. UserAgent userAgent = UserAgent.parseUserAgentString(header);
  67. Browser browser = userAgent.getBrowser();// 获取浏览器信息
  68. Version version = browser.getVersion(header);// 获取浏览器版本号
  69. return version.getVersion();
  70. }catch (Exception e){
  71. return "not found version";
  72. }
  73. }
  74. /**
  75. * 获取发起请求的操作系统名称
  76. */
  77. public static String getOsName(HttpServletRequest request) {
  78. try{
  79. String header = request.getHeader("User-Agent");
  80. UserAgent userAgent = UserAgent.parseUserAgentString(header);
  81. OperatingSystem operatingSystem = userAgent.getOperatingSystem();
  82. return operatingSystem.getName();
  83. }catch (Exception e){
  84. return "not found osName";
  85. }
  86. }
  87. }