我有一个简单的聊天,我使用频道 2。在用户连接到消费者之前,我在通过令牌 JWT(简单 JWT)授权时遇到问题。
我的中间件:
from channels.auth import AuthMiddlewareStack
from rest_framework.authtoken.models import Token
from django.contrib.auth.models import AnonymousUser
class TokenAuthMiddleware:
"""
Token authorization middleware for Django Channels 2
"""
def __init__(self, inner):
self.inner = inner
def __call__(self, scope):
headers = dict(scope['headers'])
if b'authorization' in headers:
try:
token_name, token_key = headers[b'authorization'].decode().split()
if token_name == 'Token':
token = Token.objects.get(key=token_key)
scope['user'] = token.user
except Token.DoesNotExist:
scope['user'] = AnonymousUser()
return self.inner(scope)
TokenAuthMiddlewareStack = lambda inner: TokenAuthMiddleware(AuthMiddlewareStack(inner))
我的消费者的片段:
User = get_user_model()
class ChatConsumer(WebsocketConsumer):
def connect(self):
"""
Join channel group by chatname.
"""
self.group_name = 'chat_{0}'.format(self.scope['url_route']['kwargs']['chatname'])
if self.scope['user'] == AnonymousUser():
raise DenyConnection("Invalid User")
async_to_sync(self.channel_layer.group_add)(
self.group_name,
self.channel_name,
)
self.accept()
def disconnect(self, close_code):
"""
Leave channel by group name.
"""
async_to_sync(self.channel_layer.group_discard)(
self.group_name,
self.channel_name
)
我的路由:
from .auth import TokenAuthMiddlewareStack
application = ProtocolTypeRouter({
'websocket': TokenAuthMiddlewareStack(
URLRouter([
path('ws/chat/<slug:chatname>/', ChatConsumer),
])
),
})
现在有两个问题。
如何在我的测试客户端 Web 套接字中传递令牌?上面的片段是正确的吗?因为我从客户那里收到
raise InvalidStatusCode(status_code)
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 403
我的测试客户:
from asgiref.sync import async_to_sync
import websockets
import asyncio
import json
test_json = "some test to send via socket"
def test_url2(url):
async def inner():
async with websockets.connect(url) as websocket:
await websocket.send(test_json)
print(test_json)
return asyncio.get_event_loop().run_until_complete(inner())
test_url2("ws://0.0.0.0:8080/ws/over/3ff621b1-b392-4a82-ab35-a0ad81ab1179_f99c82ba-1404-442c-ba08-653f84d58aa4")
谢谢您的回复