|
|
@@ -216,7 +216,7 @@ public class WebSocketServerHandler extends BaseWebSocketServerHandler {
|
|
|
handshaker = wsFactory.newHandshaker(req);
|
|
|
Constant.handShakerMap.put(ctx.channel().id().asLongText(), handshaker);
|
|
|
// 在这里处理用户登录;
|
|
|
- handleWsLogin(ctx, req.uri());
|
|
|
+ handleWsLogin(ctx, req.uri(), req);
|
|
|
}
|
|
|
if (handshaker == null) {
|
|
|
// 不支持
|
|
|
@@ -226,7 +226,7 @@ public class WebSocketServerHandler extends BaseWebSocketServerHandler {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void handleWsLogin(ChannelHandlerContext ctx, final String requestURI) {
|
|
|
+ private void handleWsLogin(ChannelHandlerContext ctx, final String requestURI, FullHttpRequest req) {
|
|
|
WebsocketThreadPool.addTask(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
@@ -280,8 +280,26 @@ public class WebSocketServerHandler extends BaseWebSocketServerHandler {
|
|
|
jsonObject.put("groups", groups);
|
|
|
replyMsg.setStatus(200);
|
|
|
replyMsg.setObject(jsonObject);
|
|
|
- String remoteAddr = ctx.channel().remoteAddress().toString();
|
|
|
- String clientIP = CommonUtils.getIpFromFullAddress(remoteAddr);
|
|
|
+
|
|
|
+ // 优先从 Nginx 传递的 header 获取真实 IP
|
|
|
+ String clientIP = req.headers().get("X-Real-IP");
|
|
|
+ if (clientIP == null || clientIP.isEmpty()) {
|
|
|
+ clientIP = req.headers().get("X-Forwarded-For");
|
|
|
+ // X-Forwarded-For 可能是多个 IP,取第一个
|
|
|
+ if (clientIP != null && clientIP.contains(",")) {
|
|
|
+ clientIP = clientIP.split(",")[0].trim();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 兜底:如果 header 为空,再用 remoteAddress
|
|
|
+ if (clientIP == null || clientIP.isEmpty()) {
|
|
|
+ String remoteAddr = ctx.channel().remoteAddress().toString();
|
|
|
+ clientIP = CommonUtils.getIpFromFullAddress(remoteAddr);
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.info("real client IP: {}", clientIP);
|
|
|
+
|
|
|
+// String remoteAddr = ctx.channel().remoteAddress().toString();
|
|
|
+// String clientIP = CommonUtils.getIpFromFullAddress(remoteAddr);
|
|
|
SessionEntity sessionEntity = new SessionEntity();
|
|
|
sessionEntity.setClientIp(clientIP);
|
|
|
sessionEntity.setExtNum(extnum);
|