我正在尝试获取连接到RSocket+SpringBoot Web服务器的浏览器的远程IP地址。连接方式为RSocket over WebSocket。
该Web服务器使用Java-8,SpringBoot-2,使用RSocket over WebSocket并向浏览器发送RequestStreams。我利用SpringBoot自动配置进行RSocket设置,在服务器端只需编写极少量的代码 - 如下所示。
下面代码中的@Headers
和MessageHeader
只是为了查看它们是否有任何可能导致远程IP地址的内容,没有其他原因。
我搜遍了网络寻找答案 - 对于HTTP有很多,对于WebSockets有一些,但对于RSocket则零。
这个问题 https://github.com/rsocket/rsocket-java/issues/735 看起来很有前途,但无法获取DuplexConnection的句柄,所以无法解决该问题。
有任何想法吗?谢谢!
application.yml:
spring.rsocket.server:
mapping-path: /rsocket-test
transport: websocket
server.port: 8080
TestController.java
/**
* TODO: get the remote IP address and log.
* Options:
* 1. @see <a href="https://github.com/rsocket/rsocket-java/issues/735">Ability to intercept requests and access channel information such as remote address</a>.
* 2. Inject IP in header by nginx. See if it shows up in the @Headers param here.
* 3. Browser gets its public IP and adds it to the request object. Doable, but lame
* 4. (Unlikely) Find a way to access thru this chain of private members: headers.req.rsocket().source.connection.source.connection.connection.channel.remoteAddress
*/
@MessageMapping("subscribe-topic")
public Flux<StreamingEvent> subscribeToEventStream(
@Headers Map<String,Object> hdrs,
MessageHeaders mh,
testRequest request) {
return testService.subscribeTopic(request.getRequestId(), request.getTopic());
}