英文:
API Call in Websocket Handler
问题
我需要检查用户在请求WebSocket连接时是否提供了有效的令牌。为了进行验证,需要调用验证服务器。我该如何实现同步,以便WebSocket处理程序在未收到答复之前不会结束。我正在使用vertx框架。
server.websocketHandler(ctx -> {
String clientId = ctx.binaryHandlerID();
oAuth2WebSocket.authenticate(new JsonObject().put("access_token", ctx.query())
.put("token_type", "Bearer"), authHndlr -> authenticateTokenOverWebSocket(authHndlr, ctx, clientId));
});
英文:
I need to check if a user delivers a valid token while he request a websocket connection. For the validation a call verification server is necessary. How can I implement it synchronous so that the websocket handler does not end before there was an answer. I am using the vertx framework.
server.websocketHandler(ctx -> {
String clientId = ctx.binaryHandlerID();
oAuth2WebSocket.authenticate(new JsonObject().put("access_token", ctx.query())
.put("token_type", "Bearer"), authHndlr -> authenticateTokenOverWebSocket(authHndlr, ctx, clientId));
});
答案1
得分: 0
一种方法是在ServerEndpoint上配置一个Configurator。实现modifyHandshake方法并在其中检查令牌。在这个方法中,可以修改标头,并指示用户是否没有令牌。
另一种方法是在同一个方法(modifyHandshake)中,在从ServerEndpointConfig.getUserProperties()获取的映射中设置一个变量,并在处理程序的实现中(例如,Whole)检查该变量并作出相应的响应。
英文:
One approach is to configure a Configurator on the ServerEndpoint. Implement the modifyHandshake method and there check for the token. In this method it is possible to modify the headers and indicate if the user does not have the token.
In another approach, in the same method (modifyHandshake), set a variable in the Map obtained from ServerEndpointConfig.getUserProperties() and in the implementation of the handler (Whole for example) to check the variable and respond accordingly.
专注分享java语言的经验与见解,让所有开发者获益!
评论