带有Tomcat的JSR-356 WebSockets - 如何限制单个IP地址内的连接? [英] JSR-356 WebSockets with Tomcat - How to limit connections within single IP address?

查看:136
本文介绍了带有Tomcat的JSR-356 WebSockets - 如何限制单个IP地址内的连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个JSR-356 @ServerEndpoint ,其中我想限制来自单个IP地址的活动连接,以防止简单的DDOS攻击。

I made a JSR-356 @ServerEndpoint in which I want to limit alive connections from single IP address, to prevent simple DDOS attacks.

请注意,我正在搜索Java解决方案(JSR-356,Tomcat或Servlet 3.0规范)。

Note that I'm search for Java solution (JSR-356, Tomcat or Servlet 3.0 specs).

我试过自定义端点配置器但即使在 HandshakeRequest 对象中,我也无法访问IP地址。

I have tried custom endpoint configurer but I don't have access to IP address even in HandshakeRequest object.

如何限制JSR没有像iptables这样的外部软件的单个IP地址的-356连接数?

推荐答案

根据Tomcat开发人员@ mark- thomas客户端IP 通过JSR-356公开,因此用纯JSR-356 API-s实现这样的功能是不可能的。

According to Tomcat developer @mark-thomas client IP is not exposed via JSR-356 thus it is impossible to implement such a function with pure JSR-356 API-s.

你必须使用一个相当丑陋的黑客来解决标准的限制。

You have to use a rather ugly hack to work around the limitation of the standard.

需要做的事情归结为:


  1. 在每个用户初始生成包含其IP的令牌任务(在websocket握手之前)

  2. 将令牌传递到链中,直到达到端点实现

至少有两个hacky选项可以达到这个目的。

There are at least two hacky options to achieve that.


  1. 使用 ServletRequestListener监听传入的HTTP请求

  2. 调用 request.getSession()传入请求以确保它具有会话并将客户端IP存储为会话属性。

  3. 创建 ServerEndpointConfig.Configurator HandshakeRequest#getHttpSession 中提取客户端IP,并使用将其作为用户属性附加到 EndpointConfig modifyHandshake 方法。

  4. EndpointConfig 用户属性获取客户端IP,将其存储在地图或其他内容中如果每个IP的会话数超过阈值,则触发清理逻辑。

  1. Listen to incoming HTTP requests with a ServletRequestListener
  2. Call request.getSession() on incoming request to ensure it has a session and store client IP as a session attribute.
  3. Create a ServerEndpointConfig.Configurator that lifts client IP from HandshakeRequest#getHttpSession and attaches it to EndpointConfig as a user property using the modifyHandshake method.
  4. Get the client IP from EndpointConfig user properties, store it in map or whatever and trigger cleanup logic if the number of sessions per IP exceeds a threshold.

您还可以使用 @WebFilter 而不是 ServletRequestListener

请注意,除非您的应用程序已经存在,否则此选项的资源消耗很高使用会话,例如用于身份验证。

Note that this option can have a high resource consumption unless your application already uses sessions e.g. for authentication purposes.


  1. 创建一个附加到非websocket入口点的servlet或过滤器。例如 / mychat

  2. 获取客户端IP,使用随机盐和密钥对其进行加密以生成令牌。

  3. 使用 ServletRequest#getRequestDispatcher 将请求转发到 / mychat / TOKEN

  4. 配置端点以使用路径参数,例如 @ServerEndpoint(/ mychat / {token})

  5. @PathParam提取令牌并解密以获取客户端IP。如果每个IP的会话数超过阈值,则将其存储在地图或其他内容中并触发清理逻辑。

  1. Create a servlet or a filter that attaches to a non websocket entry point. e.g. /mychat
  2. Get client IP, encrypt it with a random salt and a secret key to generate a token.
  3. Use ServletRequest#getRequestDispatcher to forward the request to /mychat/TOKEN
  4. Configure your endpoint to use path parameters e.g. @ServerEndpoint("/mychat/{token}")
  5. Lift the token from @PathParam and decrypt to get client IP. Store it in map or whatever and trigger cleanup logic if the number of sessions per IP exceeds a threshold.

为了便于安装,您可以希望在应用程序启动时生成加密密钥。

For ease of installation you may wish to generate encryption keys on application startup.

请注意,即使您正在进行客户端不可见的内部调度,也需要加密IP。没有任何东西可以阻止攻击者直接连接到 /mychat/2.3.4.5 ,从而欺骗客户端IP(如果它没有加密)。

Please note that you need to encrypt the IP even if you are doing an internal dispatch that is not visible to the client. There is nothing that would stop an attacker from connecting to /mychat/2.3.4.5 directly thus spoofing the client IP if it's not encrypted.

参见:

  • apache tomcat 8 websocket origin and client address
  • Find number of active sessions created from a given client IP
  • Accessing HttpSession from HttpServletRequest in a Web Socket @ServerEndpoint
  • https://tyrus.java.net/documentation/1.4/index/websocket-api.html
  • http://docs.oracle.com/javaee/7/tutorial/doc/websocket010.htm#BABJAIGH

这篇关于带有Tomcat的JSR-356 WebSockets - 如何限制单个IP地址内的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆