【问题标题】:Botdetect Captcha HTML <taglib>Botdetect Captcha HTML <taglib>
【发布时间】:2017-07-12 08:20:07
【问题描述】:

我想将 Botdetect Captcha 添加到我的 html 文件中。但是在 Botdetect 验证码的网站中,只有 jsp 的示例。在这个jsp文件中,&lt;taglib&gt;是这样使用的:

<%@taglib prefix="botDetect" uri="https://captcha.com/java/jsp"%>
....
<botDetect:captcha id="basicExample" userInputID="captchaCode" />

  <div class="validationDiv">
    <input name="captchaCode" type="text" id="captchaCode" value="${basicExample.captchaCode}" />
    <input type="submit" name="validateCaptchaButton" value="Validate" id="validateCaptchaButton" />
    <span class="correct">${basicExample.captchaCorrect}</span>
    <span class="incorrect">${basicExample.captchaIncorrect}</span>
  </div>

在 HTML 文件中 &lt;%@taglib&gt; 是否有任何替代方案。我该如何解决这个问题?

【问题讨论】:

  • HTML 是静态的。
  • 不,没有,但是还有许多其他与纯 HTML 兼容的验证码
  • 嗨,Ben,你能建议普通 HTML 或带有 Thymleaf 的 HTML 的验证码吗? @本

标签: html captcha taglib botdetect


【解决方案1】:

我遇到了这个问题,因为我使用 Thymeleaf 而不是 JSP,所以我不能使用 taglib,但我仍然有“动态 HTML”。我假设这是您的情况。

我在这里的帮助页面上看到了一个可能的解决方案:https://captcha.com/doc/java/jsp-captcha.html 在第 2 部分:

<%
  // Adding BotDetect Captcha to the page
  Captcha captcha = Captcha.load(request, "exampleCaptcha");
  captcha.setUserInputID("captchaCode");

  String captchaHtml = captcha.getHtml();
  out.write(captchaHtml);
%>

这是 JSP 代码,但可以很容易地适应 Thymeleaf。这在打开页面的@Controller 中:

Captcha captcha = Captcha.load(request, "exampleCaptcha");
captcha.setUserInputID("captchaCode");

String captchaHtml = captcha.getHtml();
model.addAttribute("captchaHtml", captchaHtml);

html 就像

<th:block th:utext="${captchaHtml}"></th:block>

验证码检查的代码与JSP相同,但放在处理表单的@Controller中:

 // validate the Captcha to check we're not dealing with a bot
 boolean isHuman = captcha.validate(request.getParameter("captchaCode"));
 if (isHuman) {
  // TODO: Captcha validation passed, perform protected action
 } else {
  // TODO: Captcha validation failed, show error message
 }

要完成,您还需要编辑 web.xml(如果有的话)并按照官方文档导入 java 库。我正在使用 Gradle 并导入 'com.captcha:botdetect-servlet:4.0.beta3.7'

警告:您的服务器应该在 HTTPS 上,否则在使用 4.0.beta3.7 版本时您可能会收到此错误:

Captcha.UserInputID 未设置。您对 BotDetect 的实现是 还不完全安全

【讨论】:

    【解决方案2】:
    <%@taglib prefix="botDetect" uri="https://captcha.com/java/jsp"%>
    ....
    <botDetect:captcha id="basicExample" userInputID="captchaCode" />
    
      <div class="validationDiv">
        <input name="captchaCode" type="text" id="captchaCode" value="${basicExample.captchaCode}" />
        <input type="submit" name="validateCaptchaButton" value="Validate" id="validateCaptchaButton" />
        <span class="correct">${basicExample.captchaCorrect}</span>
        <span class="incorrect">${basicExample.captchaIncorrect}</span>
      </div>
    

    这是错误的,使用 HTML(超文本标记语言)你必须列出 HTML 的版本,在这种情况下它是 Doctype HTML,所以下面的 应该是正确的...

    <!DOCTYPE html>
    <%@taglib prefix="botDetect" uri="https://captcha.com/java/jsp"%>
    ....
    <botDetect:captcha id="basicExample" userInputID="captchaCode" />
    
      <div class="validationDiv">
        <input name="captchaCode" type="text" id="captchaCode" value="${basicExample.captchaCode}" />
        <input type="submit" name="validateCaptchaButton" value="Validate" id="validateCaptchaButton" />
        <span class="correct">${basicExample.captchaCorrect}</span>
        <span class="incorrect">${basicExample.captchaIncorrect}</span>
      </div>
    

    【讨论】:

      猜你喜欢
      • 2017-05-04
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多