浏览代码

ip定位更换为阿里云定位,定位参数作为常量可配置,sql更新

徐玉祥 4 年之前
父节点
当前提交
d480818eee

+ 4 - 0
_sql/guns-separation.sql

@ -74,6 +74,10 @@ INSERT INTO `sys_config` VALUES (1270380786649982741, 'Guns放开XSS过滤的接
INSERT INTO `sys_config` VALUES (1270380786649982742, '单用户登陆的开关', 'GUNS_ENABLE_SINGLE_LOGIN', 'false', 'Y', '单用户登陆的开关,true-打开,false-关闭,如果一个人登录两次,就会将上一次登陆挤下去', 0, 'DEFAULT', '2020-06-09 23:42:37', 1265476890672672808, NULL, NULL);
INSERT INTO `sys_config` VALUES (1280694183769792514, 'druid监控登录账号', 'GUNS_DRUID_USERNAME', '', 'Y', 'druid监控登录账号', 0, 'DEFAULT', '2020-07-08 10:44:22', 1265476890672672808, NULL, NULL);
INSERT INTO `sys_config` VALUES (1280694281648070658, 'druid监控界面登录密码', 'GUNS_DRUID_PASSWORD', '', 'Y', 'druid监控登录密码', 0, 'DEFAULT', '2020-07-08 10:44:46', 1265476890672672808, NULL, NULL);
INSERT INTO `sys_config` VALUES (1280694281648070659, '阿里云定位api接口地址', 'GUNS_IP_GEO_API', 'http://api01.aliyun.venuscn.com/ip?ip=%s', 'Y', '阿里云定位api接口地址', 0, 'DEFAULT', '2020-07-20 10:44:46', 1265476890672672808, NULL, NULL);
INSERT INTO `sys_config` VALUES (1280694281648070660, '阿里云定位appCode', 'GUNS_IP_GEO_APP_CODE', '你的阿里云定位appCode', 'Y', '阿里云定位appCode', 0, 'DEFAULT', '2020-07-20 10:44:46', 1265476890672672808, NULL, NULL);
-- ----------------------------
-- Table structure for sys_dict_data

+ 20 - 1
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/context/constant/ConstantContextHolder.java

@ -234,6 +234,26 @@ public class ConstantContextHolder {
        return getSysConfigWithDefault("GUNS_ENABLE_SINGLE_LOGIN", Boolean.class, false);
    }
    /**
     * 获取阿里云定位接口
     *
     * @author xuyuxiang
     * @date 2020/7/20 9:20
     **/
    public static String getIpGeoApi() {
        return getSysConfig("GUNS_IP_GEO_API", String.class, false);
    }
    /**
     * 获取阿里云定位appCode
     *
     * @author xuyuxiang
     * @date 2020/7/20 10:33
     **/
    public static String getIpGeoAppCode() {
        return getSysConfig("GUNS_IP_GEO_APP_CODE", String.class, false);
    }
    /**
     * 获取config表中的配置,如果为空返回默认值
     *
@ -292,5 +312,4 @@ public class ConstantContextHolder {
            }
        }
    }
}

+ 22 - 32
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/util/IpAddressUtil.java

@ -27,13 +27,21 @@ package cn.stylefeng.guns.core.util;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.log.Log;
import cn.stylefeng.guns.core.consts.CommonConstant;
import cn.stylefeng.guns.core.consts.SymbolConstant;
import cn.stylefeng.guns.core.context.constant.ConstantContext;
import cn.stylefeng.guns.core.context.constant.ConstantContextHolder;
import cn.stylefeng.guns.core.context.requestno.RequestNoContext;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
/**
@ -81,6 +89,7 @@ public class IpAddressUtil {
     * @author xuyuxiang
     * @date 2020/3/16 15:17
     */
    @SuppressWarnings("unchecked")
    public static String getAddress(HttpServletRequest request) {
        String resultJson = SymbolConstant.DASH;
@ -92,41 +101,22 @@ public class IpAddressUtil {
        }
        try {
            //根据url获取地址
            resultJson = HttpUtil.get(URL, genParamMap(ip));
            //获取阿里云定位api接口
            String api = ConstantContextHolder.getIpGeoApi();
            //获取阿里云定位appCode
            String appCode = ConstantContextHolder.getIpGeoAppCode();
            if (ObjectUtil.isAllNotEmpty(api, appCode)) {
                String path = "$['data']['country','region','city','isp']";
                String appCodeSymbol = "APPCODE";
                HttpRequest http = HttpUtil.createGet(String.format(api, ip));
                http.header(CommonConstant.AUTHORIZATION, appCodeSymbol + " " + appCode);
                resultJson = http.timeout(3000).execute().body();
                resultJson = String.join("", (List<String>) JSONPath.read(resultJson, path));
            }
        } catch (Exception e) {
            log.error(">>> 根据ip定位异常,请求号为:{},具体信息为:{}", RequestNoContext.get(), e.getMessage());
            return resultJson;
        }
        if (ObjectUtil.isEmpty(resultJson)) {
            return resultJson;
        } else {
            Object provinceObj = JSON.parseObject(resultJson).get(PROVINCE);
            Object cityObj = JSON.parseObject(resultJson).get(CITY);
            if (ObjectUtil.hasEmpty(provinceObj, cityObj)) {
                return resultJson;
            }
            String province = provinceObj.toString();
            String city = cityObj.toString();
            //拼接 省+市 并返回
            return province.equals(city) ? province : province + city;
        }
    }
    /**
     * 构造map参数
     *
     * @author xuyuxiang
     * @date 2020/3/16 15:17
     */
    private static Map<String, Object> genParamMap(String ip) {
        Map<String, Object> paramMap = CollectionUtil.newHashMap();
        paramMap.put("ip", ip);
        paramMap.put("output", OUTPUT);
        paramMap.put("key", KEY);
        return paramMap;
        return resultJson;
    }
}