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.net.NetUtil; |
|
28 |
import cn.hutool.core.util.ObjectUtil; |
|
29 |
import cn.hutool.extra.servlet.ServletUtil; |
|
30 |
import cn.hutool.http.HttpRequest; |
|
31 |
import cn.hutool.http.HttpUtil; |
|
32 |
import cn.hutool.log.Log; |
|
33 |
import com.alibaba.fastjson.JSONPath; |
|
34 |
import vip.xiaonuo.core.consts.CommonConstant; |
|
35 |
import vip.xiaonuo.core.consts.SymbolConstant; |
|
36 |
import vip.xiaonuo.core.context.constant.ConstantContextHolder; |
|
37 |
|
|
38 |
import javax.servlet.http.HttpServletRequest; |
|
39 |
import java.util.List; |
|
40 |
|
|
41 |
/** |
|
42 |
* 根据ip地址定位工具类,使用阿里云定位api,如使用本接口,仅需使用以下地址购买接口,然后替换sys_config表中定位appCode为你自己的即可 |
|
43 |
* 接口购买地址:https://market.aliyun.com/products/57002003/cmapi021970.html |
|
44 |
* |
|
45 |
* @author xuyuxiang |
|
46 |
* @date 2020/3/16 11:25 |
|
47 |
*/ |
|
48 |
public class IpAddressUtil { |
|
49 |
|
|
50 |
private static final Log log = Log.get(); |
|
51 |
|
|
52 |
private static final String LOCAL_IP = "127.0.0.1"; |
|
53 |
|
|
54 |
private static final String LOCAL_REMOTE_HOST = "0:0:0:0:0:0:0:1"; |
|
55 |
|
|
56 |
/** |
|
57 |
* 获取客户端ip |
|
58 |
* |
|
59 |
* @author xuyuxiang |
|
60 |
* @date 2020/3/19 9:32 |
|
61 |
*/ |
|
62 |
public static String getIp(HttpServletRequest request) { |
|
63 |
if (ObjectUtil.isEmpty(request)) { |
|
64 |
return LOCAL_IP; |
|
65 |
} else { |
|
66 |
String remoteHost = ServletUtil.getClientIP(request); |
|
67 |
return LOCAL_REMOTE_HOST.equals(remoteHost) ? LOCAL_IP : remoteHost; |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* 根据ip地址定位 |
|
73 |
* |
|
74 |
* @author xuyuxiang |
|
75 |
* @date 2020/3/16 15:17 |
|
76 |
*/ |
|
77 |
@SuppressWarnings("unchecked") |
|
78 |
public static String getAddress(HttpServletRequest request) { |
|
79 |
String resultJson = SymbolConstant.DASH; |
|
80 |
|
|
81 |
String ip = getIp(request); |
|
82 |
|
|
83 |
//如果是本地ip或局域网ip,则直接不查询 |
|
84 |
if (ObjectUtil.isEmpty(ip) || NetUtil.isInnerIP(ip)) { |
|
85 |
return resultJson; |
|
86 |
} |
|
87 |
|
|
88 |
try { |
|
89 |
//获取阿里云定位api接口 |
|
90 |
String api = ConstantContextHolder.getIpGeoApi(); |
|
91 |
//获取阿里云定位appCode |
|
92 |
String appCode = ConstantContextHolder.getIpGeoAppCode(); |
|
93 |
if (ObjectUtil.isAllNotEmpty(api, appCode)) { |
|
94 |
String path = "$['data']['country','region','city','isp']"; |
|
95 |
String appCodeSymbol = "APPCODE"; |
|
96 |
HttpRequest http = HttpUtil.createGet(String.format(api, ip)); |
|
97 |
http.header(CommonConstant.AUTHORIZATION, appCodeSymbol + " " + appCode); |
|
98 |
resultJson = http.timeout(3000).execute().body(); |
|
99 |
resultJson = String.join("", (List<String>) JSONPath.read(resultJson, path)); |
|
100 |
} |
|
101 |
} catch (Exception e) { |
|
102 |
resultJson = SymbolConstant.DASH; |
|
103 |
//注释掉此log,以免频繁打印,可自行开启 |
|
104 |
//log.error(">>> 根据ip定位异常,请求号为:{},具体信息为:{}", RequestNoContext.get(), e.getMessage()); |
|
105 |
} |
|
106 |
return resultJson; |
|
107 |
} |
|
108 |
|
|
109 |
} |