当前位置:网站首页>websocket-js连接如何携带token验证
websocket-js连接如何携带token验证
2022-06-28 23:57:00 【hay_lee】
websocket协议在握手阶段借用了HTTP的协议,但是在JavaScript websocketAPI中并没有修改请求头的方法。
var token='eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzdWFkbWluIiwiY3JlYXRlZCI6MTY1NjI5ODQxMjkwMSwiYXV0aG9yaXRpZXMiOlt7ImF1dGhvcml0eSI6ImFwcEFsYXJtIn0seyJhdXRob3JpdHkiOiJyZXNvdXJjZU1vbml0b3IifSx7ImF1dGhvcml0eSI6InJlc291cmNlIn0seyJhdXRob3JpdHkiOiJzeXM6dXNlciJ9LHsiYXV0aG9yaXR5IjoiZGV0VGFzayJ9LHsiYXV0aG9yaXR5IjoicmVvdXJjZTpjb25maWcifSx7ImF1dGhvcml0eSI6ImhvbWUifSx7ImF1dGhvcml0eSI6InJlb3VyY2U6YXVkaXQifSx7ImF1dGhvcml0eSI6InJlb3VyY2U6bW9uaXRvciJ9LHsiYXV0aG9yaXR5IjoiYXBwQW5hbHlzaXMifSx7ImF1dGhvcml0eSI6ImRpc3Bvc2FsU2NoZW1lIn0seyJhdXRob3JpdHkiOiJhcHBNYW5hZ2UifSx7ImF1dGhvcml0eSI6InJlb3VyY2U6YWxhcm0ifSx7ImF1dGhvcml0eSI6InN5czpyb2xlIn0seyJhdXRob3JpdHkiOiJyZW91cmNlOnVybENvbmZpZyJ9XSwiZXhwIjoxNjU2MzAyMDEyfQ.5Sh9wzMtsnsncbDyrzaC64e5RFN6e-_p-nbkaL0BQv2zkfmiALN84MoY0IMkvXUYnw9_HQe8KS8azyGXLNrVdg'send发送数据
var ws = new WebSocket("ws://" + url + "/webSocketServer");
ws.onopen=function(){
ws.send(token)
}1.请求地址中带参数
var ws = new WebSocket("ws://" + url?token + "/webSocketServer");2.基于协议头
websocket请求头中可以包含Sec-WebSocket-Protocol这个属性,该属性是一个自定义的子协议。它从客户端发送到服务器并返回从服务器到客户端确认子协议。我们可以利用这个属性添加token。
var ws = new WebSocket("ws://" + url+ "/webSocketServer",[token]);
如果基于协议头传递了参数,后端响应的时候header也需要带上协议头参数。
public static Session connection(String uri){
Session session = null;
try {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig cec = ClientEndpointConfig.Builder
.create().configurator(new ClientEndpointConfig.Configurator() {
public void beforeRequest(Map<String, List<String>> headers) {
headers.put("token", token);
}
}).build();
session = container.connectToServer(ClientSocket.class, cec, URI.create(uri));
} catch (Exception e) {
e.printStackTrace();
}
return session;
}或者写filter过滤websocket请求加相应参数
边栏推荐
- [SSM] an error is reported that the user name of the access denied for user 'WYF' @ 'localhost' (using password: yes) data becomes the user name of the computer
- Rongyun communication solution solves the pain points of enterprise communication
- 好用免费的PPT模板
- ctfshow XSS
- urllib.parse 解析url连接中的参数
- [buuctf.reverse] 131-135
- 【LeetCode】21. 合并两个有序链表 - Go 语言题解
- Reading notes of English grammar new thinking Basic Edition 2 (I)
- LinkedIn datahub - experience sharing
- Picture 64base transcoding and decoding
猜你喜欢

MapReduce案例

Typescript -- Section 2: variable declaration

Summary of software testing cognition

剑指 Offer 12. 矩阵中的路径

stm32F407-------串行(串口)通信

ctfshow XSS

Implementation of dynamic timer for quartz

Software testing tools: complete and precise
![Edge extraction based on Halcon learning [2] circles Hdev routine](/img/e4/e3738d71c2ff5a239a12f67d06e2c9.jpg)
Edge extraction based on Halcon learning [2] circles Hdev routine

Blue Bridge Cup top ten common heaven level skill - breath of water The type of one recursion
随机推荐
Behaviortree in ros2
入行数字IC验证后会做些什么?
I can't sleep
[buuctf.reverse] 131-135
stm32F407-------GPIO输入实验
How many locks are added to an update statement? Take you to understand the underlying principles
TypeScript--第四节:函数
MapReduce case
Stm32f407 ------ running lamp and buzzer
一条update语句到底加了多少锁?带你深入理解底层原理
Typescript -- Section 3: Interface
TypeScript -- 第七节 枚举
What pitfalls should be avoided in the job interview for the operation post in 2022?
力扣(LeetCode)178. 分数排名(2022.06.27)
MNIST handwritten numeral recognition demo based on pytorch framework
"Five considerations" for safe use of the Internet
stm32F407-------通用定时器
Yyds dry goods inventory building knowledge map from scratch with neo4j (I)
自动化测试的生命周期是什么?
Is it safe to open an account for buying stocks online?