commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.core.util; |
|
26 |
|
|
27 |
import cn.hutool.core.util.ObjectUtil; |
|
28 |
import cn.hutool.extra.servlet.ServletUtil; |
|
29 |
import cn.hutool.http.useragent.Browser; |
|
30 |
import cn.hutool.http.useragent.UserAgent; |
|
31 |
import cn.hutool.http.useragent.UserAgentUtil; |
|
32 |
import vip.xiaonuo.core.consts.CommonConstant; |
|
33 |
import vip.xiaonuo.core.consts.SymbolConstant; |
|
34 |
|
|
35 |
import javax.servlet.http.HttpServletRequest; |
|
36 |
|
|
37 |
/** |
|
38 |
* 用户代理工具类 |
|
39 |
* |
|
40 |
* @author xuyuxiang |
|
41 |
* @date 2020/3/19 14:52 |
|
42 |
*/ |
|
43 |
public class UaUtil { |
|
44 |
|
|
45 |
/** |
|
46 |
* 获取客户端浏览器 |
|
47 |
* |
|
48 |
* @author xuyuxiang |
|
49 |
* @date 2020/3/19 14:53 |
|
50 |
*/ |
|
51 |
public static String getBrowser(HttpServletRequest request) { |
|
52 |
UserAgent userAgent = getUserAgent(request); |
|
53 |
if (ObjectUtil.isEmpty(userAgent)) { |
|
54 |
return SymbolConstant.DASH; |
|
55 |
} else { |
|
56 |
String browser = userAgent.getBrowser().toString(); |
|
57 |
return CommonConstant.UNKNOWN.equals(browser) ? SymbolConstant.DASH : browser; |
|
58 |
} |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* 获取客户端操作系统 |
|
63 |
* |
|
64 |
* @author xuyuxiang |
|
65 |
* @date 2020/3/19 14:53 |
|
66 |
*/ |
|
67 |
public static String getOs(HttpServletRequest request) { |
|
68 |
UserAgent userAgent = getUserAgent(request); |
|
69 |
if (ObjectUtil.isEmpty(userAgent)) { |
|
70 |
return SymbolConstant.DASH; |
|
71 |
} else { |
|
72 |
String os = userAgent.getOs().toString(); |
|
73 |
return CommonConstant.UNKNOWN.equals(os) ? SymbolConstant.DASH : os; |
|
74 |
} |
|
75 |
} |
|
76 |
|
|
77 |
/** |
|
78 |
* 获取请求代理头 |
|
79 |
* |
|
80 |
* @author xuyuxiang |
|
81 |
* @date 2020/3/19 14:54 |
|
82 |
*/ |
|
83 |
private static UserAgent getUserAgent(HttpServletRequest request) { |
|
84 |
String userAgentStr = ServletUtil.getHeaderIgnoreCase(request, CommonConstant.USER_AGENT); |
|
85 |
UserAgent userAgent = UserAgentUtil.parse(userAgentStr); |
|
86 |
//判空 |
|
87 |
if (ObjectUtil.isNotEmpty(userAgentStr)) { |
|
88 |
//如果根本没获取到浏览器 |
|
89 |
if (CommonConstant.UNKNOWN.equals(userAgent.getBrowser().getName())) { |
|
90 |
//则将ua设置为浏览器 |
|
91 |
userAgent.setBrowser(new Browser(userAgentStr, null, "")); |
|
92 |
} |
|
93 |
} |
|
94 |
return userAgent; |
|
95 |
} |
|
96 |
} |