摘自Quart
的创建者的中篇文章:https://medium.com/@pgjones/websockets-in-quart-f2067788d1ee:
认证
授权时控制接受的能力最有用请求,因为它允许检查请求标头,或者接受升级或返回401。一个简单的例子是,
def auth_required(func): @wraps(func) async def wrapper(*args, **kwargs): auth = websocket.authorization if ( auth is not None and auth.username == current.app.config['USERNAME'] and compare_digest( auth.password, current.app.config['PASSWORD'], ) ): return await func(*args, **kwargs) else: abort(401) return wrapper @app.websocket('/ws') @auth_required async def ws(): ...
但是客户端javascript如何导致websocket.authorization
属性被填充?
[var ws = new WebSocket('ws://' + 'user' + ':' + 'pass' + '@' + document.domain + ':' + location.port + '/ws');
在最近几年中似乎已被弃用,并且不支持Websocket的HTTP请求中的自定义标头。
谢谢