迁移reCAPTCHA到v2版本是否仍需保留隐藏字段?
关于reCAPTCHA v2迁移时隐藏公钥字段的处理
不需要保留第一行的那个隐藏字段hf-register-publickey,原因如下:
- reCAPTCHA v2的集成逻辑和v1完全不同:v1依赖手动传递公钥给旧版脚本,而v2是通过官方脚本加载+页面元素属性来配置公钥的,根本不需要额外的隐藏字段存储公钥。
- 原来的隐藏字段是给v1的
recaptcha/api/challenge脚本提供公钥用的,v2的脚本(https://www.google.com/recaptcha/api.js)不会读取这个字段,保留它只会增加冗余的DOM元素,没有任何实际作用。
给你一个reCAPTCHA v2的基础实现示例,替换原来的代码即可:
<div class="editor-field"> <!-- reCAPTCHA v2容器 --> <div class="g-recaptcha" data-sitekey="@SettingsHelper.CaptaPublicKey"></div> <!-- 无脚本 fallback --> <noscript> <div style="width: 302px; height: 422px;"> <div style="width: 302px; height: 422px; position: relative;"> <div style="width: 302px; height: 422px; position: absolute;"> <iframe src="https://www.google.com/recaptcha/api/fallback?k=@SettingsHelper.CaptaPublicKey" frameborder="0" scrolling="no" style="width: 302px; height:422px; border-style: none;"> </iframe> </div> <div style="width: 250px; height: 80px; position: absolute; border-style: none; bottom: 21px; left: 25px; margin: 0; padding: 0; right: 25px;"> <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 80px; border: 1px solid #c1c1c1; margin: 0; padding: 0; resize: none;" value=""> </textarea> </div> </div> </div> </noscript> </div>
简单说明下这个示例:
- 核心是带有
data-sitekey属性的g-recaptcha容器,直接把你的公钥通过Razor语法注入进去 - 无脚本的fallback也使用了v2的官方地址,参数传递方式和v1不同,不需要依赖那个隐藏字段
内容的提问来源于stack exchange,提问作者BWhite
相关产品推荐
相关产品推荐

