django sso客户端应用程序,与dj sso服务器一起工作
dj-sso-client2的Python项目详细描述
dj-sso-client是django应用程序作为dj-sso-server(https://github.com/feifangit/dj-sso-server)的sso客户端工作
安装
pip install dj-sso-client
添加到项目中
在项目的settings.py中修改以下设置
- ^{tt5}$, add ^{tt6}$ as the backends
- ^{tt7}$, set ^{tt8}$ as user model
AUTHENTICATION_BACKENDS=('djssoclient.authbackend.SSOAuthBackend',)AUTH_USER_MODEL='djssoclient.SSOUser'
根据需要添加以下dj-sso-client设置
^{tt10}$: set API key, SEC key and remote SSO provider URL. This setting is used by underneath ^{tt11}$ module to proejct API accessing.
SSO_API_AUTH_SETTING={"apikey":"f4a05287","seckey":"6a4eeaea54d54f51af703e79c6096d51","url":"https://dj-sso-sample.herokuapp.com",}
SSO_REMOTE_URL_PREFIX(可选):远程SSO提供程序中的SSO路径。默认值/sso/
SSO_USER_STORAGE``(optional): SSOUser storage solution, there are 2 storage backends in ``dj-sso-client已经。默认值:ssouserdbstorage
- ^{tt15}$: store user data in database
- ^{tt16}$: store user data in cache. You will get better performance.
SSO_SETTING_CACHE(可选):如果选择SSOUserCacheStorage作为用户存储后端,并且在settings.py中有多个缓存,则可以在此处拾取缓存名称。默认值:default
注意:SSOUserCacheStorage
默认的django.core.cache.backends.locmem.LocMemCache存储每个进程的数据。在多进程生产环境(多核服务器上的gunicorn)中,使用SSOUserCacheStorage作为用户存储引擎时可能会出现问题。
请使用专用缓存系统(memcached或redis)作为缓存后端,以避免此问题。
ssouser
SSOUser是存储用户数据的用户模型。如果选择SSOUserDBStorage作为用户存储引擎,则可以将其用作数据库模型类。
classSSOUser(AbstractBaseUser):username=models.CharField(unique=True,max_length=50)extras=models.TextField(default="{}")...
^ {STR 1 }额外的用户属性>:^ {tT24} $类中不存在属性。(除了username、password、last_login等属性)
所有额外的用户属性都可以由getattr方法或.运算符访问。它们以json格式存储在类成员extras中。
把手弄脏
我们已经有一个sso提供程序(dj-sso-server)应用程序在heroku:http://dj-sso-sample.herokuapp.com/上运行。在本地文件夹example/ssoclient/中运行示例应用程序。
示例应用程序中使用的api键与localhost:8000绑定,因此请确保通过localhost:8000而不是127.0.0.1:8000访问本地应用程序。
fresh登录
- 确保您没有登录http://dj-sso-sample.herokuapp.com/,您应该会看到please log in with ...
- 单击本地应用程序上的sso login,将重定向到http://dj-sso-sample.herokuapp.com/sso/…。
- 重定向到本地应用程序后,您将看到用户信息。
- 在http://dj-sso-sample.herokuapp.com/上,您的状态仍然未登录
^ {STR 1 }登录现有登录帐户< /强>
- 使用用户名user1和密码123登录http://dj-sso-sample.herokuapp.com/
- 单击本地应用程序上的sso login,您将被重定向到http://dj-sso-sample.herokuapp.com/sso/….,但您将看到另一个新登录的登录页面。
- 选择continue with current user? user1
- 您将以user1身份登录本地应用程序
切换帐户
- 如果您选择switch account,并使用user2/456从上一个示例的步骤3登录
- 您将以user2身份登录本地应用程序
- 您在http://dj-sso-sample.herokuapp.com/上的登录状态将不更改(仍以user1登录)
待办事项
- 示例:作为额外的auth方法工作