简体   繁体   English

如何将不记名令牌传递给 Postman 中的原始 websocket

[英]How to pass bearer token to raw websocket in Postman

I am trying to build a chat application with django channels, however, can't figure a way to pass bearer token我正在尝试使用 django 频道构建聊天应用程序,但是无法找到传递不记名令牌的方法

I am trying to reach the following url: ws://localhost:8000/ws/chat/1/我正在尝试访问以下网址:ws://localhost:8000/ws/chat/1/

I am familiar with adding request headers to HTTP requests, have tried using similar approaches (header Authorization as a key, and Bearer token as a value), tried passing the token as a query param (tried auth=token and token=token), tried passing 40{"token":token} to the message.我熟悉将请求标头添加到 HTTP 请求,尝试使用类似的方法(标头授权作为键,承载令牌作为值),尝试将令牌作为查询参数传递(尝试 auth=token 和 token=token),尝试将 40{"token":token} 传递给消息。 Nothing seemed to work.似乎没有任何效果。 But maybe I am doing something wrong?但也许我做错了什么?

You need to make your own middleware, I'm using this one every time I need to use jwt with channels您需要制作自己的中间件,每次我需要将 jwt 与通道一起使用时,我都在使用这个

from django.db import close_old_connections
from rest_framework_simplejwt.tokens import UntypedToken
from rest_framework_simplejwt.exceptions import InvalidToken, TokenError
from jwt import decode as jwt_decode
from django.conf import settings
from django.contrib.auth import get_user_model
from channels.db import database_sync_to_async
from channels.middleware import BaseMiddleware
from django.contrib.auth.models import AnonymousUser
@database_sync_to_async
def get_user(validated_token):
    try:
        user = get_user_model().objects.get(id=validated_token["user_id"])
        return user
   
    except User.DoesNotExist:
        return AnonymousUser()

 
class TokenAuthMiddleware():
    def __init__(self, inner):
        self.inner = inner
    async def __call__(self, scope, receive, send, *args, **kwargs):
        close_old_connections()
        token = dict(scope)['path'].split("token=")[1]
        try:
            UntypedToken(token)
        except (InvalidToken, TokenError) as e:
            return None
        else:
            decoded_data = jwt_decode(token, settings.SECRET_KEY, algorithms=["HS256"])
            user = await get_user(decoded_data)
            
        return await self.inner(dict(scope, user=user), receive, send, *args, **kwargs)

And here's the path example这是路径示例

path('user/token=<str:token>', ChatConsumer.as_asgi()), Or you can set the token in cookies path('user/token=<str:token>', ChatConsumer.as_asgi()),或者你可以在cookies中设置token

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

相关问题 如何在 url 中传递不记名令牌 - How to pass bearer token in url 如何在没有 postman 的情况下传递令牌 - How to pass a token without postman 如何将不记名令牌从 postman 读取到 Python 代码中? - How to read a bearer token from postman into Python code? 如何在 python api 中传递不记名令牌 - how to pass bearer token in python api 无法将不记名 api 令牌键入 Postman Santau.io api - Cannot key in bearer api token into Postman Santau.io api 如何将接收到的(承载)访问令牌传递给生成的 REST 客户端以调用安全的 API-Gateway 端点 - How to pass received (bearer) access token to generated REST Client in order to invoke secured API-Gateway Endpoint 如何使用 python lambda function 通过 Z8A5DA52ED1206747D8AAZGatewayA70 传递和读取授权不记名令牌? - How to pass and read authorization bearer-token using python lambda function through api gateway? 如何使用python Websocket-client lib传递令牌以与Websocket API连接 - How to pass token to get connected with Websocket API using python Websocket-client lib 如何使用 python 以编程方式获取 GCP Bearer 令牌 - How to get a GCP Bearer token programmatically with python 如何从 Flask 返回不记名 JWT 令牌? - How to return a bearer JWT token FROM Flask?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM