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