Browse Source

获取ip并解析归属地

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

+ 6
- 0
suser/pom.xml View File

81
             <artifactId>mapper-spring-boot-starter</artifactId>
81
             <artifactId>mapper-spring-boot-starter</artifactId>
82
             <version>2.0.2</version>
82
             <version>2.0.2</version>
83
         </dependency>
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
     </dependencies>
91
     </dependencies>
86
     <dependencyManagement>
92
     <dependencyManagement>

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

4
 import com.xhkjedu.suser.model.system.TUser;
4
 import com.xhkjedu.suser.model.system.TUser;
5
 import com.xhkjedu.suser.model.system.TUserUsecret;
5
 import com.xhkjedu.suser.model.system.TUserUsecret;
6
 import com.xhkjedu.suser.service.system.UserService;
6
 import com.xhkjedu.suser.service.system.UserService;
7
+import com.xhkjedu.suser.utils.IpRegionUtil;
7
 import com.xhkjedu.suser.utils.UserUtil;
8
 import com.xhkjedu.suser.utils.UserUtil;
8
 import com.xhkjedu.suser.vo.assess.AssessParams;
9
 import com.xhkjedu.suser.vo.assess.AssessParams;
9
 import com.xhkjedu.utils.N_Utils;
10
 import com.xhkjedu.utils.N_Utils;
12
 import org.springframework.beans.factory.annotation.Value;
13
 import org.springframework.beans.factory.annotation.Value;
13
 import org.springframework.util.LinkedMultiValueMap;
14
 import org.springframework.util.LinkedMultiValueMap;
14
 import org.springframework.util.MultiValueMap;
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
 import org.springframework.web.multipart.MultipartFile;
19
 import org.springframework.web.multipart.MultipartFile;
20
 
20
 
21
 import javax.servlet.http.HttpServletRequest;
21
 import javax.servlet.http.HttpServletRequest;
22
 import java.util.List;
22
 import java.util.List;
23
 import java.util.Map;
23
 import java.util.Map;
24
+import java.util.TreeMap;
24
 
25
 
25
 /**
26
 /**
26
  * @ClassName UserController
27
  * @ClassName UserController
621
         userService.batchActivate(userids);
622
         userService.batchActivate(userids);
622
         return new ResultVo(0, "批量激活成功");
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
 import com.xhkjedu.suser.model.system.TUser;
15
 import com.xhkjedu.suser.model.system.TUser;
16
 import com.xhkjedu.suser.model.system.TUserLog;
16
 import com.xhkjedu.suser.model.system.TUserLog;
17
 import com.xhkjedu.suser.model.system.TUserUsecret;
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
 import com.xhkjedu.suser.vo.assess.AssessParams;
19
 import com.xhkjedu.suser.vo.assess.AssessParams;
23
 import com.xhkjedu.suser.vo.system.UserVo;
20
 import com.xhkjedu.suser.vo.system.UserVo;
24
 import com.xhkjedu.suser.vo.system.ZtMsgVo;
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
 import com.xhkjedu.vo.PageResult;
23
 import com.xhkjedu.vo.PageResult;
31
 import com.xhkjedu.vo.ResultVo;
24
 import com.xhkjedu.vo.ResultVo;
32
 import lombok.extern.slf4j.Slf4j;
25
 import lombok.extern.slf4j.Slf4j;
37
 
30
 
38
 import javax.annotation.Resource;
31
 import javax.annotation.Resource;
39
 import javax.servlet.http.HttpServletRequest;
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
 import java.util.stream.Collectors;
34
 import java.util.stream.Collectors;
46
 
35
 
47
 /**
36
 /**
344
         Integer usertype = user.getUsertype();
333
         Integer usertype = user.getUsertype();
345
         if (result.getCode() == 0) {
334
         if (result.getCode() == 0) {
346
             UserVo userVo = (UserVo) result.getObj();
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
             //redis
336
             //redis
368
             String ticketKey = UUID.randomUUID().toString().replace("-", "");
337
             String ticketKey = UUID.randomUUID().toString().replace("-", "");
369
             String token = DESUtils.encrypt(ticketKey, ConfigKey.secretKey);//加密
338
             String token = DESUtils.encrypt(ticketKey, ConfigKey.secretKey);//加密
388
             userLog.setUserid(user.getUserid());
357
             userLog.setUserid(user.getUserid());
389
             userLog.setUsername(user.getUsername());
358
             userLog.setUsername(user.getUsername());
390
             userLog.setSchoolid(user.getSchoolid());
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
             if(user.getUltype().equals("p_web") && usertype != null){
372
             if(user.getUltype().equals("p_web") && usertype != null){
394
                 //网页端学生和教师合并登录,前端传p_web,拼接完整的ultype
373
                 //网页端学生和教师合并登录,前端传p_web,拼接完整的ultype
395
                 if(usertype == 2){
374
                 if(usertype == 2){

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

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
 
2
 
3
 import com.xhkjedu.vo.ResultVo;
3
 import com.xhkjedu.vo.ResultVo;
4
 import lombok.extern.slf4j.Slf4j;
4
 import lombok.extern.slf4j.Slf4j;
5
+import org.apache.commons.lang.StringUtils;
5
 import org.springframework.http.HttpEntity;
6
 import org.springframework.http.HttpEntity;
6
 import org.springframework.http.HttpHeaders;
7
 import org.springframework.http.HttpHeaders;
7
 import org.springframework.http.MediaType;
8
 import org.springframework.http.MediaType;
10
 import org.springframework.web.client.RestClientException;
11
 import org.springframework.web.client.RestClientException;
11
 import org.springframework.web.client.RestTemplate;
12
 import org.springframework.web.client.RestTemplate;
12
 
13
 
14
+import javax.servlet.http.HttpServletRequest;
13
 import java.util.ArrayList;
15
 import java.util.ArrayList;
14
 import java.util.List;
16
 import java.util.List;
15
 
17
 
62
         }
64
         }
63
         return resultVo;
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