如何在django + oauth环境中访问OpenId连接端点?
我试图使用Django (3.2.5) (1.5.0)来设置一个带有OAuth v2 + OpenId连接的django(3.2.5)。我能够跟踪教程,这意味着我有oauth的支持。我能够得到Oauth令牌,并使用它们保护端点。
但是当我尝试配置OpenId连接时,我无法访问 o/.well-known/... 端点,它们根本不是注册的。我得到了一个HTTP404,调试页面显示django只知道o/authorize/、o/token/和o/revoke-token/。OpendId连接段似乎意味着我不需要做任何其他事情,只需要启用OpenId来显示这些视图。
我的urls.py看起来像:
oauth2_endpoint_views = [
path('authorize/', oauth2_views.AuthorizationView.as_view(), name="authorize"),
path('token/', oauth2_views.TokenView.as_view(), name="token"),
path('revoke-token/', oauth2_views.RevokeTokenView.as_view(), name="revoke-token"),
]
urlpatterns = [
path('admin/', admin.site.urls),
re_path('^accounts/', admin.site.urls),
path('o/', include((oauth2_endpoint_views, 'oauth2_provider'), namespace="oauth2_provider")),
path('api/hello', ApiEndpoint.as_view()), # an example protected resource endpoint
path('api/secret', secret_page, name='secret'), # requires authentication
]作为OAuth配置的一部分,我已经
oauth2_provider添加到settings.INSTALLED_APPS中。oauth2_provider.middleware.OAuth2TokenMiddleware添加到settings.MIDDLEWARE中。django.contrib.auth.backends.ModelBackend、oauth2_provider.backends.OAuth2Backend、django.contrib.auth.backends.ModelBackend添加到settings.AUTHENTICATION_BACKENDS中。CORS_ORIGIN_ALLOW_ALL被设置为True。path('o/', include((oauth2_endpoint_views, 'oauth2_provider'), namespace="oauth2_provider"))。而OAuth正如预期的那样工作。
作为OpenId连接i的一部分
OAUTH2_PROVIDER配置添加到settings中。DEBUG = False设置为settings。我没有注册任何额外的urls,因为(i)我不知道该注册什么,(ii)没有迹象表明我应该做任何其他的事情。
发布于 2021-07-04 16:49:15
网址声明必须是:
# Configuration according to tutorial, this should be ommited
#oauth2_endpoint_views = [
# path('authorize/', oauth2_views.AuthorizationView.as_view(), name="authorize"),
# path('token/', oauth2_views.TokenView.as_view(), name="token"),
# path('revoke-token/', oauth2_views.RevokeTokenView.as_view(), name="revoke-token"),
#]
urlpatterns = [
...
# configuration according yo tutorial, which OMITS OIDC
#path('o/', include((oauth2_endpoint_views, 'oauth2_provider'), namespace="oauth2_provider")),
# This is the proper configuration, which enables OIDC
path('o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
...
]https://stackoverflow.com/questions/68245014
复制相似问题