mirror of
				https://github.com/continew-org/continew-starter.git
				synced 2025-10-31 22:57:19 +08:00 
			
		
		
		
	feat: 新增项目自动配置,封装 IPUtils 工具类
This commit is contained in:
		| @@ -52,6 +52,12 @@ | |||||||
|             <artifactId>spring-boot-configuration-processor</artifactId> |             <artifactId>spring-boot-configuration-processor</artifactId> | ||||||
|         </dependency> |         </dependency> | ||||||
|  |  | ||||||
|  |         <!-- 第三方封装 Ip2region(离线 IP 数据管理框架和定位库,支持亿级别的数据段,10 微秒级别的查询性能,提供了许多主流编程语言的 xdb 数据管理引擎的实现) --> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>net.dreamlu</groupId> | ||||||
|  |             <artifactId>mica-ip2region</artifactId> | ||||||
|  |         </dependency> | ||||||
|  |  | ||||||
|         <!-- Hutool(小而全的 Java 工具类库,通过静态方法封装,降低相关 API 的学习成本,提高工作效率,使 Java 拥有函数式语言般的优雅,让 Java 语言也可以“甜甜的”) --> |         <!-- Hutool(小而全的 Java 工具类库,通过静态方法封装,降低相关 API 的学习成本,提高工作效率,使 Java 拥有函数式语言般的优雅,让 Java 语言也可以“甜甜的”) --> | ||||||
|         <dependency> |         <dependency> | ||||||
|             <groupId>cn.hutool</groupId> |             <groupId>cn.hutool</groupId> | ||||||
|   | |||||||
| @@ -0,0 +1,33 @@ | |||||||
|  | /* | ||||||
|  |  * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. | ||||||
|  |  * <p> | ||||||
|  |  * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; | ||||||
|  |  * you may not use this file except in compliance with the License. | ||||||
|  |  * You may obtain a copy of the License at | ||||||
|  |  * <p> | ||||||
|  |  * http://www.gnu.org/licenses/lgpl.html | ||||||
|  |  * <p> | ||||||
|  |  * Unless required by applicable law or agreed to in writing, software | ||||||
|  |  * distributed under the License is distributed on an "AS IS" BASIS, | ||||||
|  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
|  |  * See the License for the specific language governing permissions and | ||||||
|  |  * limitations under the License. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | package top.charles7c.continew.starter.core.autoconfigure.project; | ||||||
|  |  | ||||||
|  | import lombok.extern.slf4j.Slf4j; | ||||||
|  | import org.springframework.boot.autoconfigure.AutoConfiguration; | ||||||
|  | import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * 项目自动配置 | ||||||
|  |  * | ||||||
|  |  * @author Charles7c | ||||||
|  |  * @since 1.0.0 | ||||||
|  |  */ | ||||||
|  | @Slf4j | ||||||
|  | @AutoConfiguration | ||||||
|  | @EnableConfigurationProperties(ProjectProperties.class) | ||||||
|  | public class ProjectAutoConfiguration { | ||||||
|  | } | ||||||
| @@ -14,12 +14,12 @@ | |||||||
|  * limitations under the License. |  * limitations under the License. | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| package top.charles7c.continew.starter.core.autoconfigure; | package top.charles7c.continew.starter.core.autoconfigure.project; | ||||||
| 
 | 
 | ||||||
|  | import cn.hutool.core.convert.Convert; | ||||||
|  | import cn.hutool.extra.spring.SpringUtil; | ||||||
| import lombok.Data; | import lombok.Data; | ||||||
| import org.springframework.boot.context.properties.ConfigurationProperties; | import org.springframework.boot.context.properties.ConfigurationProperties; | ||||||
| import org.springframework.boot.context.properties.NestedConfigurationProperty; |  | ||||||
| import org.springframework.stereotype.Component; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * 项目配置属性 |  * 项目配置属性 | ||||||
| @@ -28,7 +28,6 @@ import org.springframework.stereotype.Component; | |||||||
|  * @since 1.0.0 |  * @since 1.0.0 | ||||||
|  */ |  */ | ||||||
| @Data | @Data | ||||||
| @Component |  | ||||||
| @ConfigurationProperties(prefix = "project") | @ConfigurationProperties(prefix = "project") | ||||||
| public class ProjectProperties { | public class ProjectProperties { | ||||||
| 
 | 
 | ||||||
| @@ -65,13 +64,11 @@ public class ProjectProperties { | |||||||
|     /** |     /** | ||||||
|      * 联系人 |      * 联系人 | ||||||
|      */ |      */ | ||||||
|     @NestedConfigurationProperty |  | ||||||
|     private Contact contact; |     private Contact contact; | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * 许可协议 |      * 许可协议 | ||||||
|      */ |      */ | ||||||
|     @NestedConfigurationProperty |  | ||||||
|     private License license; |     private License license; | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
| @@ -79,6 +76,17 @@ public class ProjectProperties { | |||||||
|      */ |      */ | ||||||
|     private boolean production = false; |     private boolean production = false; | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * 是否启用本地解析 IP 归属地 | ||||||
|  |      */ | ||||||
|  |     public static final boolean IP_ADDR_LOCAL_PARSE_ENABLED; | ||||||
|  | 
 | ||||||
|  |     static { | ||||||
|  |         String underlineCaseProperty = SpringUtil.getProperty("ip-addr-local-parse-enabled"); | ||||||
|  |         String camelCaseProperty = SpringUtil.getProperty("ipAddrLocalParseEnabled"); | ||||||
|  |         IP_ADDR_LOCAL_PARSE_ENABLED = Convert.toBool(underlineCaseProperty, false) || Convert.toBool(camelCaseProperty, false); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     /** |     /** | ||||||
|      * 联系人配置属性 |      * 联系人配置属性 | ||||||
|      */ |      */ | ||||||
| @@ -0,0 +1,112 @@ | |||||||
|  | /* | ||||||
|  |  * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. | ||||||
|  |  * <p> | ||||||
|  |  * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; | ||||||
|  |  * you may not use this file except in compliance with the License. | ||||||
|  |  * You may obtain a copy of the License at | ||||||
|  |  * <p> | ||||||
|  |  * http://www.gnu.org/licenses/lgpl.html | ||||||
|  |  * <p> | ||||||
|  |  * Unless required by applicable law or agreed to in writing, software | ||||||
|  |  * distributed under the License is distributed on an "AS IS" BASIS, | ||||||
|  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
|  |  * See the License for the specific language governing permissions and | ||||||
|  |  * limitations under the License. | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | package top.charles7c.continew.starter.core.util; | ||||||
|  |  | ||||||
|  | import cn.hutool.core.net.NetUtil; | ||||||
|  | import cn.hutool.extra.spring.SpringUtil; | ||||||
|  | import cn.hutool.http.HtmlUtil; | ||||||
|  | import cn.hutool.http.HttpUtil; | ||||||
|  | import cn.hutool.json.JSONObject; | ||||||
|  | import cn.hutool.json.JSONUtil; | ||||||
|  | import lombok.AccessLevel; | ||||||
|  | import lombok.NoArgsConstructor; | ||||||
|  | import lombok.extern.slf4j.Slf4j; | ||||||
|  | import net.dreamlu.mica.ip2region.core.Ip2regionSearcher; | ||||||
|  | import net.dreamlu.mica.ip2region.core.IpInfo; | ||||||
|  | import top.charles7c.continew.starter.core.autoconfigure.project.ProjectProperties; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * IP 工具类 | ||||||
|  |  * | ||||||
|  |  * <p> | ||||||
|  |  *     使用本地解析时请提前引入 Hutool SpringUtil,详情请见<a href="https://doc.hutool.cn/pages/SpringUtil">引入方式</a>。 | ||||||
|  |  * </p> | ||||||
|  |  * | ||||||
|  |  * @author Charles7c | ||||||
|  |  * @since 1.0.0 | ||||||
|  |  */ | ||||||
|  | @Slf4j | ||||||
|  | @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||||||
|  | public class IpUtils { | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 太平洋网开放 API:查询 IP 归属地 | ||||||
|  |      */ | ||||||
|  |     private static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp?ip=%s&json=true"; | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 根据 IP 获取归属地信息 | ||||||
|  |      * | ||||||
|  |      * @param ip | ||||||
|  |      *            IP 地址 | ||||||
|  |      * @return 归属地信息 | ||||||
|  |      */ | ||||||
|  |     public static String getCityInfo(String ip) { | ||||||
|  |         if (ProjectProperties.IP_ADDR_LOCAL_PARSE_ENABLED) { | ||||||
|  |             return getLocalCityInfo(ip); | ||||||
|  |         } else { | ||||||
|  |             return getHttpCityInfo(ip); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 根据 IP 获取归属地信息(网络解析) | ||||||
|  |      * | ||||||
|  |      * @param ip | ||||||
|  |      *            IP 地址 | ||||||
|  |      * @return 归属地信息 | ||||||
|  |      */ | ||||||
|  |     public static String getHttpCityInfo(String ip) { | ||||||
|  |         if (isInnerIp(ip)) { | ||||||
|  |             return "内网IP"; | ||||||
|  |         } | ||||||
|  |         String api = String.format(IP_URL, ip); | ||||||
|  |         JSONObject object = JSONUtil.parseObj(HttpUtil.get(api)); | ||||||
|  |         return object.get("addr", String.class); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 根据 IP 获取归属地信息(本地解析) | ||||||
|  |      * | ||||||
|  |      * @param ip | ||||||
|  |      *            IP 地址 | ||||||
|  |      * @return 归属地信息 | ||||||
|  |      */ | ||||||
|  |     public static String getLocalCityInfo(String ip) { | ||||||
|  |         if (isInnerIp(ip)) { | ||||||
|  |             return "内网IP"; | ||||||
|  |         } | ||||||
|  |         Ip2regionSearcher ip2regionSearcher = SpringUtil.getBean(Ip2regionSearcher.class); | ||||||
|  |         IpInfo ipInfo = ip2regionSearcher.memorySearch(ip); | ||||||
|  |         if (null != ipInfo) { | ||||||
|  |             return ipInfo.getAddress(); | ||||||
|  |         } | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 是否为内网 IPv4 | ||||||
|  |      * | ||||||
|  |      * @param ip | ||||||
|  |      *            IP 地址 | ||||||
|  |      * @return 是否为内网 IP | ||||||
|  |      */ | ||||||
|  |     public static boolean isInnerIp(String ip) { | ||||||
|  |         ip = "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : HtmlUtil.cleanHtmlTag(ip); | ||||||
|  |         return NetUtil.isInnerIP(ip); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -1,3 +1,4 @@ | |||||||
| top.charles7c.continew.starter.core.autoconfigure.cors.CorsAutoConfiguration | top.charles7c.continew.starter.core.autoconfigure.project.ProjectAutoConfiguration | ||||||
| top.charles7c.continew.starter.core.autoconfigure.threadpool.ThreadPoolAutoConfiguration | top.charles7c.continew.starter.core.autoconfigure.threadpool.ThreadPoolAutoConfiguration | ||||||
| top.charles7c.continew.starter.core.autoconfigure.threadpool.AsyncAutoConfiguration | top.charles7c.continew.starter.core.autoconfigure.threadpool.AsyncAutoConfiguration | ||||||
|  | top.charles7c.continew.starter.core.autoconfigure.cors.CorsAutoConfiguration | ||||||
|   | |||||||
| @@ -64,6 +64,7 @@ | |||||||
|         <sms4j.version>3.0.4</sms4j.version> |         <sms4j.version>3.0.4</sms4j.version> | ||||||
|         <easy-captcha.version>1.6.2</easy-captcha.version> |         <easy-captcha.version>1.6.2</easy-captcha.version> | ||||||
|         <knife4j.version>4.3.0</knife4j.version> |         <knife4j.version>4.3.0</knife4j.version> | ||||||
|  |         <ip2region.version>3.1.5.1</ip2region.version> | ||||||
|         <hutool.version>5.8.23</hutool.version> |         <hutool.version>5.8.23</hutool.version> | ||||||
|     </properties> |     </properties> | ||||||
|  |  | ||||||
| @@ -162,6 +163,13 @@ | |||||||
|                 <scope>import</scope> |                 <scope>import</scope> | ||||||
|             </dependency> |             </dependency> | ||||||
|  |  | ||||||
|  |             <!-- 第三方封装 Ip2region(离线 IP 数据管理框架和定位库,支持亿级别的数据段,10 微秒级别的查询性能,提供了许多主流编程语言的 xdb 数据管理引擎的实现) --> | ||||||
|  |             <dependency> | ||||||
|  |                 <groupId>net.dreamlu</groupId> | ||||||
|  |                 <artifactId>mica-ip2region</artifactId> | ||||||
|  |                 <version>${ip2region.version}</version> | ||||||
|  |             </dependency> | ||||||
|  |  | ||||||
|             <!-- Hutool(小而全的 Java 工具类库,通过静态方法封装,降低相关 API 的学习成本,提高工作效率,使 Java 拥有函数式语言般的优雅,让 Java 语言也可以“甜甜的”) --> |             <!-- Hutool(小而全的 Java 工具类库,通过静态方法封装,降低相关 API 的学习成本,提高工作效率,使 Java 拥有函数式语言般的优雅,让 Java 语言也可以“甜甜的”) --> | ||||||
|             <dependency> |             <dependency> | ||||||
|                 <groupId>cn.hutool</groupId> |                 <groupId>cn.hutool</groupId> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user