我使用this Websocket library和here中的示例来创建基于websockets的多个服务.
接收请求的主服务器是Nginx服务器,它根据其来源(基于域)转发请求,因此我可以确定收到的请求是针对websockets的.
然而问题在于:
public void onOpen(WebSocket ws, ClientHandshake handshake) {
System.out.println("-------------------------------");
System.out.println(ws.getRemoteSocketAddress());
System.out.println("-------------------------------");
}
getRemoteSocketAddress将始终返回nginx服务器IP,例如/192.168.1.100:43556
我的nginx配置是:
server{
listen 80;
server_name we.mydomain.com;
access_log /var/log/nginx/access-ws-debug.log;
error_log /var/log/nginx/error-ws-debug.log;
set_real_ip_from 192.168.1.0/24;
real_ip_header X-Real-IP;
real_ip_recursive on;
location / {
proxy_pass http://192.168.1.101:54321;
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host; #host
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Y-Real-IP $realip_remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_connect_timeout 1d;
proxy_send_timeout 1d;
proxy_read_timeout 1d;
}
}
nxing配置有问题吗?或者有没有其他方法来获取websocket背后的用户的IP?
感谢Mark的回答.以下代码与提到的库无关.这是没有任何库的jsp中的“经典”Web实现.
我修改了握手,看起来像这样:
@Override
public void modifyHandshake(
ServerEndpointConfig config,
HandshakeRequest request,
HandshakeResponse response
) {
HttpSession httpSession = (HttpSession) request.getHttpSession();
config.getUserProperties().put(HttpSession.class.getName(), httpSession);
config.getUserProperties().put("Headers",request.getHeaders());
}
要获得Headers,只需在OnOpen中调用它:
@OnOpen
public void onOpen(final Session session, EndpointConfig config){ //This happens when javascript opens socket session
Object list = config.getUserProperties().get("Headers");
System.out.println("-------------------------------------");
System.out.println(list);
System.out.println("-------------------------------------");
}
返回的标题格式如下(使用我的手机):
{upgrade=[websocket],
accept-language=[en-US,en;q=0.9,es;q=0.8],
cookie=[JSESSIONID=0BAE99DFDE5DD81D20CFF04E4257D7FE],
sec-websocket-extensions=[permessage-deflate; client_max_window_bits],
origin=[https://mywebsite.com],
x-forwarded-for=[1.2.3.4],
sec-websocket-version=[13],
pragma=[no-cache],
x-real-ip=[1.2.3.4],
y-real-ip=[1.2.3.4],
host=[mywebsite.com],
connection=[Upgrade],
sec-websocket-key=[ZroOewrBGrg660x7ocI+nog==],
cache-control=[no-cache],
accept-encoding=[gzip, deflate, br],
user-agent=[Mozilla/5.0 (Linux; Android 8.0.0; SM-N950F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.80 Mobile Safari/537.36]}
IP在我所在地区附近可以进行地理定位.
解决方法:
这不是一项微不足道的任务,但您的配置看起来不错,您只需从您设置的标头(X-Real-IP)中获取原始IP.
import javax.websocket.*;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpoint;
import javax.websocket.server.ServerEndpointConfig;
@ServerEndpoint(value = "/test", configurator = Test.TestConfiguratior.class)
public class Test extends Endpoint {
@Override
public void onOpen(Session session, EndpointConfig endpointConfig) {
Object o = endpointConfig.getUserProperties().get(TestConfiguratior.HEADER_NAME);
//o should be the string object you are looking for
}
@OnMessage
public void onMessage(Session session, String msg) {
//do what u want
}
public static class TestConfiguratior extends ServerEndpointConfig.Configurator {
static String HEADER_NAME = "X-Real-IP";
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
String firstFoundHeader = request.getHeaders().get(HEADER_NAME.toLowerCase()).get(0);
sec.getUserProperties().put(HEADER_NAME, firstFoundHeader);
}
}
}
TL.DR.:您可以使用javax.websocket.EndpointConfig#getUserProperties映射将数据放入其中并稍后检索.最初我认为这会在使用这个确切的代码时导致一些并发问题,但我无法重现它.甚至文档也建议这样做:
https://javaee.github.io/tutorial/websocket010.html
更新:这是一个JSR 356示例,并不特定于上述框架.
标签:nginx,java,websocket
来源: https://codeday.me/bug/20190701/1346077.html