![](/img/trans.png)
[英]websocket stomp in spring over sockjs redirects to http protocol while the origin is https in pivotal web services
[英]WebSocket Stomp over SockJS - http custom headers
我在我的 javascript 客户端中通过 SockJS 使用 stomp.js。 我正在使用
stompClient.connect({}, function (frame) {
stomp over sockJS 连接有 2 个 http 请求:
客户端发送所有 cookie。 我还想发送自定义标头(例如 XSRF 标头),但没有找到方法。 将不胜感激任何帮助。
@Rohitdev 所以基本上你不能使用 stompClient 发送任何HTTP标头,因为 STOMP 是 websockets 的层,只有当 websockets 握手发生时,我们才有可能发送自定义标头。 所以只有 SockJS 可以发送这个标头,但由于某些原因不要这样做: https : //github.com/sockjs/sockjs-client/issues/196
自定义标题:
stompClient.connect({token: "ABC123"}, function(frame) { ... code ...});
没有自定义标题:
stompClient.connect({}, function(frame) { ... code ...});
在 Javascript 中,您可以使用以下方法提取 STOMP 标头:
username = frame.headers['user-name'];
在服务器端,如果您使用的是 Spring Framework,您可以实现一个拦截器来将 HTTP 参数复制到 WebSockets STOMP 标头。
public class HttpSessionHandshakeInterceptor_personalised implements HandshakeInterceptor {
@Override
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());
// ============================================= CODIGO PERSONAL
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
HttpServletRequest httpServletRequest = servletRequest.getServletRequest();
// httpServletRequest.getCookies();
// httpServletRequest.getParameter("inquiryId");
// httpServletRequest.getRemoteUser();
String token = httpServletRequest.getParameter("token");
...
}
}
对于没有 STOMP 参数的发送消息:
function sendMessage() {
var from = document.getElementById('from').value;
var text = document.getElementById('text').value;
stompClient.send("/app/chatchannel", {},
JSON.stringify({'from':from, 'text':text}));
}
在这里,您将参数传递到 STOMP 标头中。
function sendMessage() {
var from = document.getElementById('from').value;
var text = document.getElementById('text').value;
stompClient.send("/app/chatchannel", {'token':'AA123'},
JSON.stringify({'from':from, 'text':text}));
}
如果您在服务器上使用 Spring boot,请在方法中使用@Header(name = "token")
注释。
用法 -
@Controller
public class SocketController {
static final String token = "1234";
@MessageMapping("/send")
@SendTo("/receive/changes")
public Object notify(MessageModel message, @Header(name = "token") String header)throws Exception {
if(!header.equals(token)) {
// return when headers do not match
return("Unauthorized");
}
// return the model object with associated sent message
return new MessageModel(message.getMessage());
}
}
您应该有一个MessageModel
类,其中包含message
变量和必需的getters
、 setters
和contructor
。
在前端使用 Stomp
用法 -
function sendMessage() {
var text = document.getElementById('text').value;
stompClient.send("/send/message", {'token':'1234'},
JSON.stringify({'message':text}));
}
为了增加更多的安全性,你可以在 Spring 中使用 CORS
来自http://jmesnil.net/stomp-websocket/doc/
stompClient.connect(headers,connectCallback,errorCallback);
其中header是包含自定义HTTP标头的地图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.