Java 如何在SpringBoot中获取Rsocket连接的远程IP地址
我正在尝试获取连接到RSocket+SpringBoot Web服务器的浏览器的远程IP。连接是通过WebSocket进行的 Web服务器是Java-8,SpringBoot-2,通过WebSocket使用RSocket,并将RequestStreams发送到浏览器。我正在利用SpringBoot自动配置进行RSocket设置,因此服务器端的代码非常少——请参见下文 下面代码中的Java 如何在SpringBoot中获取Rsocket连接的远程IP地址,java,spring-boot,spring-webflux,rsocket,Java,Spring Boot,Spring Webflux,Rsocket,我正在尝试获取连接到RSocket+SpringBoot Web服务器的浏览器的远程IP。连接是通过WebSocket进行的 Web服务器是Java-8,SpringBoot-2,通过WebSocket使用RSocket,并将RequestStreams发送到浏览器。我正在利用SpringBoot自动配置进行RSocket设置,因此服务器端的代码非常少——请参见下文 下面代码中的@Headers和MessageHeader只是为了看看它们是否有任何可能导致远程IP的东西,没有其他原因 我在网上搜
@Headers和MessageHeader
只是为了看看它们是否有任何可能导致远程IP的东西,没有其他原因
我在网上搜寻答案——很多是http,一些是WebSocket,zero是RSocket。
这--看起来很有希望,但无法处理双工连接,因此没有雪茄
有什么想法吗?谢谢
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());
}
/**
*TODO:获取远程IP地址和日志。
*选项:
* 1. @看见
* 2. 通过nginx在报头中注入IP。查看它是否显示在此处的@Headers参数中。
* 3. 浏览器获取其公共IP并将其添加到请求对象。行得通,但站不住脚
* 4. (不太可能)找到通过此私有成员链访问的方法:headers.req.rsocket().source.connection.source.connection.connection.channel.remoteAddress
*/
@消息映射(“订阅主题”)
公共流量订阅流量(
@标题映射HDR,
消息头mh,
测试请求(请求){
返回testService.subscribeTopic(request.getRequestId(),request.getTopic());
}
客户端的ip地址在双工连接
类中可用。您可以向RSocketServer
添加拦截器,如下所示:
@Bean
public RSocketServerCustomizer ipCustomizer() {
return rSocketServer -> rSocketServer.interceptors(registry -> registry.forConnection(new ConnectionInterceptor()));
}
其中,ConnectionInterceptor
是:
static class ConnectionInterceptor implements DuplexConnectionInterceptor {
@Override
public DuplexConnection apply(Type type, DuplexConnection duplexConnection) {
SocketAddress socketAddress = duplexConnection.remoteAddress();
if (socketAddress instanceof InetSocketAddress) {
InetSocketAddress iso = (InetSocketAddress) socketAddress;
// Here is the ip: iso.getHostString()
}
return duplexConnection;
}
}