diff --git a/continew-starter-core/pom.xml b/continew-starter-core/pom.xml index f021d45e..cb5a153a 100644 --- a/continew-starter-core/pom.xml +++ b/continew-starter-core/pom.xml @@ -52,6 +52,12 @@ spring-boot-configuration-processor + + + net.dreamlu + mica-ip2region + + cn.hutool diff --git a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/project/ProjectAutoConfiguration.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/project/ProjectAutoConfiguration.java new file mode 100644 index 00000000..36825cfe --- /dev/null +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/project/ProjectAutoConfiguration.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * 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 + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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 { +} diff --git a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/ProjectProperties.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/project/ProjectProperties.java similarity index 77% rename from continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/ProjectProperties.java rename to continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/project/ProjectProperties.java index c2b52390..3e0ba824 100644 --- a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/ProjectProperties.java +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/project/ProjectProperties.java @@ -14,12 +14,12 @@ * 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 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 */ @Data -@Component @ConfigurationProperties(prefix = "project") public class ProjectProperties { @@ -65,13 +64,11 @@ public class ProjectProperties { /** * 联系人 */ - @NestedConfigurationProperty private Contact contact; /** * 许可协议 */ - @NestedConfigurationProperty private License license; /** @@ -79,6 +76,17 @@ public class ProjectProperties { */ 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); + } + /** * 联系人配置属性 */ diff --git a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/IpUtils.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/IpUtils.java new file mode 100644 index 00000000..d0a29e74 --- /dev/null +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/IpUtils.java @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * 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 + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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 工具类 + * + *

+ * 使用本地解析时请提前引入 Hutool SpringUtil,详情请见引入方式。 + *

+ * + * @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); + } +} diff --git a/continew-starter-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 9887d7eb..5f54182d 100644 --- a/continew-starter-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/continew-starter-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -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.AsyncAutoConfiguration \ No newline at end of file +top.charles7c.continew.starter.core.autoconfigure.threadpool.AsyncAutoConfiguration +top.charles7c.continew.starter.core.autoconfigure.cors.CorsAutoConfiguration diff --git a/continew-starter-dependencies/pom.xml b/continew-starter-dependencies/pom.xml index 790dc855..12c2b552 100644 --- a/continew-starter-dependencies/pom.xml +++ b/continew-starter-dependencies/pom.xml @@ -64,6 +64,7 @@ 3.0.4 1.6.2 4.3.0 + 3.1.5.1 5.8.23 @@ -162,6 +163,13 @@ import
+ + + net.dreamlu + mica-ip2region + ${ip2region.version} + + cn.hutool