mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
fix(web): 添加Servlet工具类对getOs和getBrowser方法中User-Agent为空或解析失败时的非空判断
This commit is contained in:
@@ -24,6 +24,7 @@ import cn.hutool.http.useragent.UserAgentUtil;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
@@ -68,8 +69,22 @@ public class ServletUtils extends JakartaServletUtil {
|
||||
* @return 浏览器及其版本信息
|
||||
*/
|
||||
public static String getBrowser(String userAgentString) {
|
||||
UserAgent userAgent = UserAgentUtil.parse(userAgentString);
|
||||
return userAgent.getBrowser().getName() + StringConstants.SPACE + userAgent.getVersion();
|
||||
if (StringUtils.isBlank(userAgentString)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
UserAgent userAgent = UserAgentUtil.parse(userAgentString);
|
||||
if (userAgent == null || userAgent.getBrowser() == null) {
|
||||
return null;
|
||||
}
|
||||
String browserName = userAgent.getBrowser().getName();
|
||||
String version = userAgent.getVersion();
|
||||
return StringUtils.isBlank(version)
|
||||
? browserName
|
||||
: browserName + StringConstants.SPACE + version;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,8 +107,18 @@ public class ServletUtils extends JakartaServletUtil {
|
||||
* @return 操作系统
|
||||
*/
|
||||
public static String getOs(String userAgentString) {
|
||||
UserAgent userAgent = UserAgentUtil.parse(userAgentString);
|
||||
return userAgent.getOs().getName();
|
||||
if (StringUtils.isEmpty(userAgentString)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
UserAgent userAgent = UserAgentUtil.parse(userAgentString);
|
||||
if (userAgent == null || userAgent.getOs() == null) {
|
||||
return null;
|
||||
}
|
||||
return userAgent.getOs().getName();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user