eu.bitwalker.UserAgentUtils 替换为 com.blueconic.browscap-java

添加UserAgentUtils工具类简化操作
This commit is contained in:
guxin0123 2022-01-12 12:08:27 +08:00
parent 869dcf73f8
commit f6fa8c4c3d
5 changed files with 57 additions and 14 deletions

View File

@ -19,7 +19,7 @@
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<druid.version>1.2.8</druid.version>
<bitwalker.version>1.21</bitwalker.version>
<browscap.version>1.3.11</browscap.version>
<swagger.version>3.0.0</swagger.version>
<kaptcha.version>2.3.2</kaptcha.version>
<mybatis-spring-boot.version>2.2.0</mybatis-spring-boot.version>
@ -58,9 +58,9 @@
<!-- 解析客户端操作系统、浏览器等 -->
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>${bitwalker.version}</version>
<groupId>com.blueconic</groupId>
<artifactId>browscap-java</artifactId>
<version>${browscap.version}</version>
</dependency>
<!-- SpringBoot集成mybatis框架 -->

View File

@ -115,8 +115,8 @@
<!-- 解析客户端操作系统、浏览器等 -->
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<groupId>com.blueconic</groupId>
<artifactId>browscap-java</artifactId>
</dependency>
<!-- servlet包 -->

View File

@ -0,0 +1,42 @@
package com.ruoyi.common.utils;
import com.blueconic.browscap.Capabilities;
import com.blueconic.browscap.ParseException;
import com.blueconic.browscap.UserAgentParser;
import com.blueconic.browscap.UserAgentService;
import java.io.IOException;
/**
* 浏览器UserAgent工具类
*
* @author Gux
*/
public class UserAgentUtils {
UserAgentParser parser;
String browser = "";
String platform = "";
public static UserAgentUtils parseUserAgentString(String userAgentString) {
UserAgentUtils userAgentUtils = new UserAgentUtils();
try {
userAgentUtils.parser = new UserAgentService().loadParser();
final Capabilities capabilities = userAgentUtils.parser.parse(userAgentString);
userAgentUtils.browser = capabilities.getBrowser() + " " + capabilities.getBrowserMajorVersion();
userAgentUtils.platform = capabilities.getPlatform();
} catch (IOException | ParseException e) {
e.printStackTrace();
}
return userAgentUtils;
}
public String getBrowser() {
return browser;
}
public String getOperatingSystem() {
return platform;
}
}

View File

@ -1,6 +1,8 @@
package com.ruoyi.framework.manager.factory;
import java.util.TimerTask;
import com.ruoyi.common.utils.UserAgentUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.constant.Constants;
@ -14,7 +16,6 @@ import com.ruoyi.system.domain.SysLogininfor;
import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.service.ISysLogininforService;
import com.ruoyi.system.service.ISysOperLogService;
import eu.bitwalker.useragentutils.UserAgent;
/**
* 异步工厂产生任务用
@ -37,7 +38,7 @@ public class AsyncFactory
public static TimerTask recordLogininfor(final String username, final String status, final String message,
final Object... args)
{
final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
final UserAgentUtils userAgent = UserAgentUtils.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
final String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
return new TimerTask()
{
@ -54,9 +55,9 @@ public class AsyncFactory
// 打印信息到日志
sys_user_logger.info(s.toString(), args);
// 获取客户端操作系统
String os = userAgent.getOperatingSystem().getName();
String os = userAgent.getOperatingSystem();
// 获取客户端浏览器
String browser = userAgent.getBrowser().getName();
String browser = userAgent.getBrowser();
// 封装对象
SysLogininfor logininfor = new SysLogininfor();
logininfor.setUserName(username);

View File

@ -4,6 +4,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import com.ruoyi.common.utils.UserAgentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@ -15,7 +16,6 @@ import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.AddressUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import eu.bitwalker.useragentutils.UserAgent;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
@ -154,12 +154,12 @@ public class TokenService
*/
public void setUserAgent(LoginUser loginUser)
{
UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
UserAgentUtils userAgent = UserAgentUtils.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
loginUser.setIpaddr(ip);
loginUser.setLoginLocation(AddressUtils.getRealAddressByIP(ip));
loginUser.setBrowser(userAgent.getBrowser().getName());
loginUser.setOs(userAgent.getOperatingSystem().getName());
loginUser.setBrowser(userAgent.getBrowser());
loginUser.setOs(userAgent.getOperatingSystem());
}
/**