我正在尝试运行简单的Django应用程序。我已经安装了简单的sso模块在我的本地应用程序。我试过了,但没有解决。下面的图片告诉我们,当我运行localserver时发生了错误。
在这里输入图像描述输入图片描述
下面的代码是一些"settings.py":
INSTALLED_APPS = [
'ssoLogin.apps.ssoLoginConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'simple_sso.sso_server',
]
ROOT_URLCONF = 'ssoLogin.urls'
# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
SSO_PRIVATE_KEY='ttVCo7bewsATjEPS7CNHd4tIk1ayyBKb1qbPk5DxWZiK4pvyLZQtnpinjPh6fWr3'
SSO_PUBLIC_KEY='hGtAakAg7Sh3SffSs2obwAAflMuzbbLBUJUHpk6WrFnxhA9b78EHNoTOr0DsDho3'
SSO_SERVER='http://127.0.0.1:8000/server'
我用了"simple-sso"模块根据文档正确。我认为所有的配置(settings.py)是好的,但我不知道url和视图是正确的。总之,这是我在urls.py中的配置。
代码:
from django.contrib import admin
from django.urls import path, include, re_path
from simple_sso.sso_server.server import Server
from simple_sso.sso_client.client import Client
from django.conf import settings
test_client = Client(settings.SSO_SERVER, settings.SSO_PUBLIC_KEY, settings.SSO_PRIVATE_KEY)
test_server = Server()
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
re_path(r'^server/', include(test_server.get_urls())),
re_path(r'^client/', include(test_client.get_urls())),
]
正确导入服务器和客户端对象。我认为客户端工作良好,但我不知道服务器工作。也许服务器不能正常工作。需要解决这个问题
- 第一条错误信息
SSO_SERVER需要一个斜杠:
SSO_SERVER='http://127.0.0.1:8000/server/'
- 后续错误信息(来自下面的注释):
根本原因是服务器和客户端共存于一个应用程序中。
当你请求/client/时,会有一个获取令牌的请求:
(见https://github.com/divio/django-simple-sso/blob/master/simple_sso/sso_client/client.py)
class LoginView(View):
...
def get(self, request):
...
request_token = self.client.get_request_token(redirect_to)
...
将从
构建一个urlSSO_SERVER='http://127.0.0.1:8000/server/' + reverse('simple-sso-request-token')
因为您的服务器url中有服务器,所以reverse('simple-sso-request-token')返回"/server/request-token/">
合在一起就得到
http://127.0.0.1:8000/server/server/request-token/
我不确定路径名是否"simple-sso-request-token";在其他地方是需要的——如果不需要,您只需"覆盖"即可。它通过在urls.py中添加一行虚拟视图来改变相反的结果:
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
re_path(r'^server/', include(test_server.get_urls())),
re_path(r'^client/', include(test_client.get_urls())),
path("request-token/", some_dummy_view , name='simple-sso-request-token'), # this is only to force reverse("simple-sso-request-token") to return "request-token/"
]
(reverse()返回urlpatterns列表中的最后一个路径)
这将导致
http://127.0.0.1:8000/server/request-token/
希望没有更多的错误信息