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);
  };
};

本文标签: