登录强制使用验证码

This commit is contained in:
virusdefender 2015-10-09 16:59:49 +08:00
parent 61fd72ebae
commit 1fc098b093
4 changed files with 17 additions and 65 deletions

View File

@ -38,8 +38,6 @@ class User(AbstractBaseUser):
reset_password_token = models.CharField(max_length=40, blank=True, null=True)
# token 生成时间
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'
REQUIRED_FIELDS = []

View File

@ -33,30 +33,19 @@ class UserLoginAPIView(APIView):
serializer = UserLoginSerializer(data=request.data)
if serializer.is_valid():
data = serializer.data
if "captcha" not in data:
return error_response(u"请填写验证码!")
captcha = Captcha(request)
if not captcha.check(data["captcha"]):
return error_response(u"验证码错误")
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:
return error_response(u"请填写验证码!")
captcha = Captcha(request)
if not captcha.check(data["captcha"]):
return error_response(u"验证码错误")
auth.login(request, user)
# 登陆成功计数器减去1
if user.login_failed_counter > 0:
user.login_failed_counter -= 1
user.save()
return success_response(u"登录成功")
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"用户名或密码错误")
else:
return serializer_invalid_response(serializer)
@ -234,23 +223,6 @@ class UserInfoAPIView(APIView):
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):
def post(self, request):
"""

View File

@ -4,14 +4,12 @@ require(["jquery", "bsAlert", "csrfToken", "validator"], function ($, bsAlert, c
if (!e.isDefaultPrevented()) {
var username = $("#username").val();
var password = $("#password").val();
var ajaxData = {username: username, password: password};
if (applied_captcha) {
ajaxData.captcha = $("#captcha").val();
}
var captcha = $("#captcha").val();
$.ajax({
beforeSend: csrfTokenHeader,
url: "/api/login/",
data: ajaxData,
data: {username: username, password: password, captcha: captcha},
dataType: "json",
method: "post",
success: function (data) {
@ -47,28 +45,6 @@ require(["jquery", "bsAlert", "csrfToken", "validator"], function ($, bsAlert, c
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>&nbsp;&nbsp;<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(){
$("#captcha-img")[0].src = "/captcha/?" + Math.random();
$("#captcha")[0].value = "";

View File

@ -22,7 +22,13 @@
<div class="help-block with-errors"></div>
</div>
<div class="form-group" id="captcha-area"></div>
<div class="form-group" id="captcha-area">
<label for="captcha">验证码</label>&nbsp;&nbsp;<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">
<button type="submit" class="btn btn-primary">提交</button>
</div>