gpt4 book ai didi

python - Django 身份验证覆盖不起作用

转载 作者:太空狗 更新时间:2023-10-29 21:57:55 25 4
gpt4 key购买 nike

我在我的 django 项目中创建了一个与“settings.py”文件相同级别的文件“authentication.py”。这个文件的内容是:

from django.contrib.auth.models import User

class SettingsBackend(object):

def authenticate(self, request, username=None, password=None):
user_name = 'user_name'
user_password = 'user_pass'
login_valid = (user_name == username)
pwd_valid = (password == user_password)
if login_valid and pwd_valid:
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
user = User(username=username)
user.is_staff = True
user.is_superuser = True
user.save()
return user
return None

def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None

然后我在“settings.py”文件中添加了以下行:

AUTHENTICATION_BACKENDS = ('myProject.authentication.SettingsBackend',)

但是,登录不起作用。当用户的凭据存储在数据库中时,它在这两个修改之前工作。我不知道如何调试它。有什么想法吗?

这是我的一些设置文件:

INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'default',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ["templates/"],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}




# Password validation
# https://docs.djangoproject.com/en/1.10/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',
},
]

LOGIN_REDIRECT_URL = '/'
LOGIN_URL = '/login'
LOGOUT_URL = '/logout'
ADMIN_ENABLED = False

编辑:我删除了文件夹根目录下的“db.sqlite3”文件,然后运行 ​​django shell 并执行了以下操作:

from django.contrib.sessions.models import Session
Session.objects.all().delete()

然后我明白了:

from django.contrib.auth.models import User
user = User.objects.get(username='user_name')
>> DoesNotExist: User matching query does not exist.

最佳答案

您可能会尝试运行:

Session.objects.all().delete().

在测试新的身份验证后端之前。 From the Django docs:

Once a user has authenticated, Django stores which backend was used to authenticate the user in the user’s session, and re-uses the same backend for the duration of that session whenever access to the currently authenticated user is needed. This effectively means that authentication sources are cached on a per-session basis, so if you change AUTHENTICATION_BACKENDS, you’ll need to clear out session data if you need to force users to re-authenticate using different methods. A simple way to do that is simply to execute Session.objects.all().delete().

关于python - Django 身份验证覆盖不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45955031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com