18
18
import cn .hutool .http .HttpUtil ;
19
19
import cn .hutool .json .JSONObject ;
20
20
import cn .hutool .json .JSONUtil ;
21
+ import lombok .extern .slf4j .Slf4j ;
22
+ import me .zhengjie .config .ElAdminProperties ;
23
+ import net .dreamlu .mica .ip2region .core .Ip2regionSearcher ;
24
+ import net .dreamlu .mica .ip2region .core .IpInfo ;
21
25
import nl .basjes .parse .useragent .UserAgent ;
22
26
import nl .basjes .parse .useragent .UserAgentAnalyzer ;
23
- import org .lionsoul .ip2region .DataBlock ;
24
- import org .lionsoul .ip2region .DbConfig ;
25
- import org .lionsoul .ip2region .DbSearcher ;
26
- import org .slf4j .Logger ;
27
- import org .slf4j .LoggerFactory ;
28
- import org .springframework .core .io .ClassPathResource ;
29
-
30
27
import javax .servlet .http .HttpServletRequest ;
31
- import java .io .File ;
32
- import java .net .Inet4Address ;
33
28
import java .net .InetAddress ;
34
29
import java .net .NetworkInterface ;
35
30
import java .net .UnknownHostException ;
41
36
* @author Zheng Jie
42
37
* 字符串工具类, 继承org.apache.commons.lang3.StringUtils类
43
38
*/
39
+ @ Slf4j
44
40
public class StringUtils extends org .apache .commons .lang3 .StringUtils {
45
41
46
- private static final Logger log = LoggerFactory .getLogger (StringUtils .class );
47
- private static boolean ipLocal = false ;
48
- private static File file = null ;
49
- private static DbConfig config ;
50
42
private static final char SEPARATOR = '_' ;
51
43
private static final String UNKNOWN = "unknown" ;
52
44
53
- private static final UserAgentAnalyzer userAgentAnalyzer = UserAgentAnalyzer
45
+ /**
46
+ * 注入bean
47
+ */
48
+ private final static Ip2regionSearcher IP_SEARCHER = SpringContextHolder .getBean (Ip2regionSearcher .class );
49
+
50
+
51
+ private static final UserAgentAnalyzer USER_AGENT_ANALYZER = UserAgentAnalyzer
54
52
.newBuilder ()
55
53
.hideMatcherLoadStats ()
56
54
.withCache (10000 )
57
55
.withField (UserAgent .AGENT_NAME_VERSION )
58
56
.build ();
59
57
60
-
61
- static {
62
- SpringContextHolder .addCallBacks (() -> {
63
- StringUtils .ipLocal = SpringContextHolder .getProperties ("ip.local-parsing" , false , Boolean .class );
64
- if (ipLocal ) {
65
- /*
66
- * 此文件为独享 ,不必关闭
67
- */
68
- String path = "ip2region/ip2region.db" ;
69
- String name = "ip2region.db" ;
70
- try {
71
- config = new DbConfig ();
72
- file = FileUtil .inputStreamToFile (new ClassPathResource (path ).getInputStream (), name );
73
- } catch (Exception e ) {
74
- log .error (e .getMessage (), e );
75
- }
76
- }
77
- });
78
- }
79
-
80
58
/**
81
59
* 驼峰命名法工具
82
60
*
@@ -196,7 +174,7 @@ public static String getIp(HttpServletRequest request) {
196
174
* 根据ip获取详细地址
197
175
*/
198
176
public static String getCityInfo (String ip ) {
199
- if (ipLocal ) {
177
+ if (ElAdminProperties . ipLocal ) {
200
178
return getLocalCityInfo (ip );
201
179
} else {
202
180
return getHttpCityInfo (ip );
@@ -216,27 +194,16 @@ public static String getHttpCityInfo(String ip) {
216
194
* 根据ip获取详细地址
217
195
*/
218
196
public static String getLocalCityInfo (String ip ) {
219
- try {
220
- DbSearcher dbSearcher = new DbSearcher (config , file .getPath ());
221
- DataBlock dataBlock = dbSearcher .binarySearch (ip );
222
- String region = dataBlock .getRegion ();
223
- String address = region .replace ("0|" , "" );
224
- char symbol = '|' ;
225
- if (address .charAt (address .length () - 1 ) == symbol ) {
226
- address = address .substring (0 , address .length () - 1 );
227
- }
228
- if (dataBlock !=null ){
229
- dbSearcher .close ();
230
- }
231
- return address .equals (ElAdminConstant .REGION ) ? "内网IP" : address ;
232
- } catch (Exception e ) {
233
- log .error (e .getMessage (), e );
197
+ IpInfo ipInfo = IP_SEARCHER .memorySearch (ip );
198
+ if (ipInfo != null ){
199
+ return ipInfo .getAddress ();
234
200
}
235
- return "" ;
201
+ return null ;
202
+
236
203
}
237
204
238
205
public static String getBrowser (HttpServletRequest request ) {
239
- UserAgent .ImmutableUserAgent userAgent = userAgentAnalyzer .parse (request .getHeader ("User-Agent" ));
206
+ UserAgent .ImmutableUserAgent userAgent = USER_AGENT_ANALYZER .parse (request .getHeader ("User-Agent" ));
240
207
return userAgent .get (UserAgent .AGENT_NAME_VERSION ).getValue ();
241
208
}
242
209
0 commit comments