اگه قصد دارید با حملات هکرها مقابله کنید و لاگ تلاش های ناموفق در ورود به اپلیکیشن رو داشته باشد می توانید از django-axes استفاده کنید. نحوه استفاده هم خیلی راحته:
بعد از نصب با دستور
pip install django-axes
مراحل زیر را انجام دهید:
اپلیکیشن axes رو در قسمت INSTALLED_APPS در فایل setting.py وارد کنید:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles','django_plotly_dash.apps.DjangoPlotlyDashConfig',
# Axes app can be in any position in the INSTALLED_APPS list.
'axes',
]
در همین جا به قسمت AUTHENTICATION_BACKENDS موارد زیر رو اضافه کنید:
AUTHENTICATION_BACKENDS = [
# AxesBackend should be the first backend in the AUTHENTICATION_BACKENDS list.
'axes.backends.AxesBackend',
# Django ModelBackend is the default authentication backend.
'django.contrib.auth.backends.ModelBackend',
]
همچنین به MIDDLEWARE اضافه کنید:
MIDDLEWARE = [
# The following is the list of default middleware in new Django projects.
'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',
# AxesMiddleware should be the last middleware in the MIDDLEWARE list.
# It only formats user lockout messages and renders Axes lockout responses
# on failed user authentication attempts from login views.
# If you do not want Axes to override the authentication response
# you can skip installing the middleware and use your own views.
'axes.middleware.AxesMiddleware',
]
حالا در cmd ، این عبارت رو اجرا کنید:
python manage.py check
و بعد
python manage.py migrate
بعد از انجام این مراحل در صفحه ادمین جنگو می توانید لاگ ها رو مشاهده کنید.
پ ن: توجه داشته باشید:
def custom_login_view(request)
username = ...
password = ...
user = authenticate(
request=request, # this is the important custom argument
username=username,
password=password,
)
if user is not None:
login(request, user)
این ماژول یه کاربرد دیگه هم داره... آن هم محدود کردن تعداد دفعات ورود رمز و نام کاربری اشتباه است. تلاش می کنم در آینده این رو هم اضافه کنم.