2017-03-26 19 views
0

私はここで理論的で愚かな質問をしていますが、本当に答えを知りたいです。今私はDjangoを使用して、フォームを記入する際に迷惑メールを防ぐためにGoogle reCaptchaでウェブサイトを作成しています。私は、すべての投稿に「私はロボットではない」というメッセージが表示されるように、GoogleのreCaptchaをスキップしないようにjsを設定しました。Google reCaptchaの検証に失敗する可能性がありますか?

私の質問は、迷惑メール(ロボットではない)ではないことが既に証明されている場合、views.pyで構文を書いて、GoogleのreCaptchaをサイトと一致する秘密鍵で検証する必要がある理由ですキー?どのような状況下で、スパムによる提出は、バックエンドの検証ではなく、GoogleのreCaptchaに合格する可能性がありますか?

純粋に理論的な質問ですが、私がやっていることを参考にしたいと思ったら私のコードを添付します。どうもありがとう。

テンプレート:

<form id="contact_form" class="form-horizontal" method="post" action="{% url 'contact' %}"> 
    {% csrf_token %} 



<div class="form-group"> 
    <span id="name_err" style="color:red; bold: false; font-size:1vw; padding-left:1vw; display:none">Please enter your name</span> 
    <label class="control-label col-sm-2" for="name">Name:</label> 
    <div class="col-sm-10"> 
     <input type="text" class="form-control" id="name" name="name" placeholder="Enter name here"> 
    </div> 
</div> 
<script src='https://www.google.com/recaptcha/api.js'></script> 
<div style="padding-left: 11vw;" class="g-recaptcha" data-sitekey="xxx"></div><br> 
<div style="padding-left: 11vw;" id="submit-div"><input type="submit" value="post" class="btn btn-primary"></div> 

views.py

if request.method == 'POST': 

contact_name = request.POST.get('name') 
contact_email = request.POST.get('email') 
contact_subject = request.POST.get('subject') 
contact_message = request.POST.get('message') 
recaptcha_response = request.POST.get('g-recaptcha-response') 

url = 'https://www.google.com/recaptcha/api/siteverify' 
values = { 
    'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY, 
    'response': recaptcha_response 
}    
data = urllib.parse.urlencode(values).encode() 
req = urllib.request.Request(url, data=data) 
response = urllib.request.urlopen(req) 
result = json.loads(response.read().decode()) 
''' End reCAPTCHA validation ''' 

答えて

1

私はあなたが求めているものを完全にはよく分からないけど、あなたは応答があなたのWebページのために有効であることを検証しますユーザーは別のサーバーからのreCaptcha応答を受け取ってから、フォーム送信にそれを挿入することができました。

recaptcha_responseは有効な回答である可能性がありますが、あなたのサイトには該当しない可能性があります。私はそれが助けて欲しい

+0

ありがとう、ありがとう。しかし、サイトに有効なrecaptcha_responseを移植するのは簡単ですか? –

+0

はあなたのサイトの有効なonです。 –

関連する問題