環境準備html
環境規格:python
python3.5git
django2.0github
django-simple-sso-0.14django
環境安裝:json
pip install django-simple-sso-0.14cookie
環境說明:session
opstest01爲django應用,做爲服務端使用,端口8001測試
testsso爲django應用,做爲客戶端使用,端口8000ui
單點概念
參考:https://www.cnblogs.com/morethink/p/8047711.html
服務端實現(opstest01)
配置修改:
修改setting.py文件
INSTALLED_APPS中添加'simple_sso.sso_server',
執行python manage.py makemigrations, python manage.py migrate
添加表數據
sso_server_consumer表中添加一行數據指定私鑰公鑰,用於客服端的配置使用
編輯urls.py
暴露兩個訪問地址
1 from django.contrib import admin
2 from django.urls import path, include 3 from django.conf.urls import url 4 from django.contrib.auth.views import LoginView 5 6 #sso 7 from simple_sso.sso_server.server import Server 8 test_server = Server() 9 10 urlpatterns = [ 11 path('admin/', admin.site.urls), 12 url('^server/', include(test_server.get_urls())), 13 url(r'^login/$', LoginView.as_view(template_name='admin/login.html'), name="login"), 14 ]
server爲服務端認證,獲取token,驗證地址;login爲django系統自帶登錄地址做測試用(測試用戶經過python manage.py createsuperuser建立)
處處服務端建立完成!
客戶端實現
建立django項目testsso作爲客戶端測試使用
編輯setting.py文件
INSTALLED_APPS中添加'simple_sso.sso_client',
結尾處添加sso相關參數
#sso
SSO_SERVER = 'http://127.0.0.1:8001/server/'
SSO_PUBLIC_KEY = 'public'
SSO_PRIVATE_KEY = 'private'
LOGIN_URL = '/testsso/client/' #login_required未登錄跳轉地址
添加views.py
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required # Create your views here. @login_required def testClientSSO(request): json_data = {'name': 'nalanxiao', 'id': 0} return JsonResponse(json_data)
login_require爲django系統裝飾器,判斷登錄狀態
testClientSSO爲測試頁面
編輯urls.py
from django.urls import include
from django.conf.urls import url from simple_sso.sso_client.client import Client from django.conf import settings from . import views test_client = Client(settings.SSO_SERVER, settings.SSO_PUBLIC_KEY, settings.SSO_PRIVATE_KEY) urlpatterns = [ url(r'^client/', include(test_client.get_urls())), url(r'test/', views.testClientSSO, name='testSSO'), ]
添加client,test兩個訪問地址
測試
訪問http://127.0.0.1/8000/client/test/頁面調整到8001/login/頁面
登錄完成後跳轉回test頁面:
cookie中增長sessionid
gihub:https://github.com/Roherolxh/django-sso
以爲有幫助就點亮小星星吧