介绍
UserAgentUtils 是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备等相关信息,这些信息通常包含在接口请求的 User-Agent 字符串中。
这个库可以用于解析用户代理头,以提取有关所使用的浏览器、浏览器版本、平台、平台版本和设备类型的信息。对于确定客户端是否是台式机、平板电脑或移动设备,或者客户端是否在Windows或Mac OS上(仅举几例)非常有用。
- 超过150种不同的浏览器;
- 7种不同的浏览器类型;
- 超过60种不同的操作系统;
- 6种不同的设备类型;
- 9种不同的渲染引擎;
- 9种不同的Web应用,如HttpClient、Bot。
官方文档:https://www.bitwalker.eu/software/user-agent-utils
Github:https://github.com/HaraldWalker/user-agent-utils/tree/release-1.21
特别注意该项目已停止了维护,但是功能还是挺好用的
效果图
依赖
<!--解析浏览器字符串-->
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>1.21</version>
</dependency>
<!--字符串工具-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<!--提供了大量的工具类,更方便地进行字符串、日期、集合、反射等操作。-->
封装客户端工具
public class ClientUtils {
/**
* 获取当前线程的请求
*/
public static ServletRequestAttributes getRequestAttributes()
{
//获取当前线程的请求
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
//提供了更具体的 Servlet 请求属性
return (ServletRequestAttributes) attributes;
}
/**
* 获取request信息
*/
public static HttpServletRequest getRequest()
{
return getRequestAttributes().getRequest();
}
}
封装IP工具
public class IpUtils {
/**
* 获取客户端IP
*
* @return IP地址
*/
public static String getIpAddr()
{
return getIpAddr(ClientUtils.getRequest());
}
/**
* 获取客户端IP
*
* @param request 请求对象
* @return IP地址
*/
public static String getIpAddr(HttpServletRequest request)
{
if (request == null)
{
return "unknown";
}
String ip = request.getHeader("x-forwarded-for");
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("X-Forwarded-For");
}
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("X-Real-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
{
ip = request.getRemoteAddr();
}
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : getMultistageReverseProxyIp(ip);
}
/**
* 从多级反向代理中获得第一个非unknown IP地址
*
* @param ip 获得的IP地址
* @return 第一个非unknown IP地址
*/
public static String getMultistageReverseProxyIp(String ip)
{
// 多级反向代理检测
if (ip != null && ip.indexOf(",") > 0)
{
final String[] ips = ip.trim().split(",");
for (String subIp : ips)
{
if (false == isUnknown(subIp))
{
ip = subIp;
break;
}
}
}
return StringUtils.substring(ip, 0, 255);
}
/**
* 检测给定字符串是否为未知,多用于检测HTTP请求相关
*
* @param checkString 被检测的字符串
* @return 是否未知
*/
public static boolean isUnknown(String checkString)
{
return StringUtils.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);
}
}
实体类
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("login_log")
public class LoginLog implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 编号
*/
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
/**
* 用户名称
*/
@TableField("user_name")
private String userName;
/**
* IP
*/
@TableField("ip")
private String ip;
/**
* 浏览器
*/
@TableField("browser")
private String browser;
/**
* 操作系统
*/
@TableField("platform")
private String platform;
/**
* 登录时间
*/
@TableField("login_time")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private String loginTime;
}
获取设备信息入库
public void recordLogin(LoginLog loginLog){
//获取当前线程中的请求
String s = ClientUtils.getRequest().getHeader("User-Agent");
//反向代理中获取IP
loginLog.setIp(IpUtils.getIpAddr());
//异步执行
CompletableFuture.runAsync(() -> {
// 获取当前线程请求头的 "User-Agent"
UserAgent userAgent = UserAgent.parseUserAgentString(s);
// 获取使用的浏览器
loginLog.setBrowser(userAgent.getBrowser().getName());
// 获取使用的操作系统
loginLog.setPlatform(userAgent.getOperatingSystem().getName());
//数据入库
mapper.insert(loginLog);
});
}
效果图