开发喵星球

若依集成browscap读取浏览器用户代理(八十二)

1、修改pom.xml,将bitwalker替换成browscap-java

<browscap.version>1.3.12</browscap.version>

<!-- 解析客户端操作系统、浏览器等 -->
<dependency>
    <groupId>com.blueconic</groupId>
    <artifactId>browscap-java</artifactId>
    <version>${browscap.version}</version>
</dependency>

2、ruoyi-framework/pom.xml删除bitwalker

3、ruoyi-common/pom.xml新增browscap-java

<dependency>
    <groupId>com.blueconic</groupId>
    <artifactId>browscap-java</artifactId>
</dependency>

4、新增用户代理解析类UserAgent.java

package com.ruoyi.common.utils.http;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.blueconic.browscap.Capabilities;
import com.blueconic.browscap.UserAgentParser;
import com.blueconic.browscap.UserAgentService;
import com.ruoyi.common.utils.AddressUtils;

/**
 * 浏览器用户代理解析
 * 
 */
public class UserAgent
{
    private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);

    /** 浏览器 */
    public String browser = "";

    /** 操作系统 */
    public String operatingSystem = "";

    /** 解析器 */
    private static UserAgentParser parser = null;

    static
    {
        try
        {
            parser = new UserAgentService().loadParser();
        }
        catch (Exception e)
        {
            log.error("获取用户代理异常 {}", e);
        }
    }

    public UserAgent(String userAgentString)
    {
        if (parser != null)
        {
            String userAgentLowercaseString = userAgentString == null ? null : userAgentString.toLowerCase();
            Capabilities capabilities = parser.parse(userAgentLowercaseString);
            this.browser = String.format("%s %s", capabilities.getBrowser(), capabilities.getBrowserMajorVersion());
            this.operatingSystem = capabilities.getPlatform();
        }
    }

    public static UserAgent parseUserAgentString(String userAgentString)
    {
        return new UserAgent(userAgentString);
    }

    public String getBrowser()
    {
        return browser;
    }

    public String getOperatingSystem()
    {
        return operatingSystem;
    }
}

5、AsyncFactory、OnlineSessionFactory修改UserAgent包路径并修改获取方法

// bitwalker 获取浏览器/操作系统方法
String os = userAgent.getOperatingSystem().getName();
String browser = userAgent.getBrowser().getName();

// ======== 修改为 ========    

// browscap-java 获取浏览器/操作系统方法       
String os = userAgent.getOperatingSystem();
String browser = userAgent.getBrowser();
   
分类:Java/OOP 作者:无限繁荣, 吴蓉 发表于:2023-12-27 10:17:14 阅读量:124
<<   >>


powered by kaifamiao