依赖
<!-- https://mvnrepository.com/artifact/eu.bitwalker/UserAgentUtils -->
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>1.21</version>
</dependency>
测试
@Slf4j
public class UtilTest {
@Test
public void testUserAgentUtil() {
//browserName=Chrome,os=Android,manufacture=Google Inc.,deviceType=Mobile
//String userAgentStr = "Mozilla/5.0 (Linux; Android 10; LIO-AN00 Build/HUAWEILIO-AN00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045713 Mobile Safari/537.36 MMWEBID/3189 MicroMessenger/8.0.11.1980(0x28000B51) Process/tools WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64";
//browserName=Chrome,os=Mac OS X,manufacture=Apple Inc.,deviceType=Computer
//String userAgentStr = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36";
//browserName=Chrome,os=Android,manufacture=Google Inc.,deviceType=Mobile
String userAgentStr = "Mozilla/5.0 (Linux; Android 10; LIO-AN00 Build/HUAWEILIO-AN00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045713 Mobile Safari/537.36 MMWEBID/3189 MicroMessenger/8.0.11.1980(0x28000B51) Process/tools WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64";
UserAgent userAgent = UserAgent.parseUserAgentString(userAgentStr);
Browser browser = userAgent.getBrowser();
OperatingSystem operatingSystem = userAgent.getOperatingSystem();
//浏览器名称
String browserName = browser.getGroup().getName();
//操作系统名称
String os = operatingSystem.getGroup().getName();
//设备厂商
String manufacture = operatingSystem.getManufacturer().getName();
//设备类型
String deviceType = operatingSystem.getDeviceType().getName();
System.out.println("browserName=" + browserName + ",os=" + os + ",manufacture=" + manufacture + ",deviceType=" + deviceType);
}
}
封装
/**
*
*
* 浏览器名称
* @param userAgent
* @return Firefox、Chrome
*/
public static String getBrowserName(String userAgent) {
Browser browser = getBrowser(userAgent);
String browserGroup = browser.getGroup().getName();
return browserGroup;
}
/**
* 获取deviceType
* @param userAgent
*
* @return MOBILE、COMPUTER
*/
public static String getDeviceType(String userAgent) {
OperatingSystem operatingSystem = getOperatingSystem(userAgent);
String deviceType = operatingSystem.getDeviceType().toString();
return deviceType;
}
/**
* 获取os:Windows/ios/Android
* @param userAgent
* @return
*/
public static String getOS(String userAgent) {
OperatingSystem operatingSystem = getOperatingSystem(userAgent);
String os = operatingSystem.getGroup().getName();
return os;
}
/**
* 获取device的生产厂家
*
* @param userAgent
* @return GOOGLE、APPLE
*/
public static String getDeviceManufacturer(String userAgent) {
OperatingSystem operatingSystem = getOperatingSystem(userAgent);
String deviceManufacturer = operatingSystem.getManufacturer().toString();
return deviceManufacturer;
}
/**
* 操作系统版本
* @param userAgent
* @return Android 1.x、Intel Mac OS X 10.15
*/
public static String getOSVersion(String userAgent) {
String osVersion = "";
if(StringUtils.isBlank(userAgent)) {
return osVersion;
}
String[] strArr = userAgent.substring(userAgent.indexOf("(")+1,
userAgent.indexOf(")")).split(";");
if(null == strArr || strArr.length == 0) {
return osVersion;
}
osVersion = strArr[1];
return osVersion;
}
/**
* 获取浏览器对象
* @param request
* @return
*/
private static Browser getBrowser(String agent) {
UserAgent userAgent = UserAgent.parseUserAgentString(agent);
Browser browser = userAgent.getBrowser();
return browser;
}
/**
* 获取操作系统对象
* @param userAgent
* @return
*/
private static OperatingSystem getOperatingSystem(String userAgent) {
UserAgent agent = UserAgent.parseUserAgentString(userAgent);
OperatingSystem operatingSystem = agent.getOperatingSystem();
return operatingSystem;
}
/**
* 解析对象
* @param agent
* @return
*/
public static DeviceInfoDO getDeviceInfo(String agent){
UserAgent userAgent = UserAgent.parseUserAgentString(agent);
Browser browser = userAgent.getBrowser();
OperatingSystem operatingSystem = userAgent.getOperatingSystem();
String browserName = browser.getGroup().getName();
String os = operatingSystem.getGroup().getName();
String manufacture = operatingSystem.getManufacturer().toString();
String deviceType = operatingSystem.getDeviceType().toString();
DeviceInfoDO deviceInfoDO = DeviceInfoDO.builder().browserName(browserName)
.deviceManufacturer(manufacture)
.deviceType(deviceType)
.os(os)
.osVersion(getOSVersion(agent))
.build();
return deviceInfoDO;
}
对象
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class DeviceInfoDO {
/**
* 浏览器名称
*/
private String browserName;
/**
* 操作系统
*/
private String os;
/**
* 系统版本
*/
private String osVersion;
/**
* 设备类型
*/
private String deviceType;
/**
* 设备厂商
*/
private String deviceManufacturer;
/**
* 用户唯一标识
*/
private String udid;
}