极验 Django Example

from user import views
from django.urls import path

app_name = 'user'

urlpatterns = [
path('register/', views.RegisterView.as_view(), name='register'),
path('login/', views.LoginView.as_view(), name='login'),
path('logout/', views.LogoutView.as_view(), name='logout'),
path('pc-gt/', views.GtValidateView.as_view(), name='gt-validate'),
path(r'active/<uidb64>/<token>/', views.ActiveView.as_view(), name='active'),
]
from django.conf import settings
from django.shortcuts import render
from django.contrib import messages
from django.http import HttpResponse
from django.urls import reverse_lazy
from django.contrib.auth.models import User
from django.contrib.auth import login, logout
from django.utils.http import urlsafe_base64_decode
from django.contrib.auth.tokens import default_token_generator
from django.http import HttpResponseRedirect, HttpResponseBadRequest
from django.views.generic import FormView, View, TemplateView, CreateView

from geetest import GeetestLib # https://github.com/GeeTeam/gt-python-sdk
from user.forms import UserRegisterForm, UserLoginForm


class GeeTestMixin(object):
gid = settings.GEETEST_ID # 极验ID
gkey = settings.GEETEST_KEY # 极验 Key

def check_request(self, request): # 检查验证是否通过
gt = GeetestLib(self.gid, self.gkey)
challenge = request.POST.get(gt.FN_CHALLENGE, '')
validate = request.POST.get(gt.FN_VALIDATE, '')
seccode = request.POST.get(gt.FN_SECCODE, '')
status = request.session[gt.GT_STATUS_SESSION_KEY]
user_id = request.session["user_id"]
if status:
return gt.success_validate(challenge, validate, seccode, user_id)
return gt.failback_validate(challenge, validate, seccode)


class RegisterView(CreateView, GeeTestMixin):
template_name = 'login_and_register.html'
form_class = UserRegisterForm
success_url = reverse_lazy('user:login')

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'form_title': '注册'
})
return context

def form_valid(self, form):
super().form_valid(form)
messages.add_message(self.request, messages.INFO, '激活邮件已发出,请检查您的邮箱')
return HttpResponseRedirect(self.get_success_url())

def post(self, request, *args, **kwargs):
if not self.check_request(request):
messages.add_message(request, messages.ERROR, '验证失败')
return HttpResponseRedirect(reverse_lazy('user:register'))
return super().post(request, *args, **kwargs)


class GtValidateView(View, GeeTestMixin):

def get(self, request):
user_id = 'test'
gt = GeetestLib(self.gid, self.gkey)
status = gt.pre_process(user_id)
request.session[gt.GT_STATUS_SESSION_KEY] = status
request.session["user_id"] = user_id
response_str = gt.get_response_str()
return HttpResponse(response_str)
$.ajax({
url: "{% url 'user:gt-validate' %}",
type: "get",
dataType: "json",
success: function (data) {
//请检测data的数据结构, 保证data.gt, data.challenge, data.success有值
initGeetest({
// 以下配置参数来自服务端 SDK
gt: data.gt,
challenge: data.challenge,
offline: !data.success,
new_captcha: true,
}, function (captchaObj) {
captchaObj.appendTo('#captchaBox')

captchaObj.onReady(function () {
}).onSuccess(function () {
$('button[type=submit]').removeAttr('disabled')
}).onError(function () {
})
}
)
}
})