mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2025-11-04 14:49:58 +08:00
登录强制使用验证码
This commit is contained in:
parent
61fd72ebae
commit
1fc098b093
@ -38,8 +38,6 @@ class User(AbstractBaseUser):
|
|||||||
reset_password_token = models.CharField(max_length=40, blank=True, null=True)
|
reset_password_token = models.CharField(max_length=40, blank=True, null=True)
|
||||||
# token 生成时间
|
# token 生成时间
|
||||||
reset_password_token_create_time = models.DateTimeField(blank=True, null=True)
|
reset_password_token_create_time = models.DateTimeField(blank=True, null=True)
|
||||||
# user 登陆失败的次数计数器。每次密码错误就增加3,而登陆成功一次减去1,在这个计时器大于0的时候,需要输入验证码
|
|
||||||
login_failed_counter = models.IntegerField(default=0)
|
|
||||||
|
|
||||||
USERNAME_FIELD = 'username'
|
USERNAME_FIELD = 'username'
|
||||||
REQUIRED_FIELDS = []
|
REQUIRED_FIELDS = []
|
||||||
|
|||||||
@ -33,30 +33,19 @@ class UserLoginAPIView(APIView):
|
|||||||
serializer = UserLoginSerializer(data=request.data)
|
serializer = UserLoginSerializer(data=request.data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
data = serializer.data
|
data = serializer.data
|
||||||
user = auth.authenticate(username=data["username"], password=data["password"])
|
|
||||||
# 用户名或密码错误的话 返回None
|
|
||||||
if user:
|
|
||||||
# 管理员必须使用验证码 多次错误的使用验证码
|
|
||||||
if user.admin_type > 0 or user.login_failed_counter:
|
|
||||||
if "captcha" not in data:
|
if "captcha" not in data:
|
||||||
return error_response(u"请填写验证码!")
|
return error_response(u"请填写验证码!")
|
||||||
captcha = Captcha(request)
|
captcha = Captcha(request)
|
||||||
if not captcha.check(data["captcha"]):
|
if not captcha.check(data["captcha"]):
|
||||||
return error_response(u"验证码错误")
|
return error_response(u"验证码错误")
|
||||||
|
|
||||||
|
user = auth.authenticate(username=data["username"], password=data["password"])
|
||||||
|
# 用户名或密码错误的话 返回None
|
||||||
|
if user:
|
||||||
auth.login(request, user)
|
auth.login(request, user)
|
||||||
# 登陆成功,计数器减去1
|
|
||||||
if user.login_failed_counter > 0:
|
|
||||||
user.login_failed_counter -= 1
|
|
||||||
user.save()
|
|
||||||
return success_response(u"登录成功")
|
return success_response(u"登录成功")
|
||||||
else:
|
else:
|
||||||
# 登陆失败,计数器加3
|
|
||||||
try:
|
|
||||||
user = User.objects.get(username=data["username"])
|
|
||||||
user.login_failed_counter += 3
|
|
||||||
user.save()
|
|
||||||
except User.DoesNotExist:
|
|
||||||
pass
|
|
||||||
return error_response(u"用户名或密码错误")
|
return error_response(u"用户名或密码错误")
|
||||||
else:
|
else:
|
||||||
return serializer_invalid_response(serializer)
|
return serializer_invalid_response(serializer)
|
||||||
@ -234,23 +223,6 @@ class UserInfoAPIView(APIView):
|
|||||||
return success_response(UserSerializer(request.user).data)
|
return success_response(UserSerializer(request.user).data)
|
||||||
|
|
||||||
|
|
||||||
class AccountSecurityAPIView(APIView):
|
|
||||||
def get(self, request):
|
|
||||||
"""
|
|
||||||
判断用户登录是否需要验证码
|
|
||||||
---
|
|
||||||
"""
|
|
||||||
username = request.GET.get("username", None)
|
|
||||||
if username:
|
|
||||||
try:
|
|
||||||
user = User.objects.get(username=username)
|
|
||||||
except User.DoesNotExist:
|
|
||||||
return success_response({"applied_captcha": True})
|
|
||||||
if user.admin_type > 0 or user.login_failed_counter > 0:
|
|
||||||
return success_response({"applied_captcha": True})
|
|
||||||
return success_response({"applied_captcha": False})
|
|
||||||
|
|
||||||
|
|
||||||
class ApplyResetPasswordAPIView(APIView):
|
class ApplyResetPasswordAPIView(APIView):
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -4,14 +4,12 @@ require(["jquery", "bsAlert", "csrfToken", "validator"], function ($, bsAlert, c
|
|||||||
if (!e.isDefaultPrevented()) {
|
if (!e.isDefaultPrevented()) {
|
||||||
var username = $("#username").val();
|
var username = $("#username").val();
|
||||||
var password = $("#password").val();
|
var password = $("#password").val();
|
||||||
var ajaxData = {username: username, password: password};
|
var captcha = $("#captcha").val();
|
||||||
if (applied_captcha) {
|
|
||||||
ajaxData.captcha = $("#captcha").val();
|
|
||||||
}
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
beforeSend: csrfTokenHeader,
|
beforeSend: csrfTokenHeader,
|
||||||
url: "/api/login/",
|
url: "/api/login/",
|
||||||
data: ajaxData,
|
data: {username: username, password: password, captcha: captcha},
|
||||||
dataType: "json",
|
dataType: "json",
|
||||||
method: "post",
|
method: "post",
|
||||||
success: function (data) {
|
success: function (data) {
|
||||||
@ -47,28 +45,6 @@ require(["jquery", "bsAlert", "csrfToken", "validator"], function ($, bsAlert, c
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#username').blur(function () {
|
|
||||||
if ($("#username").val()) {
|
|
||||||
$.ajax({
|
|
||||||
beforeSend: csrfTokenHeader,
|
|
||||||
url: "/api/account_security_check/?username=" + $("#username").val(),
|
|
||||||
method: "get",
|
|
||||||
success: function (data) {
|
|
||||||
if (!data.code) {
|
|
||||||
if (data.data.applied_captcha) {
|
|
||||||
$('#captcha-area').html('<label for="captcha">验证码</label> <img src="/captcha/" id="captcha-img"><small><p></p></small><input type="text" class="form-control input-lg" id="captcha" name="captcha" placeholder="验证码" maxlength="4" data-error="请填写验证码" required><div class="help-block with-errors"></div>');
|
|
||||||
applied_captcha = true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$('#captcha-area').html('');
|
|
||||||
applied_captcha = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
function refresh_captcha(){
|
function refresh_captcha(){
|
||||||
$("#captcha-img")[0].src = "/captcha/?" + Math.random();
|
$("#captcha-img")[0].src = "/captcha/?" + Math.random();
|
||||||
$("#captcha")[0].value = "";
|
$("#captcha")[0].value = "";
|
||||||
|
|||||||
@ -22,7 +22,13 @@
|
|||||||
|
|
||||||
<div class="help-block with-errors"></div>
|
<div class="help-block with-errors"></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group" id="captcha-area"></div>
|
<div class="form-group" id="captcha-area">
|
||||||
|
<label for="captcha">验证码</label> <img src="/captcha/" id="captcha-img"><small>
|
||||||
|
<p></p></small>
|
||||||
|
<input type="text" class="form-control input-lg" id="captcha" name="captcha"
|
||||||
|
placeholder="验证码" maxlength="4" data-error="请填写验证码" required>
|
||||||
|
<div class="help-block with-errors"></div>
|
||||||
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<button type="submit" class="btn btn-primary">提交</button>
|
<button type="submit" class="btn btn-primary">提交</button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user