登录认证中接入极验验证
官网: https://www.geetest.com/first_page/
注册登录以后,即进入登录后台,选择行为验证。
接下来,就可以根据官方文档,把验证码集成到项目中了
文档地址:https://docs.geetest.com/install/overview/start/
下载和安装验证码模块包。
git clone https://github.com/GeeTeam/gt3-python-sdk.git
安装依赖模块
pip install requests
把验证码模块防止在utils中
并在users子应用下创建验证码视图类,并提供验证码和校验验证码的视图方法。
from luffy.utils.geetest import GeetestLib
from rest_framework.views import APIView
from rest_framework.response import Response
# Create your views here.
class VerifyCode(APIView):
def get(self,request):
user_id = 'test'
pc_geetest_id = "a9feefab99c8d4bbcf0d9e3021048312"
pc_geetest_key = "aa467ab83be3c44929bc7da76eb88028"
gt = GeetestLib(pc_geetest_id, pc_geetest_key)
status = gt.pre_process(user_id, JSON_FORMAT=0, ip_address="127.0.0.1")
if not status:
status = 2
request.session[gt.GT_STATUS_SESSION_KEY] = status
request.session["user_id"] = user_id
response_str = gt.get_response_str()
print( response_str )
return Response(response_str)
def post(self,request):
pc_geetest_id = "a9feefab99c8d4bbcf0d9e3021048312"
pc_geetest_key = "aa467ab83be3c44929bc7da76eb88028"
gt = GeetestLib(pc_geetest_id, pc_geetest_key)
challenge = request.data.get(gt.FN_CHALLENGE, "")
validate = request.data.get(gt.FN_VALIDATE, "")
seccode = request.data.get(gt.FN_SECCODE, "")
status = request.session[gt.GT_STATUS_SESSION_KEY]
user_id = request.session["user_id"]
if status == 1:
result = gt.success_validate(challenge, validate, seccode, user_id, JSON_FORMAT=0)
else:
result = gt.failback_validate(challenge, validate, seccode)
request.session["user_id"] = user_id
return Response({{"message":result}})
路由注册:
path(r'code/', views.VerifyCode.as_view()),