Browse Source

直播添加ip归属地解析数据文件配成路程

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

+ 7
- 0
slive/pom.xml View File

@@ -92,6 +92,13 @@
92 92
             <artifactId>poi</artifactId>
93 93
             <version>3.17</version>
94 94
         </dependency>
95
+
96
+        <!--解析ip-->
97
+        <dependency>
98
+            <groupId>org.lionsoul</groupId>
99
+            <artifactId>ip2region</artifactId>
100
+            <version>2.6.6</version>
101
+        </dependency>
95 102
     </dependencies>
96 103
     <dependencyManagement>
97 104
         <dependencies>

+ 7
- 0
slive/src/main/java/com/xhkjedu/slive/config/ConfigKey.java View File

@@ -55,4 +55,11 @@ public class ConfigKey {
55 55
     public void setExpirationTimeInSeconds(Integer expirationTimeInSeconds) {
56 56
         ConfigKey.expirationTimeInSeconds = expirationTimeInSeconds;
57 57
     }
58
+
59
+    public static String ipregiondb;//解析ip地址归属地
60
+
61
+    @Value("${ipregiondb}")
62
+    public void setIpregiondb(String ipregiondb){
63
+        ConfigKey.ipregiondb = ipregiondb;
64
+    }
58 65
 }

+ 5
- 4
slive/src/main/java/com/xhkjedu/slive/controller/liveplay/CourseSectionStudentController.java View File

@@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
11 11
 import org.springframework.web.bind.annotation.RestController;
12 12
 
13 13
 import javax.annotation.Resource;
14
+import javax.servlet.http.HttpServletRequest;
14 15
 import java.util.List;
15 16
 
16 17
 /**
@@ -26,8 +27,8 @@ public class CourseSectionStudentController {
26 27
     private CourseSectionStudentService courseSectionStudentService;
27 28
 
28 29
     @PostMapping("/add")
29
-    public ResultVo add(@RequestBody LCourseSectionStudent student) {
30
-        courseSectionStudentService.save(student);
30
+    public ResultVo add(@RequestBody LCourseSectionStudent student, HttpServletRequest request) {
31
+        courseSectionStudentService.save(student,request);
31 32
         return new ResultVo(0, "保存成功");
32 33
     }
33 34
 
@@ -40,10 +41,10 @@ public class CourseSectionStudentController {
40 41
     }
41 42
 
42 43
     @PostMapping("/in_section")
43
-    public ResultVo inSection(@RequestBody LCourseSectionStudent student) {
44
+    public ResultVo inSection(@RequestBody LCourseSectionStudent student,HttpServletRequest request) {
44 45
         N_Utils.validation(new Object[]{student.getSectionid(),"课节id",1,student.getStudentid(),"学生id",1
45 46
                 ,student.getDevice(),"设备类型",2});
46
-        return courseSectionStudentService.inSection(student);
47
+        return courseSectionStudentService.inSection(student,request);
47 48
     }
48 49
 
49 50
     @PostMapping("/out_section")

+ 15
- 3
slive/src/main/java/com/xhkjedu/slive/service/liveplay/CourseSectionStudentService.java View File

@@ -4,6 +4,8 @@ import com.xhkjedu.slive.mapper.liveplay.CourseSectionMapper;
4 4
 import com.xhkjedu.slive.mapper.liveplay.CourseSectionStudentMapper;
5 5
 import com.xhkjedu.slive.model.liveplay.LCourseSection;
6 6
 import com.xhkjedu.slive.model.liveplay.LCourseSectionStudent;
7
+import com.xhkjedu.slive.utils.IpRegionUtil;
8
+import com.xhkjedu.slive.utils.LiveUtil;
7 9
 import com.xhkjedu.slive.vo.liveplay.CSStudentVo;
8 10
 import com.xhkjedu.slive.vo.liveplay.CourseVo;
9 11
 import com.xhkjedu.utils.N_Utils;
@@ -11,6 +13,7 @@ import com.xhkjedu.vo.ResultVo;
11 13
 import org.springframework.stereotype.Service;
12 14
 
13 15
 import javax.annotation.Resource;
16
+import javax.servlet.http.HttpServletRequest;
14 17
 import java.util.List;
15 18
 
16 19
 /**
@@ -27,11 +30,20 @@ public class CourseSectionStudentService {
27 30
     private CourseSectionMapper courseSectionMapper;
28 31
 
29 32
     //添加
30
-    public void save(LCourseSectionStudent model) {
33
+    public void save(LCourseSectionStudent model, HttpServletRequest request) {
31 34
         model.setCreatetime(N_Utils.getSecondTimestamp());
32 35
         if (model.getTotaltime() == null) {
33 36
             model.setTotaltime(0);
34 37
         }
38
+
39
+        String ip = LiveUtil.getIpAddress(request);
40
+        String region = null;
41
+        if(N_Utils.isNotEmpty(ip)){
42
+            IpRegionUtil ipRegionUtil = new IpRegionUtil();
43
+            region = ipRegionUtil.getIpForAddress(ip);
44
+        }
45
+        model.setLogip(ip);
46
+        model.setLogaddress(region);
35 47
         courseSectionStudentMapper.save(model);
36 48
         courseSectionMapper.updateStuNum(model.getSectionid());
37 49
     }
@@ -47,7 +59,7 @@ public class CourseSectionStudentService {
47 59
     }
48 60
 
49 61
     //学生进入课节
50
-    public ResultVo inSection(LCourseSectionStudent student) {
62
+    public ResultVo inSection(LCourseSectionStudent student,HttpServletRequest request) {
51 63
         CourseVo course = courseSectionMapper.getCourseBySectionId(student.getSectionid());
52 64
         if (course.getCoursestate() == 3 || course.getDeleted() == 10) return new ResultVo(1,"已结束");
53 65
         LCourseSectionStudent cs = courseSectionStudentMapper.getByStudent(student);//学生进入课节记录
@@ -58,7 +70,7 @@ public class CourseSectionStudentService {
58 70
         } else {//没有学生进入课节记录添加
59 71
             student.setTostamp(N_Utils.getSecondTimestamp());
60 72
             student.setOutstamp(0);
61
-            save(student);
73
+            save(student,request);
62 74
         }
63 75
         return new ResultVo(0,"学生进入课节成功");
64 76
     }

+ 54
- 0
slive/src/main/java/com/xhkjedu/slive/utils/IpRegionUtil.java View File

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

+ 40
- 4
slive/src/main/java/com/xhkjedu/slive/utils/LiveUtil.java View File

@@ -1,15 +1,17 @@
1 1
 package com.xhkjedu.slive.utils;
2 2
 
3 3
 import lombok.extern.slf4j.Slf4j;
4
+import org.apache.commons.lang.StringUtils;
4 5
 
5
-import java.text.SimpleDateFormat;
6
+import javax.servlet.http.HttpServletRequest;
6 7
 import java.time.DayOfWeek;
7 8
 import java.time.LocalDate;
8
-import java.time.ZoneId;
9 9
 import java.time.format.DateTimeFormatter;
10 10
 import java.time.temporal.ChronoUnit;
11
-import java.util.*;
12
-import java.util.stream.Collectors;
11
+import java.util.ArrayList;
12
+import java.util.HashMap;
13
+import java.util.List;
14
+import java.util.Map;
13 15
 import java.util.stream.Stream;
14 16
 
15 17
 /**
@@ -189,4 +191,38 @@ public class LiveUtil {
189 191
         return rtnlst;
190 192
     }
191 193
 
194
+    /**
195
+     * @Description 获取请求ip
196
+     * @Param [request]
197
+     * @Return java.lang.String
198
+     * @Author wn
199
+     * @Date 2022/11/27 22:26
200
+     **/
201
+    public static String getIpAddress(HttpServletRequest request){
202
+        String ip = request.getHeader("x-forwarded-for");
203
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
204
+            ip = request.getHeader("Proxy-Client-IP");
205
+        }
206
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
207
+            ip = request.getHeader("WL-Proxy-Client-IP");
208
+        }
209
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
210
+            ip = request.getHeader("HTTP_CLIENT_IP");
211
+        }
212
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
213
+            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
214
+        }
215
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
216
+            ip = request.getRemoteAddr();
217
+        }
218
+
219
+        if(StringUtils.isNotEmpty(ip)){
220
+            String[] ipArr = ip.split(",");
221
+            if(ipArr.length > 0){
222
+                ip = ipArr[0];
223
+            }
224
+        }
225
+        return ip;
226
+    }
227
+
192 228
 }

+ 3
- 0
slive/src/main/resources/application.properties View File

@@ -101,5 +101,8 @@ cron.updateLicense=0 0 0 * * ?
101 101
 #每隔1小时修改license状态
102 102
 cron.updateLicenseState=0 0 0/1 * * ?
103 103
 
104
+#解析ip归属地数据
105
+ipregiondb=D:\\school\\ip2region.xdb
106
+
104 107
 #版本号
105 108
 versionname=Test_v3.6.0

+ 7
- 0
suser/src/main/java/com/xhkjedu/suser/config/ConfigKey.java View File

@@ -51,4 +51,11 @@ public class ConfigKey {
51 51
     public void setSecretKey(String secretKey) {
52 52
         ConfigKey.secretKey = secretKey;
53 53
     }
54
+
55
+    public static String ipregiondb;//解析ip地址归属地
56
+
57
+    @Value("${ipregiondb}")
58
+    public void setIpregiondb(String ipregiondb){
59
+        ConfigKey.ipregiondb = ipregiondb;
60
+    }
54 61
 }

+ 2
- 1
suser/src/main/java/com/xhkjedu/suser/utils/IpRegionUtil.java View File

@@ -1,5 +1,6 @@
1 1
 package com.xhkjedu.suser.utils;
2 2
 
3
+import com.xhkjedu.suser.config.ConfigKey;
3 4
 import com.xhkjedu.utils.N_Utils;
4 5
 import lombok.extern.slf4j.Slf4j;
5 6
 import org.lionsoul.ip2region.xdb.Searcher;
@@ -17,7 +18,7 @@ public class IpRegionUtil {
17 18
     public String getIpForAddress(String ip){
18 19
         String region = "";
19 20
         // 1、创建 searcher 对象
20
-        String dbPath = "ip2region.xdb";
21
+        String dbPath = ConfigKey.ipregiondb;
21 22
         Searcher searcher = null;
22 23
         try {
23 24
             searcher = Searcher.newWithFileOnly(dbPath);

+ 4
- 0
suser/src/main/resources/application.properties View File

@@ -56,5 +56,9 @@ zt.url=http://api.mix2.zthysms.com/
56 56
 signName=【河南星火燎原】
57 57
 #郑州管城大数据SSO认证鉴权接口
58 58
 ssourl=http://gcdsj.zzgcjy.cn/portal/sso/api/agent
59
+
60
+#解析ip归属地数据
61
+ipregiondb=D:\\school\\ip2region.xdb
62
+
59 63
 #版本号
60 64
 versionname=Test_v3.6.0

Loading…
Cancel
Save