是否可以在@ServerEndpoint中获取HttpServletRequest?主要是我试图得到它所以我可以访问HttpSession对象.
我想在websocket handshake\session与HttpSession对象之间建立连接.
我使用了以下握手修改:
public class GetHttpSessionConfigurator extends ServerEndpointConfig.Configurator
{
@Override
public void modifyHandshake(ServerEndpointConfig config,
HandshakeRequest request,
HandshakeResponse response)
{
HttpSession httpSession = (HttpSession)request.getHttpSession();
config.getUserProperties().put(HttpSession.class.getName(),httpSession);
}
}
Run Code Online (Sandbox Code Playgroud)
正如本文所述: 在Web Socket @ServerEndpoint中从HttpServletRequest访问HttpSession
现在,由于某些原因在握手时,(HttpSession)request.getHttpSession()一直返回null.
这是我的客户端代码:
<!DOCTYPE html>
<html>
<head>
<title>Testing websockets</title>
</head>
<body>
<div>
<input type="submit" value="Start" onclick="start()" />
</div>
<div id="messages"></div>
<script type="text/javascript">
var webSocket =
new WebSocket('ws://localhost:8080/com-byteslounge-websockets/websocket');
webSocket.onerror = function(event) {
onError(event)
};
webSocket.onopen = function(event) {
onOpen(event)
};
webSocket.onmessage = function(event) {
onMessage(event)
};
function onMessage(event) {
document.getElementById('messages').innerHTML
+= '<br …Run Code Online (Sandbox Code Playgroud) 我制作了一个JSR-356 @ServerEndpoint,其中我想限制来自单个IP地址的活动连接,以防止简单的DDOS攻击.
请注意,我正在搜索Java解决方案(JSR-356,Tomcat或Servlet 3.0规范).
我尝试过自定义端点配置程序,但即使在HandshakeRequest对象中也无法访问IP地址.
如何在没有iptables等外部软件的情况下限制单个IP地址的JSR-356连接数?
我需要访问HttpServletRequest属性以获取包含TLS 请求的证书数组的javax.servlet.request.X509CertificateX509Certificate。
从 JAX-RSContainerRequestFilter我可以轻松地从ContainerRequestContext.getProperty(String property)方法中提取它,但是我找不到从 WebSocketSession或 中获取它的方法HandshakeRequest,从中我可以访问HttpSession实例但不能访问实例HttpServletRequest。
注意:这不是Accessing HttpSession from HttpServletRequest in a Web Socket @ServerEndpoint因为我需要访问HttpServletRequest(或等效于提取 TLS 证书),而不是HttpSession.
由于 WebSocket 是 HTTP 的超集,我想它应该是可能的,希望 Java 团队已经想到了一种访问 servlet 属性的方法,但我真的找不到。任何人都知道这是否可能?
websocket ×4
java ×2
servlets ×2
tomcat ×2
httpsession ×1
jakarta-ee ×1
java-ee-7 ×1
jsr356 ×1
session ×1