【问题标题】:Handling of django authentication with multiple apps使用多个应用程序处理 django 身份验证
【发布时间】:2020-07-21 09:53:26
【问题描述】:

无法从当前的 SO 问题中找到简单问题的解决方案。

我在一个 django 项目中有 2 个应用程序。
App1来自here找到的图教程
App2 将允许用户列出数据库中的数据,在这种情况下,它将是分支名称。

如果我尝试使用 @login_required 装饰器访问页面,则 url 路由添加了 /accounts/login/ 并且我得到了通常的找不到错误。

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/accounts/login/?next=/calendar

Using the URLconf defined in graph_project.urls, Django tried these URL patterns, in this order:

[name='home']
about [name='about']
signin [name='signin']
signout [name='signout']
calendar [name='calendar']
callback [name='callback']
branches/
admin/
branches/
The current path, accounts/login/, didn't match any of these.

如果我正确阅读了 django 文档,那么这是默认设置,我可以使用项目设置中的 LOGIN_URL 重定向登录路径。当我将其设置为 App1 教程中创建的登录功能时

def sign_in(request):
  # Get the sign-in URL
  sign_in_url, state = get_sign_in_url()
  # Save the expected state so we can validate in the callback
  request.session['auth_state'] = state
  # Redirect to the Azure sign-in page
  return HttpResponseRedirect(sign_in_url)

它将永远循环 MS OAuth 登录,但一旦完成就永远不会访问请求的页面。如果我从设置中省略 LOGIN_URL,它会将帐户/登录/添加到 url,因为这是默认设置。

当请求位于 Login_Required 装饰器后面时,对于其他应用程序中发出的任何请求,我不理解 App1 中的函数处理登录/注销请求的原因是什么?如果我没有 @Login_Required 装饰器并在页面之间移动,为什么它不检查我是否已经通过身份验证。

谢谢

【问题讨论】:

    标签: django django-authentication


    【解决方案1】:

    我联系了 Django-Project 的支持团队。
    其中一位维护人员查看了我的源代码,我觉得一切都很好,他还检查了我在问题中提供的教程链接。

        His exact words were
        >Personally, I’m a bit suspicious with how they’re doing this. I’ve read through the >tutorial and it looks to me like they’ve taken some shortcuts with managing the user in >the session.
        
        That is enough for me to drop that route and look somewhere else, he offered these as a suggestion should anyone care to look into what I moved on to using.  
            [Azure AD pypi][1]
            [Django Auth Docs][2]
            [MS Django Auth][3]
        
        
          [1]: https://pypi.org/project/django-azure-ad-auth/
          [2]: https://django-auth-adfs.readthedocs.io/en/latest/
          [3]: https://pypi.org/project/django-microsoft-auth/
    

    【讨论】:

    • 对不起,它被格式化为代码,所以不会像往常一样接受它。它显示一个错误,说代码已格式化(答案中没有代码)
    猜你喜欢
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 2017-06-22
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多