user-agent-utils 是一个用来解析 User-Agent 字符串的 Java 类库。 其能够识别的内容包括: 超过150种不同的浏览器; 7种不同的浏览器类型; 超过60种不同的操作系统; 6种不同的设备类型; 9种不同的渲染引擎; 9种不同的Web应用,如HttpClient、Bot。

在web应用中我们通过request获取用户的Agent:

String agent=request.getHeader("User-Agent");

如下,我们获取了一个agent的字符串:

"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"

由此,通过User-agent-utils解析:

String agent=request.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());

AOP && UserAgent

使用日志AOP获取请求方法,参数,浏览器信息等

@Aspect
@Component
@Slf4j
public class AopLog {
   private static final String START_TIME = "request-start";

   @Pointcut("execution(* com.hjy.log.aop.controller.*.*(..) )")
   public void log(){}

   @Before("log()")
   public void beforeLog(JoinPoint point){
       ServletRequestAttributes attributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
       HttpServletRequest request= Objects.requireNonNull(attributes).getRequest();

       log.info("【请求 URL】: {}",request.getRequestURL());
       log.info("【请求 IP】: {}",request.getRemoteAddr());
       log.info("【请求类名】: {},【请求方法名】: {}",
               point.getSignature().getDeclaringTypeName(),
               point.getSignature().getName());

       Map<String,String[]> map= request.getParameterMap();
       log.info("【请求参数】: {}", JSONUtil.toJsonStr(map));
       Long start=System.currentTimeMillis();
       request.setAttribute(START_TIME,start);

  }

   @Around("log()")
   public Object aroundLog(ProceedingJoinPoint point) throws Throwable {
       Object result=point.proceed();
       log.info("【返回值】:{}",JSONUtil.toJsonStr(result));
       return result;
  }

   @After("log()")
   public void afterLog(){
       ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
       HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
       Long start= (Long) request.getAttribute(START_TIME);
       Long end=System.currentTimeMillis();
       log.info("【请求耗时】:{}ms",end-start);

       String header=request.getHeader("User-Agent");
       UserAgent userAgent=UserAgent.parseUserAgentString(header);
       log.info("【浏览器类型】:{},【操作系统】:{},【原始User-Agent】:{}",
               userAgent.getBrowser().toString(),
               userAgent.getOperatingSystem().toString(),
               header);
  }
}

参考 https://www.cnblogs.com/yjh1995/p/14164468.html

最新文章

  1. jq绑定事件的4种方式
  2. VMware桥接模式无法自动化获取IP的解决方法
  3. 注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式
  4. Hive安装与部署集成mysql
  5. 【HTML】心愿墙 Demo展示
  6. [欧拉] poj 2230 Watchcow
  7. 优秀Java程序员必须了解的GC工作原理(转)
  8. 用css2属性clip实现网页进度条
  9. 最近对Memcache的一些学习
  10. js原生设计模式——8单例模式
  11. JAVA入门[8]-测试mybatis
  12. Grafana最新版本4.3.1安装(后端使用mysql)
  13. 使用 C# (.NET Core) 实现模板方法模式 (Template Method Pattern)
  14. Jenkins|简单Job配置|启动脚本|测试报告
  15. Java课程寒假之回答问题:如何将你的兴趣化为可以立足于社会的资本
  16. Mac OS X 绑定80端口,不装nginx的小技巧
  17. 论文笔记:A Structured Self-Attentive Sentence Embedding
  18. 【Selenium】【BugList3】firefox与Selenium版本不兼容,报: Message: Unsupported Marionette protocol version 2, required 3
  19. 前端流程图jsplumb学习笔记
  20. Android开发之利用ViewPager实现页面的切换(仿微信、QQ)

热门文章

  1. cs231n__4.1 Backpropagation and Neural Network
  2. DP经典例题——LIS&amp;LCS
  3. 万万没想到,go的数据库操作,也能像php一样溜了
  4. 将xlsx列表文件转为md列表
  5. 深入理解C++虚函数底层机制和RTTI运行时类型识别
  6. 小H的小屋
  7. Ubuntu 22.04 安装 VMware Tools
  8. 手把手教你用LOTO虚拟示波器搭建测试系统整机
  9. 浅谈Pytest中的warning处理
  10. 浅析 SeaweedFS 与 JuiceFS 架构异同