django-sso单点登录的实现

环境准备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   

以为有帮助就点亮小星星吧

相关文章
相关标签/搜索