原有的啟動平臺公共元件庫comm-util的瀏覽器工具類BrowserUtils是基於UserAgentUtils的,但是該專案最後一個版本釋出於 2018/01/24,之至今日23年底,已有5年沒有維護更新,會造成最新版本的部分瀏覽器不能正確獲取到瀏覽器資訊。(至於為什麼停更了獲取不到最新瀏覽器和作業系統資訊了呢,文末給大家揭曉。)以至於公司的專案組團隊不得已去尋找其他解決方案,做該工具類的優化更新。於是我們來到的專案官網,看到了官方公告也提示專案已停止更新,官方建議使用Browscap做代替。如下圖:
替換前
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>1.21</version>
</dependency>
替換後
<dependency>
<groupId>com.blueconic</groupId>
<artifactId>browscap-java</artifactId>
<version>1.4.1</version>
</dependency>
說明:browscap-java的1.4.1版本實在2023年7月維護的
原有實現是基於UserAgentUtils的,首先從請求頭中獲取到User-Agent,再通過parseUserAgentString()方法獲取到UserAgent物件,然後在呼叫物件的getBrowser()方法獲取到瀏覽器物件Browser,進而獲取到瀏覽器相關資訊。
private static Browser getBrowser(HttpServletRequest request) {
return UserAgent.parseUserAgentString(request.getHeader("User-Agent")).getBrowser();
}
現有實現是基於的,首先獲取到UserAgentParser物件,基於物件的parse()方法去解析請求頭,進而獲取Capabilities物件,基於Capabilities物件的getValue()方法獲取使用者端瀏覽器和使用者端作業系統等資訊。
private static Capabilities getCapabilities(HttpServletRequest request) {
try {
UserAgentParser userAgentParser = new UserAgentService().loadParser();
return userAgentParser.parse(request.getHeader("User-Agent"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
改造前
public static String getOsName(HttpServletRequest request) {
UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
return userAgent.getOperatingSystem().getName();
}
改造後
public static String getOsName(HttpServletRequest request) {
Capabilities capabilities = getCapabilities(request);
return capabilities.getPlatform();
}
public static String getOsVersion(HttpServletRequest request) {
Capabilities capabilities = getCapabilities(request);
return capabilities.getPlatformVersion();
}
改造前,並不支援
改造前
public static String getBrowserName(HttpServletRequest request) {
Browser browser = getBrowser(request);
return browser.getName();
}
改造後
public static String getBrowserName(HttpServletRequest request) {
Capabilities capabilities = getCapabilities(request);
return capabilities.getBrowser();
}
改造前
public static String getBrowserVersion(HttpServletRequest request) {
Browser browser = getBrowser(request);
Version version = browser.getVersion(request.getHeader("User-Agent"));
return version.getVersion();
}
改造後
public static String getBrowserVersion(HttpServletRequest request) {
Capabilities capabilities = getCapabilities(request);
return capabilities.getBrowserMajorVersion();
}
public static void main(String[] args) {
final String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36";
try {
UserAgentParser userAgentParser = new UserAgentService().loadParser();
Capabilities capabilities = userAgentParser.parse(userAgent);
System.out.println("系統名稱==>" + capabilities.getPlatform() + "/" + capabilities.getPlatformVersion());
System.out.println("瀏覽器資訊==>" + capabilities.getBrowser() + "/" + capabilities.getBrowserMajorVersion());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
為什麼原有的工具類不能獲取最新的瀏覽器和作業系統的資訊呢?跟下原始碼你就明白了
Windows
Android
Symbian
Edge
Chrome
Gitee:https://gitee.com/startdis/startdis-comm
Github:https://github.com/startdis/startdis-comm
Gitlab:https://gitlab.com/startdis/startdis-comm