Django - 没有模型的登录用户
Django - Login User without model
我正在尝试实施 SSO 登录,从 saml 响应中获取所有授权权限:
class SAMLServiceProviderBackend(object):
def authenticate(self, saml_authentication=None):
if not saml_authentication: # Using another authentication method
return None
if saml_authentication.is_authenticated():
attributes = saml_authentication.get_attributes()
user = User(username=saml_authentication.get_nameid())
user.first_name = attributes['givenName']
user.last_name = attributes['sn']
return None
在浏览量中我得到了类似
的内容
...
user = authenticate(saml_authentication=auth)
login(self.request, user)
...
login
由于缺少 save()
方法而失败。唯一的方法是继承 User
并覆盖 save
方法。试试这个,我得到了 is_authenticated
、get_and_delete_messages
等下一个错误
有没有一种简单的方法可以将用户对象插入到会话中,而无需将用户保存到数据库中?
类似于:
request.session['user'] = authenticate(saml_authentication=auth)
我想应该有一些限制,例如。您不能将您的数据保存为 FK 用户。
我自己试过了,我怀疑你可以在 authenticate()
方法中动态创建一个用户实例,只是不要调用 user.save()
,或者覆盖 save()
方法什么都不做。
您可能还需要在请求之间连接用户记录,因此您可能需要为用户编写自己的序列化程序并从会话中加载构建用户实例。
我相当确定身份验证后端需要 Django 的会话对象。即,如果您登录,则 Django 需要在某处存储对此事实的引用。 Django 通常使用它的数据库来执行此操作,但是,如果您没有在应用程序中使用数据库,那么您可以查看缓存会话:
https://docs.djangoproject.com/en/1.11/topics/http/sessions/#using-cached-sessions
(根据您的问题,我假设您没有使用数据库)
然而,更重要的是,根据您的需要,您可能需要考虑创建/配置自定义用户模型以使您的后端正常工作:
https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#substituting-a-custom-user-model
我正在尝试实施 SSO 登录,从 saml 响应中获取所有授权权限:
class SAMLServiceProviderBackend(object):
def authenticate(self, saml_authentication=None):
if not saml_authentication: # Using another authentication method
return None
if saml_authentication.is_authenticated():
attributes = saml_authentication.get_attributes()
user = User(username=saml_authentication.get_nameid())
user.first_name = attributes['givenName']
user.last_name = attributes['sn']
return None
在浏览量中我得到了类似
的内容...
user = authenticate(saml_authentication=auth)
login(self.request, user)
...
login
由于缺少 save()
方法而失败。唯一的方法是继承 User
并覆盖 save
方法。试试这个,我得到了 is_authenticated
、get_and_delete_messages
等下一个错误
有没有一种简单的方法可以将用户对象插入到会话中,而无需将用户保存到数据库中?
类似于:
request.session['user'] = authenticate(saml_authentication=auth)
我想应该有一些限制,例如。您不能将您的数据保存为 FK 用户。
我自己试过了,我怀疑你可以在 authenticate()
方法中动态创建一个用户实例,只是不要调用 user.save()
,或者覆盖 save()
方法什么都不做。
您可能还需要在请求之间连接用户记录,因此您可能需要为用户编写自己的序列化程序并从会话中加载构建用户实例。
我相当确定身份验证后端需要 Django 的会话对象。即,如果您登录,则 Django 需要在某处存储对此事实的引用。 Django 通常使用它的数据库来执行此操作,但是,如果您没有在应用程序中使用数据库,那么您可以查看缓存会话:
https://docs.djangoproject.com/en/1.11/topics/http/sessions/#using-cached-sessions
(根据您的问题,我假设您没有使用数据库)
然而,更重要的是,根据您的需要,您可能需要考虑创建/配置自定义用户模型以使您的后端正常工作:
https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#substituting-a-custom-user-model