提交代码

master
胡锐 4 months ago committed by yuanye
parent 3681028b75
commit d058de5e30
  1. 56
      service/src/main/java/com/hfkj/common/utils/AliyunService.java
  2. 28
      service/src/main/java/com/hfkj/common/utils/AmapApiService.java
  3. 11
      service/src/main/java/com/hfkj/service/impl/SecUserLoginLogServiceImpl.java
  4. 1
      service/src/main/java/com/hfkj/service/impl/SecUserServiceImpl.java
  5. 11
      service/src/main/java/com/hfkj/service/user/impl/BsUserLoginLogServiceImpl.java

@ -1,56 +0,0 @@
package com.hfkj.common.utils;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import java.util.HashMap;
import java.util.Map;
/**
* 阿里云业务服务
* @className: AliyunService
* @author: HuRui
* @date: 2024/4/3
**/
public class AliyunService {
/**
* 查询ip地址
* @param ip ip地址
* @return
*/
public static JSONObject queryAddress(String ip) {
try {
String host = "https://ipaddquery.market.alicloudapi.com";
String path = "/ip/address-query";
String method = "POST";
String appcode = "f9ace4c915054ca697a76fb9a4e1e8c0";
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "APPCODE " + appcode);
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
Map<String, String> bodys = new HashMap<>();
bodys.put("ip", ip);
HttpResponse response = HttpUtils.doPost(host, path, method, headers, new HashMap<>(), bodys);
JSONObject resObj = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
if (resObj.getString("code").equals("200")) {
return resObj.getJSONObject("data");
}
return null;
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) {
try {
System.out.println(queryAddress("123.147.76.209"));
} catch (Exception e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,28 @@
package com.hfkj.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
/**
* 高德API服务
* @className: AmapApiService
* @author: HuRui
* @date: 2024/6/28
**/
public class AmapApiService {
/**
* 获取ip
*/
public static JSONObject queryIp(String ip) {
JSONObject obj = HttpsUtils.doGet("https://restapi.amap.com/v3/ip?key=0083a5b1cc5289d7e9691424ee62136e&ip="+ip);
if (obj.getString("status").equals("1")) {
return obj;
} else {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, obj.getString("info"));
}
}
}

@ -1,7 +1,7 @@
package com.hfkj.service.impl; package com.hfkj.service.impl;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.utils.AliyunService; import com.hfkj.common.utils.AmapApiService;
import com.hfkj.common.utils.RequestUtils; import com.hfkj.common.utils.RequestUtils;
import com.hfkj.dao.SecUserLoginLogMapper; import com.hfkj.dao.SecUserLoginLogMapper;
import com.hfkj.entity.SecUser; import com.hfkj.entity.SecUser;
@ -51,14 +51,11 @@ public class SecUserLoginLogServiceImpl implements SecUserLoginLogService {
loginLog.setUserLoginName(user.getLoginName()); loginLog.setUserLoginName(user.getLoginName());
loginLog.setIp(RequestUtils.getIpAddress(request)); loginLog.setIp(RequestUtils.getIpAddress(request));
// 查询ip归属地 // 查询ip归属地
JSONObject ipAddress = AliyunService.queryAddress(loginLog.getIp()); JSONObject ipAddress = AmapApiService.queryIp(loginLog.getIp());
if (ipAddress != null) { if (ipAddress != null) {
loginLog.setCountry(StringUtils.isNotBlank(ipAddress.getString("country"))?ipAddress.getString("country"):"未知"); loginLog.setRegionName(StringUtils.isNotBlank(ipAddress.getString("province"))?ipAddress.getString("province"):"未知");
loginLog.setRegionId(StringUtils.isNotBlank(ipAddress.getString("region_id"))?ipAddress.getString("region_id"):null); loginLog.setCityId(StringUtils.isNotBlank(ipAddress.getString("adcode"))?ipAddress.getString("adcode"):null);
loginLog.setRegionName(StringUtils.isNotBlank(ipAddress.getString("region"))?ipAddress.getString("region"):"未知");
loginLog.setCityId(StringUtils.isNotBlank(ipAddress.getString("city_id"))?ipAddress.getString("city_id"):null);
loginLog.setCityName(StringUtils.isNotBlank(ipAddress.getString("city"))?ipAddress.getString("city"):"未知"); loginLog.setCityName(StringUtils.isNotBlank(ipAddress.getString("city"))?ipAddress.getString("city"):"未知");
loginLog.setIsp(StringUtils.isNotBlank(ipAddress.getString("isp"))?ipAddress.getString("isp"):"未知");
loginLog.setStatus(SecUserLoginLogStatusEnum.status1.getCode()); loginLog.setStatus(SecUserLoginLogStatusEnum.status1.getCode());
} else { } else {
loginLog.setCountry("未知"); loginLog.setCountry("未知");

@ -7,7 +7,6 @@ import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.AESEncodeUtil; import com.hfkj.common.security.AESEncodeUtil;
import com.hfkj.common.security.SessionObject; import com.hfkj.common.security.SessionObject;
import com.hfkj.common.security.UserCenter; import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.AliyunService;
import com.hfkj.common.utils.MD5Util; import com.hfkj.common.utils.MD5Util;
import com.hfkj.common.utils.RequestUtils; import com.hfkj.common.utils.RequestUtils;
import com.hfkj.dao.SecUserMapper; import com.hfkj.dao.SecUserMapper;

@ -1,7 +1,7 @@
package com.hfkj.service.user.impl; package com.hfkj.service.user.impl;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.utils.AliyunService; import com.hfkj.common.utils.AmapApiService;
import com.hfkj.common.utils.RequestUtils; import com.hfkj.common.utils.RequestUtils;
import com.hfkj.dao.BsUserLoginLogMapper; import com.hfkj.dao.BsUserLoginLogMapper;
import com.hfkj.entity.*; import com.hfkj.entity.*;
@ -53,14 +53,11 @@ public class BsUserLoginLogServiceImpl implements BsUserLoginLogService {
loginLog.setUserId(user.getId()); loginLog.setUserId(user.getId());
loginLog.setIp(RequestUtils.getIpAddress(request)); loginLog.setIp(RequestUtils.getIpAddress(request));
// 查询ip归属地 // 查询ip归属地
JSONObject ipAddress = AliyunService.queryAddress(loginLog.getIp()); JSONObject ipAddress = AmapApiService.queryIp(loginLog.getIp());
if (ipAddress != null) { if (ipAddress != null) {
loginLog.setCountry(StringUtils.isNotBlank(ipAddress.getString("country"))?ipAddress.getString("country"):"未知"); loginLog.setRegionName(StringUtils.isNotBlank(ipAddress.getString("province"))?ipAddress.getString("province"):"未知");
loginLog.setRegionId(StringUtils.isNotBlank(ipAddress.getString("region_id"))?ipAddress.getString("region_id"):null); loginLog.setCityId(StringUtils.isNotBlank(ipAddress.getString("adcode"))?ipAddress.getString("adcode"):null);
loginLog.setRegionName(StringUtils.isNotBlank(ipAddress.getString("region"))?ipAddress.getString("region"):"未知");
loginLog.setCityId(StringUtils.isNotBlank(ipAddress.getString("city_id"))?ipAddress.getString("city_id"):null);
loginLog.setCityName(StringUtils.isNotBlank(ipAddress.getString("city"))?ipAddress.getString("city"):"未知"); loginLog.setCityName(StringUtils.isNotBlank(ipAddress.getString("city"))?ipAddress.getString("city"):"未知");
loginLog.setIsp(StringUtils.isNotBlank(ipAddress.getString("isp"))?ipAddress.getString("isp"):"未知");
loginLog.setStatus(SecUserLoginLogStatusEnum.status1.getCode()); loginLog.setStatus(SecUserLoginLogStatusEnum.status1.getCode());
} else { } else {
loginLog.setCountry("未知"); loginLog.setCountry("未知");

Loading…
Cancel
Save