简体   繁体   English

如何通过WSO2(API管理器)将自定义授权标头传递到我的后端Websocket服务?

[英]How to pass Custom Authorization Header to my backend websocket service through WSO2 (API Manager)?

I am using a Websocket api in WSO2. 我在WSO2中使用了Websocket api。 I need to pass custom header to my backend Websocket service. 我需要将自定义标头传递给后端Websocket服务。 I have found a documentation here https://docs.wso2.com/display/AM210/Adding+Mediation+Extensions 我在这里找到了一个文档https://docs.wso2.com/display/AM210/Adding+Mediation+Extensions

but it's for rest api call not for Websocket. 但这是针对其余api调用而不是针对Websocket。 So how can i send custom header to my backend Websocket service through WSO2. 因此,如何通过WSO2将自定义标头发送到后端Websocket服务。

You can send a custom header to the Web Socket backend in the initial WebSocket handshake. 您可以在初始WebSocket握手中将自定义标头发送到Web Socket后端。 you can set it in the following format to the client handshake request. 您可以按照以下格式将其设置为客户端握手请求。

websocket.custom.header.<required-header-name> 

Ex: If the expected header is X-JWT-Assertion, the header that should be sent is 例如:如果预期的标头是X-JWT-Assertion,则应发送的标头是

websocket.custom.header.X-JWT-Assertion

This feature support is added from API Manager v2.6.0 此功能支持是从API Manager v2.6.0添加的

You could not use mediation sequences here since the rest of the communication is done with ws frames. 您无法在此处使用中介序列,因为其余的通信都是通过ws帧完成的。

Adding more information. 添加更多信息。

Here is an example of a netty based web socket client which can be used to communicate with the WS API deployed in the API Manager. 这是一个基于netty的Web套接字客户端的示例,可用于与API Manager中部署的WS API通信。 [1] [1]

The authorization header is set in the Handshake as follows. 授权标头在握手中设置如下。

final WebSocketClientHandler handler = new WebSocketClientHandler(
                    WebSocketClientHandshakerFactory.newHandshaker(
                            uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders()
                    .add("Authorization", "Bearer e2238f3a-e43c-3f54-a05a-dd2e4bd4631f")));

This Authorization header is to authenticate with API Manager. 该授权标头用于通过API Manager进行身份验证。 If you need to send a custom header, you can add another header by modifying the above example as follows. 如果需要发送自定义标头,则可以通过如下修改上述示例来添加另一个标头。

DefaultHttpHeaders headers = new DefaultHttpHeaders();
headers.add("Authorization", "Bearer e2238f3a-e43c-3f54-a05a-dd2e4bd4631f");
headers.add("websocket.custom.header.X-WS-UserName", "bob");

final WebSocketClientHandler handler = new WebSocketClientHandler(
                    WebSocketClientHandshakerFactory.newHandshaker(
                            uri, WebSocketVersion.V13, null, false, headers));

This header will be sent to the backend as, 该标头将以以下方式发送到后端:

X-WS-UserName : bob

[1] https://docs.wso2.com/download/attachments/57748790/sample-ws-client.zip?version=2&modificationDate=1484568275000&api=v2 [1] https://docs.wso2.com/download/attachments/57748790/sample-ws-client.zip?version=2&modificationDate=1484568275000&api=v2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM