0
点赞
收藏
分享

微信扫一扫

在spring中使用UserAgentUtil获取访问者的信息

导入依赖

<dependency>
            <groupId>eu.bitwalker</groupId>
            <artifactId>UserAgentUtils</artifactId>
            <version>1.21</version>
        </dependency>



对象获取

使用 HttpServletRequest 获取User-Agent(用户代理),然后再获取UserAgent(解析agent字符串),Browser(获取浏览器对象),OperatingSystem(获取操作系统对象)。



代码

String agent=httpServletRequest.getHeader("User-Agent");
//解析agent字符串
        UserAgent userAgent = UserAgent.parseUserAgentString(agent);
//获取浏览器对象
        Browser browser = userAgent.getBrowser();
//获取操作系统对象
        OperatingSystem operatingSystem = userAgent.getOperatingSystem();
 
        System.out.println("浏览器名:"+browser.getName());
        System.out.println("浏览器类型:"+browser.getBrowserType());
        System.out.println("浏览器家族:"+browser.getGroup());
        System.out.println("浏览器生产厂商:"+browser.getManufacturer());
        System.out.println("浏览器使用的渲染引擎:"+browser.getRenderingEngine());
        System.out.println("浏览器版本:"+userAgent.getBrowserVersion());
 
        System.out.println("操作系统名:"+operatingSystem.getName());
        System.out.println("访问设备类型:"+operatingSystem.getDeviceType());
        System.out.println("操作系统家族:"+operatingSystem.getGroup());
        System.out.println("操作系统生产厂商:"+operatingSystem.getManufacturer());
        System.out.println("IP地址:"+httpServletRequest.getRemoteAddr());

 

使用:返回的UserAgent对象就是解析后的

UserAgent userAgent = UserAgent.parseUserAgentString(str);

UserAgentUtil的工具类官网:https://www.bitwalker.eu/software/user-agent-utils

 



第二种Hutool工具

<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.10</version>
</dependency>

 

UserAgent ua = UserAgentUtil.parse("5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Mobile Safari/537.36");

Console.log(ua.getOsVersion()); // 6.0

 



第三种

<dependency>
            <groupId>cz.mallat.uasparser</groupId>
            <artifactId>uasparser</artifactId>
            <version>0.6.0</version>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.jregex</groupId>
            <artifactId>jregex</artifactId>
            <version>1.2_01</version>
        </dependency>

import cz.mallat.uasparser.OnlineUpdater;
import cz.mallat.uasparser.UASparser;
import cz.mallat.uasparser.UserAgentInfo;
import eu.bitwalker.useragentutils.UserAgent;

import java.io.IOException;

public class UserAgentTest {

    static UASparser uasParser = null;

    static {
        try {
            uasParser = new UASparser(OnlineUpdater.getVendoredInputStream());
            // java.lang.UnsupportedClassVersionError:
            // cz/mallat/uasparser/UASparser : Unsupported major.minor version 51.0
            // 用jdk1.6测试时会报以上错,需要jdk1.7以上版本支持
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String str = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
        try {
            UserAgentInfo userAgentInfo = UserAgentTest.uasParser.parse(str);
            System.out.println("操作系统家族:" + userAgentInfo.getOsFamily());
            System.out.println("操作系统详细名称:" + userAgentInfo.getOsName());
            System.out.println("浏览器名称和版本:" + userAgentInfo.getUaName());
            System.out.println("类型:" + userAgentInfo.getType());
            System.out.println("浏览器名称:" + userAgentInfo.getUaFamily());
            System.out.println("浏览器版本:" + userAgentInfo.getBrowserVersionInfo());
            System.out.println("设备类型:" + userAgentInfo.getDeviceType());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

 

 

 

 

 

 

 

 

举报

相关推荐

0 条评论