【问题标题】:Spring Websockets STOMP - get client IP addressSpring Websockets STOMP - 获取客户端IP地址
【发布时间】:2015-10-18 15:10:36
【问题描述】:

有没有办法获取STOMP客户端IP地址?我正在拦截入站通道,但我看不到任何检查 IP 地址的方法。

任何帮助表示赞赏。

【问题讨论】:

  • 我发现可以从 WebSocketSession 中获取该 IP 地址...

标签: stomp spring-websocket


【解决方案1】:

您可以在与HandshakeInterceptor 握手期间将客户端 IP 设置为 WebSocket 会话属性:

public class IpHandshakeInterceptor implements HandshakeInterceptor {

    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
            WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {

        // Set ip attribute to WebSocket session
        attributes.put("ip", request.getRemoteAddress());

        return true;
    }

    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
            WebSocketHandler wsHandler, Exception exception) {          
    }
}

使用握手拦截器配置您的端点:

@Override
protected void configureStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/ws").addInterceptors(new IpHandshakeInterceptor()).withSockJS();
}

并使用标头访问器在处理程序方法中获取属性:

@MessageMapping("/destination")
public void handlerMethod(SimpMessageHeaderAccessor ha) {
    String ip = (String) ha.getSessionAttributes().get("ip");
    ...
}

【讨论】:

  • 是否可以在不传递 SimpMessageHeaderAccessor 的情况下在服务中获取此属性?我的意思类似于注入 HttpServletRequest
  • 总是获取ip为127.0.0.1。如何获取远程客户端的ip?
  • 真是天才! :) 感谢您对集成细节的深入回答。我很感激。
【解决方案2】:

以下示例已更新以获取确切的远程客户端 ip:

@Component
public class IpHandshakeInterceptor implements HandshakeInterceptor {

   @Override
   public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
                               WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
    // Set ip attribute to WebSocket session
    if (request instanceof ServletServerHttpRequest) {
        ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
        String ipAddress = servletRequest.getServletRequest().getHeader("X-FORWARDED-FOR");
        if (ipAddress == null) {
            ipAddress = servletRequest.getServletRequest().getRemoteAddr();
        }
        attributes.put("ip", ipAddress);
    }
    return true;
}

   public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
                           WebSocketHandler wsHandler, Exception exception) {
}
}

【讨论】:

  • 看起来您的应用程序位于反向代理之后,这就是为什么您必须获取“X-FORWARDED-FOR”标头的原因。更多信息在这里:en.wikipedia.org/wiki/X-Forwarded-For 所以感觉您的陈述“更新以下示例以获取确切的远程客户端 ip”实际上不是您在编写答案时假设的典型情况,而是针对更具体的情况。
【解决方案3】:

试图将此信息添加为评论,但 stackoverflow 抱怨评论太长,所以我将其发布为答案

可以在不通过的情况下在服务中获取此属性 SimpMessageHeaderAccessor ?我的意思类似于注射 HttpServletRequest – 鲨鱼 2015 年 11 月 23 日 15:02

问题。

我能够用这样的语法实现“接近”的结果:

@MessageMapping("/destination")
@SendTo("/topic/someTopic")
public String send(@Header("simpSessionAttributes") Map<String, Object> sessionAttributes) {
    String clientsAddress = sessionAttributes.get("ip"));
    return "The sender's address is: " + clientsAddress ;
}

我不熟悉消息标题的“simpSessionAttributes”名称的来源,但我注意到如果我以@Sergi Almar 在此线程中描述的方式放置信息 - 我得到这样结果。但也许这个名称“simpSessionAttributes”可能取决于某些环境配置或消息框架 idk 的特定实现......

另外,我不介意在@Sergi Almar 的回答中包含这种详细说明。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 2013-07-07
    • 2011-05-26
    • 2019-11-13
    • 1970-01-01
    • 2011-01-08
    • 2014-12-29
    相关资源
    最近更新 更多