Browse Source

加下ip

master
nili 10 months ago
parent
commit
4e3f8104c4
  1. 4
      game-service/src/main/java/awesome/group/game/service/MatrixService.java
  2. 40
      game-web/src/main/java/awesome/group/RequestContext.java
  3. 3
      game-web/src/main/java/awesome/group/controller/MatrixController.java

4
game-service/src/main/java/awesome/group/game/service/MatrixService.java

@ -14,7 +14,7 @@ public class MatrixService {
@Resource
private MatrixAdvRecordMapper mapper;
public void saveRecord(MatrixAdvRecordBo bo) {
public void saveRecord(MatrixAdvRecordBo bo, String ip) {
Assert.isTrue(StringUtils.hasText(bo.appCode), "appCode不能为空");
Integer appId = mapper.queryAppId(bo.appCode);
if (appId == null) {
@ -28,7 +28,7 @@ public class MatrixService {
record.setEcpm(bo.ecpm);
record.setDeviceBrand(bo.deviceBrand);
record.setDeviceName(bo.deviceName);
record.setIp(bo.ip);
record.setIp(ip);
mapper.insert(record);
}
}

40
game-web/src/main/java/awesome/group/RequestContext.java

@ -3,6 +3,8 @@ package awesome.group;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
@ -47,4 +49,42 @@ public class RequestContext {
THREAD_LOCAL.remove();
}
public static String getIpAddr(HttpServletRequest request) {
String ipAddress = null;
try {
ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (ipAddress.equals("127.0.0.1")) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inet.getHostAddress();
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
// = 15
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
} catch (Exception e) {
ipAddress="";
}
// ipAddress = this.getRequest().getRemoteAddr();
return ipAddress;
}
}

3
game-web/src/main/java/awesome/group/controller/MatrixController.java

@ -1,5 +1,6 @@
package awesome.group.controller;
import awesome.group.RequestContext;
import awesome.group.aop.RestApi;
import awesome.group.game.service.MatrixService;
import awesome.group.game.service.bo.MatrixAdvRecordBo;
@ -19,7 +20,7 @@ public class MatrixController {
@PostMapping("/saveAdvRecord")
@RestApi
public R<Void> saveAdvRecord(@RequestBody MatrixAdvRecordBo bo) {
matrixService.saveRecord(bo);
matrixService.saveRecord(bo, RequestContext.getIpAddr(RequestContext.getRequest()));
return new R<>(R.CODE_SUCCESS, "ok", null);
}
}

Loading…
Cancel
Save