Browse Source

获取ip并解析归属地

tags/正式版本
王宁 2 years ago
parent
commit
3239a1f462

+ 6
- 0
suser/pom.xml View File

@@ -81,6 +81,12 @@
81 81
             <artifactId>mapper-spring-boot-starter</artifactId>
82 82
             <version>2.0.2</version>
83 83
         </dependency>
84
+        <!--解析ip-->
85
+        <dependency>
86
+            <groupId>org.lionsoul</groupId>
87
+            <artifactId>ip2region</artifactId>
88
+            <version>2.6.6</version>
89
+        </dependency>
84 90
 
85 91
     </dependencies>
86 92
     <dependencyManagement>

+ 23
- 4
suser/src/main/java/com/xhkjedu/suser/controller/system/UserController.java View File

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
4 4
 import com.xhkjedu.suser.model.system.TUser;
5 5
 import com.xhkjedu.suser.model.system.TUserUsecret;
6 6
 import com.xhkjedu.suser.service.system.UserService;
7
+import com.xhkjedu.suser.utils.IpRegionUtil;
7 8
 import com.xhkjedu.suser.utils.UserUtil;
8 9
 import com.xhkjedu.suser.vo.assess.AssessParams;
9 10
 import com.xhkjedu.utils.N_Utils;
@@ -12,15 +13,15 @@ import org.springframework.beans.factory.annotation.Autowired;
12 13
 import org.springframework.beans.factory.annotation.Value;
13 14
 import org.springframework.util.LinkedMultiValueMap;
14 15
 import org.springframework.util.MultiValueMap;
15
-import org.springframework.web.bind.annotation.PostMapping;
16
-import org.springframework.web.bind.annotation.RequestBody;
17
-import org.springframework.web.bind.annotation.RequestMapping;
18
-import org.springframework.web.bind.annotation.RestController;
16
+import org.springframework.web.bind.annotation.*;
17
+import org.springframework.web.context.request.RequestAttributes;
18
+import org.springframework.web.context.request.RequestContextHolder;
19 19
 import org.springframework.web.multipart.MultipartFile;
20 20
 
21 21
 import javax.servlet.http.HttpServletRequest;
22 22
 import java.util.List;
23 23
 import java.util.Map;
24
+import java.util.TreeMap;
24 25
 
25 26
 /**
26 27
  * @ClassName UserController
@@ -621,4 +622,22 @@ public class UserController {
621 622
         userService.batchActivate(userids);
622 623
         return new ResultVo(0, "批量激活成功");
623 624
     }
625
+
626
+    @GetMapping("/ip")
627
+    public ResultVo getIpRegion(){
628
+        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
629
+        // 从获取RequestAttributes中获取HttpServletRequest的信息
630
+        HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
631
+
632
+        String ip = UserUtil.getIpAddress(request);
633
+        String region = "";
634
+        if(N_Utils.isNotEmpty(ip)){
635
+            IpRegionUtil ipRegionUtil = new IpRegionUtil();
636
+            region = ipRegionUtil.getIpForAddress(ip);
637
+        }
638
+        Map map = new TreeMap();
639
+        map.put("ip",ip);
640
+        map.put("region",region);
641
+        return new ResultVo(0,"获取成功",map);
642
+    }
624 643
 }

+ 15
- 36
suser/src/main/java/com/xhkjedu/suser/service/system/UserService.java View File

@@ -15,18 +15,11 @@ import com.xhkjedu.suser.model.system.TMsg;
15 15
 import com.xhkjedu.suser.model.system.TUser;
16 16
 import com.xhkjedu.suser.model.system.TUserLog;
17 17
 import com.xhkjedu.suser.model.system.TUserUsecret;
18
-import com.xhkjedu.suser.utils.JedisUtil;
19
-import com.xhkjedu.suser.utils.PoiUtils;
20
-import com.xhkjedu.suser.utils.SMSSender;
21
-import com.xhkjedu.suser.utils.UserUtil;
18
+import com.xhkjedu.suser.utils.*;
22 19
 import com.xhkjedu.suser.vo.assess.AssessParams;
23 20
 import com.xhkjedu.suser.vo.system.UserVo;
24 21
 import com.xhkjedu.suser.vo.system.ZtMsgVo;
25
-import com.xhkjedu.utils.DESUtils;
26
-import com.xhkjedu.utils.DatesUtil;
27
-import com.xhkjedu.utils.MD5;
28
-import com.xhkjedu.utils.N_Utils;
29
-import com.xhkjedu.utils.PageUtil;
22
+import com.xhkjedu.utils.*;
30 23
 import com.xhkjedu.vo.PageResult;
31 24
 import com.xhkjedu.vo.ResultVo;
32 25
 import lombok.extern.slf4j.Slf4j;
@@ -37,11 +30,7 @@ import org.springframework.web.multipart.MultipartFile;
37 30
 
38 31
 import javax.annotation.Resource;
39 32
 import javax.servlet.http.HttpServletRequest;
40
-import java.util.ArrayList;
41
-import java.util.HashMap;
42
-import java.util.List;
43
-import java.util.Map;
44
-import java.util.UUID;
33
+import java.util.*;
45 34
 import java.util.stream.Collectors;
46 35
 
47 36
 /**
@@ -344,26 +333,6 @@ public class UserService extends JedisUtil {
344 333
         Integer usertype = user.getUsertype();
345 334
         if (result.getCode() == 0) {
346 335
             UserVo userVo = (UserVo) result.getObj();
347
-            //以下代码登录的时候进行判断
348
-            /*if (userVo.getUserlock() == 1) {
349
-                result.setCode(1);
350
-                result.setMsg("此账号已锁定,禁止登录");
351
-                return result;
352
-            }
353
-            String usertime = userVo.getUsertime();
354
-            if (N_Utils.isNotEmpty(usertime)) {
355
-                String dayBegin = DatesUtil.getDayBegin();
356
-                if (usertime.compareTo(dayBegin) < 0) {
357
-                    result.setCode(1);
358
-                    if (usertype == 2) {//学生
359
-                        result.setMsg("账号已过有效期,请联系老师");
360
-                    } else {
361
-                        result.setMsg("账号已过有效期,请联系管理员");
362
-                    }
363
-                    return result;
364
-                }
365
-            }*/
366
-
367 336
             //redis
368 337
             String ticketKey = UUID.randomUUID().toString().replace("-", "");
369 338
             String token = DESUtils.encrypt(ticketKey, ConfigKey.secretKey);//加密
@@ -388,8 +357,18 @@ public class UserService extends JedisUtil {
388 357
             userLog.setUserid(user.getUserid());
389 358
             userLog.setUsername(user.getUsername());
390 359
             userLog.setSchoolid(user.getSchoolid());
391
-            userLog.setLogip(user.getLogip());
392
-            userLog.setLogaddress(user.getLogaddress());
360
+//            userLog.setLogip(user.getLogip());
361
+//            userLog.setLogaddress(user.getLogaddress());
362
+            //获取请求ip并解析区域
363
+            String ip = UserUtil.getIpAddress(request);
364
+            String region = null;
365
+            if(N_Utils.isNotEmpty(ip)){
366
+                IpRegionUtil ipRegionUtil = new IpRegionUtil();
367
+                region = ipRegionUtil.getIpForAddress(ip);
368
+            }
369
+            userLog.setLogip(ip);
370
+            userLog.setLogaddress(region);
371
+
393 372
             if(user.getUltype().equals("p_web") && usertype != null){
394 373
                 //网页端学生和教师合并登录,前端传p_web,拼接完整的ultype
395 374
                 if(usertype == 2){

+ 53
- 0
suser/src/main/java/com/xhkjedu/suser/utils/IpRegionUtil.java View File

@@ -0,0 +1,53 @@
1
+package com.xhkjedu.suser.utils;
2
+
3
+import com.xhkjedu.utils.N_Utils;
4
+import lombok.extern.slf4j.Slf4j;
5
+import org.lionsoul.ip2region.xdb.Searcher;
6
+
7
+import java.io.IOException;
8
+
9
+/**
10
+ * @Description 解析ip归属地
11
+ * @Author WN
12
+ * Date 2022/11/27 22:31
13
+ **/
14
+@Slf4j
15
+public class IpRegionUtil {
16
+
17
+    public String getIpForAddress(String ip){
18
+        String region = "";
19
+        // 1、创建 searcher 对象
20
+        String dbPath = "ip2region.xdb";
21
+        Searcher searcher = null;
22
+        try {
23
+            searcher = Searcher.newWithFileOnly(dbPath);
24
+        } catch (IOException e) {
25
+//            System.out.printf("failed to create searcher with `%s`: %s\n", dbPath, e);
26
+            log.error("创建searcher失败:"+ e.getMessage());
27
+            return null;
28
+        }
29
+
30
+        // 2、查询
31
+        try {
32
+//            long sTime = System.nanoTime();
33
+            region = searcher.search(ip);
34
+//            long cost = TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() - sTime));
35
+//            System.out.printf("{region: %s, ioCount: %d, took: %d μs}\n", region, searcher.getIOCount(), cost);
36
+            if(N_Utils.isNotEmpty(region)){
37
+                region = region.replace("0|","").replace("|"," ");
38
+            }
39
+        } catch (Exception e) {
40
+//            System.out.printf("failed to search(%s): %s\n", ip, e);
41
+            log.error("根据ip获取地址失败:"+ e.getMessage());
42
+            return null;
43
+        }
44
+
45
+        // 3、关闭资源
46
+        try {
47
+            searcher.close();
48
+        } catch (IOException e) {
49
+            e.printStackTrace();
50
+        }
51
+        return region;
52
+    }
53
+}

+ 36
- 0
suser/src/main/java/com/xhkjedu/suser/utils/UserUtil.java View File

@@ -2,6 +2,7 @@ package com.xhkjedu.suser.utils;
2 2
 
3 3
 import com.xhkjedu.vo.ResultVo;
4 4
 import lombok.extern.slf4j.Slf4j;
5
+import org.apache.commons.lang.StringUtils;
5 6
 import org.springframework.http.HttpEntity;
6 7
 import org.springframework.http.HttpHeaders;
7 8
 import org.springframework.http.MediaType;
@@ -10,6 +11,7 @@ import org.springframework.util.MultiValueMap;
10 11
 import org.springframework.web.client.RestClientException;
11 12
 import org.springframework.web.client.RestTemplate;
12 13
 
14
+import javax.servlet.http.HttpServletRequest;
13 15
 import java.util.ArrayList;
14 16
 import java.util.List;
15 17
 
@@ -62,4 +64,38 @@ public class UserUtil {
62 64
         }
63 65
         return resultVo;
64 66
     }
67
+
68
+    /**
69
+     * @Description 获取请求ip
70
+     * @Param [request]
71
+     * @Return java.lang.String
72
+     * @Author wn
73
+     * @Date 2022/11/27 22:26
74
+     **/
75
+    public static String getIpAddress(HttpServletRequest request){
76
+        String ip = request.getHeader("x-forwarded-for");
77
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
78
+            ip = request.getHeader("Proxy-Client-IP");
79
+        }
80
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
81
+            ip = request.getHeader("WL-Proxy-Client-IP");
82
+        }
83
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
84
+            ip = request.getHeader("HTTP_CLIENT_IP");
85
+        }
86
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
87
+            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
88
+        }
89
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
90
+            ip = request.getRemoteAddr();
91
+        }
92
+
93
+        if(StringUtils.isNotEmpty(ip)){
94
+            String[] ipArr = ip.split(",");
95
+            if(ipArr.length > 0){
96
+                ip = ipArr[0];
97
+            }
98
+        }
99
+        return ip;
100
+    }
65 101
 }

Loading…
Cancel
Save