mirror of https://github.com/elunez/eladmin
# 以后日志记录IP 获取 支持,在线与离线方式
parent
cf13fa922c
commit
6d964982d5
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Copyright 2019-2020 the original author or authors.
|
||||
*
|
||||
* 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 me.zhengjie.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author: liaojinlong
|
||||
* @date: 2020/6/9 17:02
|
||||
* @since: 1.0
|
||||
* @see {@link SpringContextHolder}
|
||||
* 针对某些初始化方法,在SpringContextHolder 初始化前时,<br>
|
||||
* 可提交一个 提交回调任务。<br>
|
||||
* 在SpringContextHolder 初始化后,进行回调使用
|
||||
*/
|
||||
|
||||
public interface CallBack {
|
||||
/**
|
||||
* 回调执行方法
|
||||
*/
|
||||
void executor();
|
||||
|
||||
/**
|
||||
* 本回调任务名称
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
default String getCallBackName() {
|
||||
return Thread.currentThread().getId() + ":" + this.getClass().getName();
|
||||
}
|
||||
}
|
||||
|
|
@ -17,11 +17,16 @@ package me.zhengjie.utils;
|
|||
|
||||
/**
|
||||
* 常用静态常量
|
||||
*
|
||||
* @author Zheng Jie
|
||||
* @date 2018-12-26
|
||||
*/
|
||||
public class ElAdminConstant {
|
||||
|
||||
/**
|
||||
* 用于IP定位转换
|
||||
*/
|
||||
public static final String REGION = "内网IP|内网IP";
|
||||
/**
|
||||
* win 系统
|
||||
*/
|
||||
|
|
|
@ -22,6 +22,7 @@ import cn.hutool.poi.excel.ExcelUtil;
|
|||
import me.zhengjie.exception.BadRequestException;
|
||||
import org.apache.poi.util.IOUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
@ -35,6 +36,7 @@ import java.util.Map;
|
|||
|
||||
/**
|
||||
* File工具类,扩展 hutool 工具包
|
||||
*
|
||||
* @author Zheng Jie
|
||||
* @date 2018-12-27
|
||||
*/
|
||||
|
@ -124,6 +126,26 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
|
|||
return resultSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* inputStream 转 File
|
||||
*/
|
||||
static File inputStreamToFile(InputStream ins, String name) throws Exception {
|
||||
File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
|
||||
if (file.exists()) {
|
||||
return file;
|
||||
}
|
||||
OutputStream os = new FileOutputStream(file);
|
||||
int bytesRead;
|
||||
int len = 8192;
|
||||
byte[] buffer = new byte[len];
|
||||
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
|
||||
os.write(buffer, 0, bytesRead);
|
||||
}
|
||||
os.close();
|
||||
ins.close();
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将文件名解析成文件的上传路径
|
||||
*/
|
||||
|
@ -257,6 +279,7 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
|
|||
|
||||
/**
|
||||
* 下载文件
|
||||
*
|
||||
* @param request /
|
||||
* @param response /
|
||||
* @param file /
|
||||
|
|
|
@ -20,6 +20,10 @@ import org.springframework.beans.BeansException;
|
|||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Jie
|
||||
|
@ -29,6 +33,23 @@ import org.springframework.context.ApplicationContextAware;
|
|||
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {
|
||||
|
||||
private static ApplicationContext applicationContext = null;
|
||||
private static List<CallBack> callBacks = new ArrayList<>();
|
||||
private static boolean addCallback = true;
|
||||
|
||||
/**
|
||||
* 针对 某些初始化方法,在SpringContextHolder 未初始化时 提交回调方法。
|
||||
* 在SpringContextHolder 初始化后,进行回调使用
|
||||
*
|
||||
* @param callBack 回调函数
|
||||
*/
|
||||
public synchronized static void addCallBacks(CallBack callBack) {
|
||||
if (addCallback) {
|
||||
SpringContextHolder.callBacks.add(callBack);
|
||||
} else {
|
||||
log.warn("CallBack:{} 已无法添加!立即执行", callBack.getCallBackName());
|
||||
callBack.executor();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
||||
|
@ -47,6 +68,44 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB
|
|||
return applicationContext.getBean(requiredType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取SpringBoot 配置信息
|
||||
*
|
||||
* @param property 属性key
|
||||
* @param defaultValue 默认值
|
||||
* @param requiredType 返回类型
|
||||
* @return
|
||||
*/
|
||||
public static <T> T getProperties(String property, T defaultValue, Class<T> requiredType) {
|
||||
T result = defaultValue;
|
||||
try {
|
||||
result = getBean(Environment.class).getProperty(property, requiredType);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取SpringBoot 配置信息
|
||||
*
|
||||
* @param property 属性key
|
||||
* @return
|
||||
*/
|
||||
public static String getProperties(String property) {
|
||||
return getProperties(property, null, String.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取SpringBoot 配置信息
|
||||
*
|
||||
* @param property 属性key
|
||||
* @param requiredType 返回类型
|
||||
* @return
|
||||
*/
|
||||
public static <T> T getProperties(String property, Class<T> requiredType) {
|
||||
return getProperties(property, null, requiredType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查ApplicationContext不为空.
|
||||
*/
|
||||
|
@ -77,5 +136,12 @@ public class SpringContextHolder implements ApplicationContextAware, DisposableB
|
|||
log.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:" + SpringContextHolder.applicationContext);
|
||||
}
|
||||
SpringContextHolder.applicationContext = applicationContext;
|
||||
if (addCallback) {
|
||||
for (CallBack callBack : SpringContextHolder.callBacks) {
|
||||
callBack.executor();
|
||||
}
|
||||
callBacks.clear();
|
||||
}
|
||||
SpringContextHolder.addCallback = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,18 @@ import cn.hutool.json.JSONObject;
|
|||
import cn.hutool.json.JSONUtil;
|
||||
import eu.bitwalker.useragentutils.Browser;
|
||||
import eu.bitwalker.useragentutils.UserAgent;
|
||||
import org.lionsoul.ip2region.DataBlock;
|
||||
import org.lionsoul.ip2region.DbConfig;
|
||||
import org.lionsoul.ip2region.DbMakerConfigException;
|
||||
import org.lionsoul.ip2region.DbSearcher;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Calendar;
|
||||
|
@ -31,6 +42,34 @@ import java.util.Date;
|
|||
* 字符串工具类, 继承org.apache.commons.lang3.StringUtils类
|
||||
*/
|
||||
public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
||||
private static final Logger log = LoggerFactory.getLogger(StringUtils.class);
|
||||
private static boolean ipLocal = false;
|
||||
private static DbSearcher searcher = null;
|
||||
|
||||
static {
|
||||
SpringContextHolder.addCallBacks(() -> {
|
||||
StringUtils.ipLocal = SpringContextHolder.getProperties("ip.local-parsing", false, Boolean.class);
|
||||
if (ipLocal) {
|
||||
/**
|
||||
* 此文件为独享 ,不必关闭
|
||||
*/
|
||||
String path = "ip2region/ip2region.db";
|
||||
String name = "ip2region.db";
|
||||
DbConfig config = null;
|
||||
try {
|
||||
config = new DbConfig();
|
||||
File file = FileUtil.inputStreamToFile(new ClassPathResource(path).getInputStream(), name);
|
||||
searcher = new DbSearcher(config, file.getPath());
|
||||
} catch (DbMakerConfigException | FileNotFoundException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
} catch (NoSuchMethodException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static final char SEPARATOR = '_';
|
||||
|
||||
|
@ -145,7 +184,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
|||
try {
|
||||
ip = InetAddress.getLocalHost().getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
return ip;
|
||||
|
@ -155,11 +194,41 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
|||
* 根据ip获取详细地址
|
||||
*/
|
||||
public static String getCityInfo(String ip) {
|
||||
if (ipLocal) {
|
||||
return getLocalCityInfo(ip);
|
||||
} else {
|
||||
return getHttpCityInfo(ip);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ip获取详细地址
|
||||
*/
|
||||
public static String getHttpCityInfo(String ip) {
|
||||
String api = String.format(ElAdminConstant.Url.IP_URL, ip);
|
||||
JSONObject object = JSONUtil.parseObj(HttpUtil.get(api));
|
||||
return object.get("addr", String.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ip获取详细地址
|
||||
*/
|
||||
public static String getLocalCityInfo(String ip) {
|
||||
try {
|
||||
DataBlock dataBlock;
|
||||
dataBlock = searcher.binarySearch(ip);
|
||||
String address = dataBlock.getRegion().replace("0|", "");
|
||||
char symbol = '|';
|
||||
if (address.charAt(address.length() - 1) == symbol) {
|
||||
address = address.substring(0, address.length() - 1);
|
||||
}
|
||||
return address.equals(ElAdminConstant.REGION) ? "内网IP" : address;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String getBrowser(HttpServletRequest request) {
|
||||
UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
|
||||
Browser browser = userAgent.getBrowser();
|
||||
|
@ -183,6 +252,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
|||
|
||||
/**
|
||||
* 获取当前机器的IP
|
||||
*
|
||||
* @return /
|
||||
*/
|
||||
public static String getLocalIp() {
|
||||
|
|
|
@ -73,6 +73,11 @@ generator:
|
|||
#是否开启 swagger-ui
|
||||
swagger:
|
||||
enabled: true
|
||||
# IP 本地解析
|
||||
|
||||
ip:
|
||||
local-parsing: true
|
||||
|
||||
|
||||
# 文件存储路径
|
||||
file:
|
||||
|
|
Binary file not shown.
6
pom.xml
6
pom.xml
|
@ -139,7 +139,11 @@
|
|||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>${druid.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.lionsoul</groupId>
|
||||
<artifactId>ip2region</artifactId>
|
||||
<version>1.7.2</version>
|
||||
</dependency>
|
||||
<!--lombok插件-->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
|
Loading…
Reference in New Issue