title: How to pass username and password at the time of handshaking to authenticate use in websocket?
body: <p>i am devloping a group chat appication and i want to send username and password at the time of handshaking. </p>
  
  <p>Here is my client side javascript code:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      var Chat = {};
  
      Chat.socket = null;
  
      Chat.connect = (function(host) {
          if ('WebSocket' in window) {
              Chat.socket = new WebSocket(host);
          } else if ('MozWebSocket' in window) {
              Chat.socket = new MozWebSocket(host);
          } else {
              alert("'Error: WebSocket is not supported by this browser.'")
              Console.log('Error: WebSocket is not supported by this browser.');
              return;
          }
  
          Chat.socket.onopen = function() {
              Console.log('Info: WebSocket connection opened.');
              document.getElementById('chat').onkeydown = function(event) {
                  if (event.keyCode == 13) {
                      Chat.sendMessage();
                  }
              };
          };
  
          Chat.socket.onclose = function() {
              document.getElementById('chat').onkeydown = null;
              Console.log('Info: WebSocket closed.');
          };
  
          Chat.socket.onmessage = function(message) {
              Console.log(message.data);
          };
  
      });
  
      Chat.initialize = function() {
          if (window.location.protocol == 'http:') {
              Chat.connect('ws://' + window.location.host
                      + '/WebSocketDemo/ChatWebSocketServlet');
          } else {
              Chat.connect('wss://' + window.location.host
                      + '/WebSocketDemo/ChatWebSocketServlet');
          }
      };
  
      Chat.sendMessage = (function() {
          var message = document.getElementById('chat').value;
          if (message != '') {
              Chat.socket.send(message);
              document.getElementById('chat').value = '';
          }
      });
  
      var Console = {};
  
      Console.log = (function(message) {
          var console = document.getElementById('console');
          var p = document.createElement('p');
          p.style.wordWrap = 'break-word';
          p.innerHTML = message;
          console.appendChild(p);
          while (console.childNodes.length &gt; 25) {
              console.removeChild(console.firstChild);
          }
          console.scrollTop = console.scrollHeight;
      });
  
      Chat.initialize();
  &lt;/script&gt;
  </code></pre>
  
  <p>and i have used tomcat 7 as a server. </p>
  
  <p>Here is my server side code:</p>
  
  <pre><code>@Deprecated
  public class ChatWebSocketServlet extends WebSocketServlet {
  
      private static final long serialVersionUID = 1L;
  
      private static final String GUEST_PREFIX = "Guest";
  
      private final AtomicInteger connectionIds = new AtomicInteger(0);
      private final Set&lt;ChatMessageInbound&gt; connections = new CopyOnWriteArraySet&lt;ChatMessageInbound&gt;();
  
      @Override
      protected StreamInbound createWebSocketInbound(String subProtocol,
              HttpServletRequest request) {
          return new ChatMessageInbound(connectionIds.incrementAndGet());
      }
  
      private final class ChatMessageInbound extends MessageInbound {
  
          private final String nickname;
  
          private ChatMessageInbound(int id) {
              System.out.println("stage 1");
              this.nickname = GUEST_PREFIX + id;
          }
  
          @Override
          protected void onOpen(WsOutbound outbound) {
              connections.add(this);
              System.out.println("user:" + this);
              String message = String.format("* %s %s", nickname, "has joined.");
              broadcast(message);
          }
  
          @Override
          protected void onClose(int status) {
              connections.remove(this);
              String message = String.format("* %s %s", nickname,
                      "has disconnected.");
              broadcast(message);
          }
  
          @Override
          protected void onBinaryMessage(ByteBuffer message) throws IOException {
              throw new UnsupportedOperationException(
                      "Binary message not supported.");
          }
  
          @Override
          protected void onTextMessage(CharBuffer message) throws IOException {
              // Never trust the client
              String filteredMessage = String.format("%s: %s", nickname,
                      HTMLFilter.filter(message.toString()));
  
              broadcast(filteredMessage);
          }
  
          private void broadcast(String message) {
              for (ChatMessageInbound connection : connections) {
                  try {
                      CharBuffer buffer = CharBuffer.wrap(message);
                      connection.getWsOutbound().writeTextMessage(buffer);
                  } catch (IOException ignore) {
                      // Ignore
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Here i want to send User's credential before actual handshaking to authenticate user.
  so how i can to in this?</p>