IPService.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package com.key.common.plugs.ipaddr;
  2. import java.net.InetAddress;
  3. import java.net.UnknownHostException;
  4. import javax.servlet.http.HttpServletRequest;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Component;
  7. @Component
  8. public class IPService {
  9. @Autowired
  10. private IPSeeker ipSeeker;
  11. public String getIp(HttpServletRequest request) {
  12. //Http Header:X-Forwarded-For
  13. String ip = request.getHeader("X-Forwarded-For");
  14. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  15. ip = request.getHeader("Proxy-Client-IP");
  16. }
  17. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  18. ip = request.getHeader("WL-Proxy-Client-IP");
  19. }
  20. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  21. ip = request.getRemoteAddr();
  22. }
  23. // ip=replaceIPv6LocalIp(ip);
  24. ip=checkLocalIp(ip);
  25. if(ip!=null && ip.indexOf(",")>0){
  26. ip=ip.substring(0,ip.indexOf(","));
  27. }
  28. return ip;
  29. }
  30. /**
  31. * 检查以localhost,127.0.0.1访问时得到真实IP
  32. * @param ip
  33. * @return
  34. */
  35. public String checkLocalIp(String ip){
  36. if("0:0:0:0:0:0:0:1".equals(ip) || "127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1%0".equals(ip)){
  37. try {
  38. InetAddress inetAddress = InetAddress.getLocalHost();
  39. ip=inetAddress.getHostAddress();
  40. if(ip!=null && ip.length()>15){ //"***.***.***.***".length() = 15
  41. if(ip.indexOf(",")>0){
  42. ip = ip.substring(0,ip.indexOf(","));
  43. }
  44. }
  45. System.out.println(ip);
  46. } catch (UnknownHostException e) {
  47. // e.printStackTrace();
  48. }
  49. }
  50. return ip;
  51. }
  52. public String replaceIPv6LocalIp(String ip){
  53. if("0:0:0:0:0:0:0:1".equals(ip)){
  54. ip="127.0.0.1";
  55. }
  56. return ip;
  57. }
  58. public String getCountry(String ip) {
  59. if(ip==null){
  60. return "";
  61. }
  62. return ipSeeker.getCountry(ip);
  63. }
  64. /**
  65. * 根据IP,查出此ip所在的城市
  66. *
  67. * @param ip
  68. * @return
  69. */
  70. public String getCurCity(String ip) {
  71. String city = ipSeeker.getCountry(ip);
  72. city = city.replaceAll("\\S+省|市.*|[内蒙古|广西]", "");
  73. city = city.replaceAll("清华大学.*", "北京");
  74. return city;
  75. }
  76. /**
  77. * 根据 getCountry(ip);得到的地址得到城市
  78. *
  79. * @param country
  80. * @return
  81. */
  82. public String getCurCityByCountry(String country) {
  83. // 省..市
  84. // ..市
  85. // ..省..市..
  86. // ..省..
  87. // ..省
  88. // 清华大学=北京
  89. // 内蒙古..市
  90. // 内蒙古..
  91. // 广西..市
  92. String city = country;
  93. city = city.replaceAll("\\S+省|市.*|[内蒙古|广西]", "");
  94. city = city.replaceAll("清华大学.*", "北京");
  95. return city;
  96. }
  97. /**
  98. * 根据 request对象得到数据请求者所在的城市
  99. *
  100. * @param request
  101. * @return
  102. */
  103. public String getCurCity(HttpServletRequest request) {
  104. String ip = getIp(request);
  105. // ip="202.106.46.151";
  106. String city = ipSeeker.getCountry(ip);
  107. city = city.replaceAll("\\S+省|市.*|[内蒙古|广西]", "");
  108. city = city.replaceAll("清华大学.*", "北京");
  109. return city;
  110. }
  111. public static void main(String[] args) {
  112. String ip="111.206.20.59, 123.151.42.46, 121.42.17.215,";
  113. System.out.println(ip.substring(0,ip.indexOf(",")));
  114. }
  115. }