python django-two-factor-auth无法访问管理站点

dsekswqp  于 2023-08-02  发布在  Python
关注(0)|答案(4)|浏览(109)

我正在使用django-two-factor-auth来做一个web应用。我无法访问管理页面。
我知道我输入了正确的凭据。当我输入不正确的凭据时,我会收到相应的错误消息。
当我输入正确的凭据时,页面只是重新加载以下URL:

http://localhost:8080/account/login/?next=/inveskore/

字符串
以下是我与two_factor相关的设置:

LOGIN_URL = 'two_factor:login'
LOGIN_REDIRECT_URL = '/inveskore'
TWO_FACTOR_SMS_GATEWAY = 'two_factor.gateways.twilio.gateway.Twilio'


这是关联的URL路径:

path('admin/', admin.site.urls),


根据this,这是由于管理员用户没有设置2FA。
那么,如果无法访问站点,如何为管理员用户设置2FA?
编辑:
我记下了网站的2FA登录要求,然后添加了一个电话设备。不走运

toe95027

toe950271#

我最近遇到了这个场景,并根据那里的一条评论创建了这个解决方案:
https://github.com/Bouke/django-two-factor-auth/issues/219#issuecomment-494382380
我子类化了AdminSiteOTPRequired,然后将其指定为要使用的admin类

from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.views import redirect_to_login
from django.http import HttpResponseRedirect
from django.shortcuts import resolve_url
from django.urls import reverse
from django.utils.http import is_safe_url
from two_factor.admin import AdminSiteOTPRequired, AdminSiteOTPRequiredMixin

class AdminSiteOTPRequiredMixinRedirSetup(AdminSiteOTPRequired):
    def login(self, request, extra_context=None):
        redirect_to = request.POST.get(
            REDIRECT_FIELD_NAME, request.GET.get(REDIRECT_FIELD_NAME)
        )
        # For users not yet verified the AdminSiteOTPRequired.has_permission
        # will fail. So use the standard admin has_permission check:
        # (is_active and is_staff) and then check for verification.
        # Go to index if they pass, otherwise make them setup OTP device.
        if request.method == "GET" and super(
            AdminSiteOTPRequiredMixin, self
        ).has_permission(request):
            # Already logged-in and verified by OTP
            if request.user.is_verified():
                # User has permission
                index_path = reverse("admin:index", current_app=self.name)
            else:
                # User has permission but no OTP set:
                index_path = reverse("two_factor:setup", current_app=self.name)
            return HttpResponseRedirect(index_path)

        if not redirect_to or not is_safe_url(
            url=redirect_to, allowed_hosts=[request.get_host()]
        ):
            redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)

        return redirect_to_login(redirect_to)

字符串
urls.py

from django.contrib import admin
admin.site.__class__ = AdminSiteOTPRequiredMixinRedirSetup

csga3l58

csga3l582#

我还不能发表评论,但是@saschwarz上面的答案在Django 2.2和django-two-factor-auth上工作得很好。他的代码中唯一缺少的是额外的import:

from django.http import HttpResponseRedirect

字符串
之后,我可以使用这一行:

admin.site.__class__ = AdminSiteOTPRequiredMixinRedirSetup


为管理员用户快速实施两个因素。
这是django-two-factor-auth的文档中真正缺少的。我看了很长时间的文档,找不到一个清晰的方法来生成二维码。它只在演示应用程序中得到了真正的演示,而不是以一种非常模块化的方式。

kulphzqa

kulphzqa3#

saschewarz的解决方案并没有解决我的应用程序中的问题。所以我找到了另一个标准的管理员登录显示。
django-two-factor-auth默认使用two_factor\admin.py中的'patch_admin'函数修补你的管理员URL,这样你总是会看到你的标准站点登录名。
要解决这个问题,您可以注解掉two_factor\admin.py中的2个函数

def patch_admin()

字符串
和/或

def unpatch_admin()


并在two_factor\apps.py中注解掉

def ready(self)


要在管理站点中使用双因素身份验证,请在main urls.py中添加以下代码(其中是管理员的路径):

from django_otp.admin import OTPAdminSite
admin.site.__class__ = OTPAdminSite

aelbi1ox

aelbi1ox4#

如果您只使用Django Admin而不使用Django网站登录Django Admin,请将下面的LOGIN_REDIRECT_URL设置为settings.py

# "settings.py"

LOGIN_REDIRECT_URL = 'admin:index' # Or

LOGIN_REDIRECT_URL = '/admin' # Or

字符串

相关问题