1818import cn .hutool .http .HttpUtil ;
1919import cn .hutool .json .JSONObject ;
2020import 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 ;
2125import nl .basjes .parse .useragent .UserAgent ;
2226import 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-
3027import javax .servlet .http .HttpServletRequest ;
31- import java .io .File ;
32- import java .net .Inet4Address ;
3328import java .net .InetAddress ;
3429import java .net .NetworkInterface ;
3530import java .net .UnknownHostException ;
4136 * @author Zheng Jie
4237 * 字符串工具类, 继承org.apache.commons.lang3.StringUtils类
4338 */
39+ @ Slf4j
4440public class StringUtils extends org .apache .commons .lang3 .StringUtils {
4541
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 ;
5042 private static final char SEPARATOR = '_' ;
5143 private static final String UNKNOWN = "unknown" ;
5244
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
5452 .newBuilder ()
5553 .hideMatcherLoadStats ()
5654 .withCache (10000 )
5755 .withField (UserAgent .AGENT_NAME_VERSION )
5856 .build ();
5957
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-
8058 /**
8159 * 驼峰命名法工具
8260 *
@@ -196,7 +174,7 @@ public static String getIp(HttpServletRequest request) {
196174 * 根据ip获取详细地址
197175 */
198176 public static String getCityInfo (String ip ) {
199- if (ipLocal ) {
177+ if (ElAdminProperties . ipLocal ) {
200178 return getLocalCityInfo (ip );
201179 } else {
202180 return getHttpCityInfo (ip );
@@ -216,27 +194,16 @@ public static String getHttpCityInfo(String ip) {
216194 * 根据ip获取详细地址
217195 */
218196 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 ();
234200 }
235- return "" ;
201+ return null ;
202+
236203 }
237204
238205 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" ));
240207 return userAgent .get (UserAgent .AGENT_NAME_VERSION ).getValue ();
241208 }
242209
0 commit comments