新增:新增系统监控模块(存放系统监控模块相关功能,例如:日志管理、服务监控等),新增操作日志引擎,记录 HTTP 请求信息

This commit is contained in:
2022-12-25 13:16:15 +08:00
parent 78e84e8941
commit 727850933f
28 changed files with 1523 additions and 12 deletions

View File

@@ -16,10 +16,14 @@
package top.charles7c.cnadmin.common.util;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/**
* 异常工具类
@@ -27,9 +31,38 @@ import lombok.NoArgsConstructor;
* @author Charles7c
* @since 2022/12/21 20:56
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ExceptionUtils {
/**
* 打印线程异常信息
*
* @param runnable
* 线程执行内容
* @param throwable
* 异常
*/
public static void printException(Runnable runnable, Throwable throwable) {
if (throwable == null && runnable instanceof Future<?>) {
try {
Future<?> future = (Future<?>)runnable;
if (future.isDone()) {
future.get();
}
} catch (CancellationException e) {
throwable = e;
} catch (ExecutionException e) {
throwable = e.getCause();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
if (throwable != null) {
log.error(throwable.getMessage(), throwable);
}
}
/**
* 如果有异常,返回 null
*

View File

@@ -0,0 +1,111 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.cnadmin.common.util;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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 top.charles7c.cnadmin.common.config.properties.ContinewAdminProperties;
import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
import net.dreamlu.mica.ip2region.core.IpInfo;
/**
* IP 工具类
*
* @author Charles7c
* @since 2022/12/23 20:00
*/
@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 (ContinewAdminProperties.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 (ipInfo != null) {
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);
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.cnadmin.common.util;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
/**
* Servlet 工具类
*
* @author Charles7c
* @since 2022/12/23 20:00
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ServletUtils {
/**
* 获取请求对象
*
* @return /
*/
public static HttpServletRequest getRequest() {
return getServletRequestAttributes().getRequest();
}
/**
* 获取响应对象
*
* @return /
*/
public static HttpServletResponse getResponse() {
return getServletRequestAttributes().getResponse();
}
private static ServletRequestAttributes getServletRequestAttributes() {
return (ServletRequestAttributes)Objects.requireNonNull(RequestContextHolder.getRequestAttributes());
}
/**
* 获取浏览器及其版本信息
*
* @param request
* 请求信息
* @return 浏览器及其版本信息
*/
public static String getBrowser(HttpServletRequest request) {
if (request == null) {
return null;
}
UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));
return userAgent.getBrowser().getName() + " " + userAgent.getVersion();
}
/**
* 获取响应所有的头header信息
*
* @param response
* 响应对象{@link HttpServletResponse}
* @return header值
*/
public static Map<String, Collection<String>> getHeaderMap(HttpServletResponse response) {
final Map<String, Collection<String>> headerMap = new HashMap<>();
final Collection<String> names = response.getHeaderNames();
for (String name : names) {
headerMap.put(name, response.getHeaders(name));
}
return headerMap;
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.cnadmin.common.util.holder;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import top.charles7c.cnadmin.common.model.dto.OperationLog;
/**
* 操作日志上下文持有者
*
* @author Charles7c
* @since 2022/12/25 8:55
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class LogContextHolder {
private static final ThreadLocal<OperationLog> LOG_THREAD_LOCAL = new ThreadLocal<>();
/**
* 存储操作日志
*
* @param operationLog
* 操作日志信息
*/
public static void set(OperationLog operationLog) {
LOG_THREAD_LOCAL.set(operationLog);
}
/**
* 获取操作日志
*
* @return 操作日志信息
*/
public static OperationLog get() {
return LOG_THREAD_LOCAL.get();
}
/**
* 移除操作日志
*/
public static void remove() {
LOG_THREAD_LOCAL.remove();
}
}