admin管理员组文章数量:1125553
Frontend webconnection issue `Backend log Entering JWTAuthFilter for request URI: /ws 2025-01-09T13:16:24.631+05:30 WARN 123068 --- [demo] [nio-8081-exec-1] com.example.demo.Config.JWTAuthFilter : Authorization header is missing.
Issue description: The JWT token header is missing. Even though the token is passed via the WebSocket connection URL, the backend log shows that the Authorization header is missing. As a result, the WebSocket connection is closing. Additionally, when attempting to establish the next WebSocket connection without passing the token, you are encountering CORS issues and the authentication filter is expecting the token to be passed.`
frontend
let socket;
export const connectWebSocket = (onMessageCallback) => {
const token = localStorage.getItem("jwtToken");
console.log("Token............." + token);
if (!token) {
console.error("No JWT token found.");
return;
}
// Pass token as a query parameter in the URL
socket = new WebSocket(`ws://localhost:8081/ws?token=${token}`);
socket.onmessage = (event) => {
if (onMessageCallback) {
onMessageCallback(event.data);
}
};
socket.onerror = (error) => {
console.error("WebSocket Error: ", error);
};
socket.onclose = (event) => {
if (event.code === 1008) {
// Specific code for policy violation (e.g., invalid JWT token)
console.error("Connection closed due to invalid token or other policy violations.");
} else {
console.log("WebSocket connection closed. Code: " + event.code);
}
// Reconnect attempt after 5 seconds
console.log("Attempting to reconnect...");
setTimeout(() => connectWebSocket(onMessageCallback), 5000);
};
};
本文标签:
版权声明:本文标题:reactjs - WebSocket connection closed. Code: 1006 (Authentication Header missing or invalid) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736664056a1946566.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论