From eb321aba7cac347934886e9c8ac6927ad587fc0f Mon Sep 17 00:00:00 2001 From: bayuzhen Date: Wed, 28 Aug 2024 21:30:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../booter/controller/LoginController.java | 4 +++ .../sms/booter/controller/TrxController.java | 6 +++-- .../bocloud/sms/interfaces/TrxService.java | 3 +++ .../bocloud/sms/service/TrxServiceImpl.java | 26 ++++++++++++++++--- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/bocloud.sms.booter/src/main/java/com/bocloud/sms/booter/controller/LoginController.java b/bocloud.sms.booter/src/main/java/com/bocloud/sms/booter/controller/LoginController.java index f5485ef..05392de 100644 --- a/bocloud.sms.booter/src/main/java/com/bocloud/sms/booter/controller/LoginController.java +++ b/bocloud.sms.booter/src/main/java/com/bocloud/sms/booter/controller/LoginController.java @@ -4,6 +4,7 @@ import com.bocloud.sms.entity.AccountSecurity; import com.bocloud.sms.entity.SystemConfig; import com.bocloud.sms.entity.TenantAccount; import com.bocloud.sms.interfaces.TenantService; +import com.bocloud.sms.interfaces.TrxService; import com.bocloud.sms.interfaces.UserService; import com.bocloud.sms.model.LoginModel; import com.bocloud.sms.repository.SystemConfigRepository; @@ -49,6 +50,7 @@ public class LoginController { private final SystemConfigRepository systemConfigRepository; private final StringRedisTemplate redisTemplate; private final LockFactory lockFactory; + private final TrxService trxService; /** * 用户登录 @@ -68,6 +70,8 @@ public class LoginController { log.info("开始调用UserService"); GeneralResult> loginResult = userService .login(account, password, request.getSession().getId(), IpTool.getIP(request), loginModel.getIsManager()); + log.info("请求方ip地址trx:" + trxService.getRequestIpAddress(request)); + log.info("请求方ip地址:" + IpTool.getIP(request)); log.info("结束调用UserService"); if (loginResult.isFailed()) { if (null == loginResult.getData()) { diff --git a/bocloud.sms.booter/src/main/java/com/bocloud/sms/booter/controller/TrxController.java b/bocloud.sms.booter/src/main/java/com/bocloud/sms/booter/controller/TrxController.java index 179ecfe..040b464 100644 --- a/bocloud.sms.booter/src/main/java/com/bocloud/sms/booter/controller/TrxController.java +++ b/bocloud.sms.booter/src/main/java/com/bocloud/sms/booter/controller/TrxController.java @@ -36,7 +36,8 @@ public class TrxController { @Operation(summary = "用户登录") public GeneralResult getAuthToken(@RequestBody TrxAuthModel trxAuthModel, HttpServletRequest request) { - trxAuthModel.setClientIp(request.getRemoteAddr()); + String ipAddress = trxService.getRequestIpAddress(request); + trxAuthModel.setClientIp(ipAddress); GeneralResult generalResult = trxService.getAuthToken(trxAuthModel); return generalResult; } @@ -45,7 +46,8 @@ public class TrxController { @Operation(summary = "天融信用户登出") public GeneralResult logout(@RequestBody TrxAuthModel trxAuthModel, HttpServletRequest request) { - trxAuthModel.setClientIp(request.getRemoteAddr()); + String ipAddress = trxService.getRequestIpAddress(request); + trxAuthModel.setClientIp(ipAddress); GeneralResult generalResult = trxService.logout(trxAuthModel); return generalResult; } diff --git a/bocloud.sms.service/src/main/java/com/bocloud/sms/interfaces/TrxService.java b/bocloud.sms.service/src/main/java/com/bocloud/sms/interfaces/TrxService.java index c3e2493..e37ef71 100644 --- a/bocloud.sms.service/src/main/java/com/bocloud/sms/interfaces/TrxService.java +++ b/bocloud.sms.service/src/main/java/com/bocloud/sms/interfaces/TrxService.java @@ -2,6 +2,7 @@ package com.bocloud.sms.interfaces; import com.bocloud.sms.model.TrxAuthModel; import com.megatron.common.model.GeneralResult; +import jakarta.servlet.http.HttpServletRequest; public interface TrxService { GeneralResult getRandomstr(String ngxCookie); @@ -9,4 +10,6 @@ public interface TrxService { GeneralResult getAuthToken(TrxAuthModel trxAuthModel); GeneralResult logout(TrxAuthModel trxAuthModel); + + String getRequestIpAddress(HttpServletRequest request); } diff --git a/bocloud.sms.service/src/main/java/com/bocloud/sms/service/TrxServiceImpl.java b/bocloud.sms.service/src/main/java/com/bocloud/sms/service/TrxServiceImpl.java index 0b388a0..2deb8ee 100644 --- a/bocloud.sms.service/src/main/java/com/bocloud/sms/service/TrxServiceImpl.java +++ b/bocloud.sms.service/src/main/java/com/bocloud/sms/service/TrxServiceImpl.java @@ -22,6 +22,7 @@ import com.megatron.common.encrypt.AESEncryptor; import com.megatron.common.encrypt.Encryptor; import com.megatron.common.model.GeneralResult; import com.megatron.common.model.RequestContext; +import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -61,10 +62,10 @@ public class TrxServiceImpl implements TrxService { paramsMap.put("ngx_cookie", ngxCookie); JSONObject result = new JSONObject(); try { - log.info("调用天融信获取随机字符串接口, url:" + url + ", 参数:" + JSONObject.toJSONString(paramsMap)); - HttpResponse response = HttpRequest.get(url) - .addHeaders(paramsMap) - .execute(); + HttpRequest httpRequest = HttpRequest.get(url) + .addHeaders(paramsMap); + log.info("调用天融信获取随机字符串接口, url:" + httpRequest.getUrl() + "请求头:" + JSONObject.toJSONString(httpRequest.headers())); + HttpResponse response = httpRequest.execute(); result = JSONObject.parseObject(response.body()); if ("-1".equals(result.getString("result"))) { return new GeneralResult(false, "获取随机数失败" + result.getString("errmsg")); @@ -233,4 +234,21 @@ public class TrxServiceImpl implements TrxService { return appModules; } + public String getRequestIpAddress(HttpServletRequest request) { + String ip = request.getHeader("X-Forwarded-For"); + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("X-Real-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("WL-Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getRemoteAddr(); + } + return ip; + } + }