public static String generateWebSocketAccept(String webSocketKey) {
String keyString = webSocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
byte[] hash = messageDigest.digest(keyString.getBytes(StandardCharsets.UTF_8));
return Base64.encodeToString(hash, Base64.NO_WRAP);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SHA-1 algorithm not found", e);
}
}
参数webSocketKey是客户端请求过来的Sec-WebSocket-Key这个头。
具体的请求和响应的例子如下文章来源:https://www.toymoban.com/news/detail-667930.html
Handshake Details
Request URL: http://127.0.0.1:8080/home/user.websocket
Request Method: GET
Status Code: 101 Switching Protocols
Request Headers
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: VKK/4ExbQre/XX9rB4yDlg==
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Host: 127.0.0.1:8080
Response Headers
Connection: Upgrade
Upgrade: WebSocket
Sec-WebSocket-Accept: iJKFTUb56s8x38R5xL1Bp/roB40=文章来源地址https://www.toymoban.com/news/detail-667930.html
到了这里,关于java生成 Sec-WebSocket-Accept的密钥的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!