当前位置:网站首页>How to carry token authentication in websocket JS connection
How to carry token authentication in websocket JS connection
2022-06-29 00:10:00 【hay_ lee】
websocket The protocol was borrowed during the handshake phase HTTP The agreement , But in JavaScript websocketAPI There is no method to modify request header in .
var token='eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzdWFkbWluIiwiY3JlYXRlZCI6MTY1NjI5ODQxMjkwMSwiYXV0aG9yaXRpZXMiOlt7ImF1dGhvcml0eSI6ImFwcEFsYXJtIn0seyJhdXRob3JpdHkiOiJyZXNvdXJjZU1vbml0b3IifSx7ImF1dGhvcml0eSI6InJlc291cmNlIn0seyJhdXRob3JpdHkiOiJzeXM6dXNlciJ9LHsiYXV0aG9yaXR5IjoiZGV0VGFzayJ9LHsiYXV0aG9yaXR5IjoicmVvdXJjZTpjb25maWcifSx7ImF1dGhvcml0eSI6ImhvbWUifSx7ImF1dGhvcml0eSI6InJlb3VyY2U6YXVkaXQifSx7ImF1dGhvcml0eSI6InJlb3VyY2U6bW9uaXRvciJ9LHsiYXV0aG9yaXR5IjoiYXBwQW5hbHlzaXMifSx7ImF1dGhvcml0eSI6ImRpc3Bvc2FsU2NoZW1lIn0seyJhdXRob3JpdHkiOiJhcHBNYW5hZ2UifSx7ImF1dGhvcml0eSI6InJlb3VyY2U6YWxhcm0ifSx7ImF1dGhvcml0eSI6InN5czpyb2xlIn0seyJhdXRob3JpdHkiOiJyZW91cmNlOnVybENvbmZpZyJ9XSwiZXhwIjoxNjU2MzAyMDEyfQ.5Sh9wzMtsnsncbDyrzaC64e5RFN6e-_p-nbkaL0BQv2zkfmiALN84MoY0IMkvXUYnw9_HQe8KS8azyGXLNrVdg'send send data
var ws = new WebSocket("ws://" + url + "/webSocketServer");
ws.onopen=function(){
ws.send(token)
}1. The request address contains parameters
var ws = new WebSocket("ws://" + url?token + "/webSocketServer");2. Based on protocol header
websocket The request header can contain Sec-WebSocket-Protocol This attribute , This attribute is a custom sub protocol . It sends from the client to the server and returns from the server to the client to confirm the sub Protocol . We can use this property to add token.
var ws = new WebSocket("ws://" + url+ "/webSocketServer",[token]);
If parameters are passed based on the protocol header , When the backend responds header You also need to bring the protocol header parameter .
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;
}Or write filter Filter websocket Request to add corresponding parameters
边栏推荐
猜你喜欢

由背景图缓存导致的canvas绘图跨域问题

Stm32f407 ------ serial (serial port) communication

MapReduce案例

Haskell 配置 VS code 开发环境 (2022年6月)

The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (2)

Along with the notes: methods simulating array like classes
How does the JVM bottom layer implement synchronized

【LeetCode】21. Merge two ordered linked lists - go language solution

随笔记:模拟类数组(array-like)的方法

stm32F407-------NVIC中断优先级管理
随机推荐
Basic operation of MySQL database: import hellodb SQL and query as required; Create account and authorize
Online yaml to JSON tool
【软件分析】软件分析、设计与建模迭代式详解
TypeScript -- 第七节 枚举
stm32F407-------寄存器地址名称映射分析
请问指南针股票软件可靠吗?在上面交易股票安全吗?
EditText监听焦点
Give you a project, how will you carry out performance testing (I)
websocket-js连接如何携带token验证
Phoenix安装教程
TypeScript--第五节:类
stm32F407-------外部中断
随笔记:定义setter和getter的三种方式
Differences among VaR, let and Const
TypeScript -- 第二节:变量声明
HandlerThread使用及原理
【C Primer Plus第二章課後編程題】
How to guarantee the delivery quality through the cloud effect test plan
10. Yolo series
Stm32f407 ------- RTC real time clock