django-sso单点登陆的实现
Posted rohero
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django-sso单点登陆的实现相关的知识,希望对你有一定的参考价值。
环境准备
环境规格:
python3.5
django2.0
django-simple-sso-0.14
环境安装:
pip install django-simple-sso-0.14
环境说明:
opstest01为django应用,作为服务端使用,端口8001
testsso为django应用,作为客户端使用,端口8000
单点概念
参考: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
觉得有帮助就点亮小星星吧
以上是关于django-sso单点登陆的实现的主要内容,如果未能解决你的问题,请参考以下文章