简体   繁体   English

请问javax.servlet.Filter拦截Websocket消息? (Java Servlet API)

[英]Will javax.servlet.Filter intercept Websocket messages? (Java Servlet API)

When I have a websocket connection, say at "/ws" 当我有一个websocket连接时,请说“/ ws”

Will a regular javax.servlet.Filter on the "/ws" path intercept each message as a regular http request? “/ ws”路径上的常规javax.servlet.Filter是否会将每条消息作为常规http请求拦截? Will it intercept only the first request (as part of the handshake?) 它会截取第一个请求(作为握手的一部分吗?)

If no, then, how can I make such a filter for websocket messages? 如果不是,那么,我如何为websocket消息制作这样的过滤器?

I don't find any specifics about that in the spec. 我没有在规范中找到任何具体细节。

No, Servlet Filter won't catch WebSocket messages. 不,Servlet过滤器不会捕获WebSocket消息。

Servlet does not support WebSocket protocol at all. Servlet根本不支持WebSocket协议。 Only thing you can do is to register HttpUpgradeHandler (see HttpServletRequest#upgrade )and then implement WebSocket protocol on top of that. 你唯一能做的就是注册HttpUpgradeHandler (参见HttpServletRequest#upgrade ),然后在其上实现WebSocket协议。

Or.. if you want to save some time, take a look at JSR 356 and the implementations, they are working on top of this API. 或者..如果你想节省一些时间,看看JSR 356和实现,他们正在使用这个API。 I personally work on Tyrus , which is the reference implementation of JSR 356. (It is part of Java EE 7, but you only need Servlet 3.1 API). 我个人在Tyrus上工作,这是JSR 356的参考实现。(它是Java EE 7的一部分,但您只需要Servlet 3.1 API)。

Will it intercept only the first request (as part of the handshake?) 它会截取第一个请求(作为握手的一部分吗?)

Yes, only the first request, the one that initiates the WebSocket Handshake, is intercepted by the Servlet Filter. 是的,只有第一个请求,即启动WebSocket握手的请求,才被Servlet过滤器拦截。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM