package com.ruoyi.auth.service;
|
|
import com.ruoyi.system.api.domain.SysLoginLog;
|
import com.ruoyi.system.api.feignClient.SysLoginLogClient;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletRequest;
|
import java.time.LocalDateTime;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 记录日志方法
|
*
|
* @author ruoyi
|
*/
|
@Component
|
public class SysRecordLogService {
|
@Resource
|
private SysLoginLogClient sysLoginLogClient;
|
|
/**
|
* 记录登录信息
|
*
|
* @param username 用户名
|
* @param status 状态
|
* @param message 消息内容
|
* @return
|
*/
|
public void recordLogininfor(HttpServletRequest request, Integer userId, String username, String status, String message) {
|
Map<String, String> headerData = getHeaderData(request);
|
//添加登录日志
|
SysLoginLog loginLog = new SysLoginLog();
|
loginLog.setUserId(userId);
|
loginLog.setUsername(username);
|
loginLog.setIpAddress(headerData.get("ip"));
|
loginLog.setAddress("");
|
loginLog.setBrowserType(headerData.get("browser"));
|
loginLog.setOperatingSystem(headerData.get("os"));
|
loginLog.setLoginTime(LocalDateTime.now());
|
loginLog.setLoginStatus(Integer.valueOf(status));
|
loginLog.setMessage(message);
|
sysLoginLogClient.saveLoginLog(loginLog);
|
}
|
|
|
public Map<String, String> getHeaderData(HttpServletRequest request) {
|
//Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
|
String browserDetails = request.getHeader("User-Agent");
|
String userAgent = browserDetails;
|
String user = userAgent.toLowerCase();
|
Map<String, String> map = new HashMap<>();
|
String os = "";
|
String browser = "";
|
if (userAgent.toLowerCase().indexOf("windows") >= 0) {
|
os = "Windows";
|
} else if (userAgent.toLowerCase().indexOf("mac") >= 0) {
|
os = "Mac";
|
} else if (userAgent.toLowerCase().indexOf("x11") >= 0) {
|
os = "Unix";
|
} else if (userAgent.toLowerCase().indexOf("android") >= 0) {
|
os = "Android";
|
} else if (userAgent.toLowerCase().indexOf("iphone") >= 0) {
|
os = "IPhone";
|
} else {
|
os = "UnKnown, More-Info: " + userAgent;
|
}
|
if (user.contains("edge")) {
|
browser = (userAgent.substring(userAgent.indexOf("Edge")).split(" ")[0]).replace("/", "-");
|
} else if (user.contains("msie")) {
|
String substring = userAgent.substring(userAgent.indexOf("MSIE")).split(";")[0];
|
browser = substring.split(" ")[0].replace("MSIE", "IE") + "-" + substring.split(" ")[1];
|
} else if (user.contains("safari") && user.contains("version")) {
|
browser = (userAgent.substring(userAgent.indexOf("Safari")).split(" ")[0]).split("/")[0]
|
+ "-" + (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];
|
} else if (user.contains("opr") || user.contains("opera")) {
|
if (user.contains("opera")) {
|
browser = (userAgent.substring(userAgent.indexOf("Opera")).split(" ")[0]).split("/")[0]
|
+ "-" + (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];
|
} else if (user.contains("opr")) {
|
browser = ((userAgent.substring(userAgent.indexOf("OPR")).split(" ")[0]).replace("/", "-"))
|
.replace("OPR", "Opera");
|
}
|
|
} else if (user.contains("chrome")) {
|
browser = (userAgent.substring(userAgent.indexOf("Chrome")).split(" ")[0]).replace("/", "-");
|
} else if ((user.indexOf("mozilla/7.0") > -1) || (user.indexOf("netscape6") != -1) ||
|
(user.indexOf("mozilla/4.7") != -1) || (user.indexOf("mozilla/4.78") != -1) ||
|
(user.indexOf("mozilla/4.08") != -1) || (user.indexOf("mozilla/3") != -1)) {
|
browser = "Netscape-?";
|
|
} else if (user.contains("firefox")) {
|
browser = (userAgent.substring(userAgent.indexOf("Firefox")).split(" ")[0]).replace("/", "-");
|
} else if (user.contains("rv")) {
|
String IEVersion = (userAgent.substring(userAgent.indexOf("rv")).split(" ")[0]).replace("rv:", "-");
|
browser = "IE" + IEVersion.substring(0, IEVersion.length() - 1);
|
} else {
|
browser = "UnKnown, More-Info: " + userAgent;
|
}
|
|
String ip = request.getHeader("x-forwarded-for");
|
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
|
// 多次反向代理后会有多个ip值,第一个ip才是真实ip
|
if (ip.indexOf(",") != -1) {
|
ip = ip.split(",")[0];
|
}
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("Proxy-Client-IP");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("WL-Proxy-Client-IP");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_CLIENT_IP");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("X-Real-IP");
|
}
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getRemoteAddr();
|
}
|
map.put("os", os);
|
map.put("browser", browser);
|
map.put("ip", ip);
|
return map;
|
}
|
|
|
}
|