【问题标题】:Can I mix sessions auth and token auth in one site?我可以在一个站点中混合使用会话身份验证和令牌身份验证吗?
【发布时间】:2014-07-21 14:43:29
【问题描述】:
我有使用会话身份验证的 django 应用程序。我需要添加 API 部分。此 API 将仅由我的 app.users 使用(网络浏览器和移动设备也是如此)。我更喜欢对 API 使用令牌身份验证,因为它看起来更健壮。我发现rest_framework_jwt 可以处理它。
我的问题:我可以在一个站点中混合使用 Web 的会话身份验证和 API 的令牌身份验证而不会出现问题吗?
我认为 Web 应用程序和 API 应用程序是两个不同的应用程序。所以我想在我的项目中将它们分开,使用不同的子域并为每个使用不同类型的身份验证。是否可以按子域分隔身份验证?我想在用户登录网络应用程序时发送令牌。这是个好主意吗?
【问题讨论】:
标签:
django
authentication
django-rest-framework
jwt
【解决方案1】:
正如您在documentation 中看到的,您可以毫无问题地配置多个身份验证后端。 DRF 将尝试每个后端,直到一个说“好的”。
要记住的一点:
如果您(例如)提供了无效的 JSON-Web-Token,则身份验证将立即失败,并且不会尝试其他后端。很高兴看到source of rest_framework_jwt。
def authenticate(self, request):
"""
Returns a two-tuple of `User` and token if a valid signature has been
supplied using JWT-based authentication. Otherwise returns `None`.
"""
auth = get_authorization_header(request).split()
if not auth or auth[0].lower() != b'jwt':
return None
if len(auth) == 1:
msg = 'Invalid JWT header. No credentials provided.'
raise exceptions.AuthenticationFailed(msg)
elif len(auth) > 2:
msg = ('Invalid JWT header. Credentials string '
'should not contain spaces.')
raise exceptions.AuthenticationFailed(msg)
try:
payload = jwt_decode_handler(auth[1])
except jwt.ExpiredSignature:
msg = 'Signature has expired.'
raise exceptions.AuthenticationFailed(msg)
except jwt.DecodeError:
msg = 'Error decoding signature.'
raise exceptions.AuthenticationFailed(msg)
user = self.authenticate_credentials(payload)
return (user, auth[1])
-
return None 表示后端在说:“这不是 JWT,让其他人试试
-
raise exceptions.AuthenticationFailed(msg) 表示:“用户尝试了 JWT,但他失败了。”
回答更多问题: