I want to add Botdetect Captcha to my html file.But in the website of the Botdetect captcha, there is example only for jsp. In this jsp file, <taglib>
is used like that:
<%@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>
Is there any alternative for <%@taglib>
in HTML files. How can I solve this problem?
答案 0 :(得分:0)
<%@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>
答案 1 :(得分:0)
我正面临这个问题,因为我使用Thymeleaf而不是JSP,所以我不能使用taglib,但是我仍然拥有“动态HTML”。我正在回答,假设这是您的情况。
我在以下第2部分的帮助页面上看到了可能的解决方案: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的实现是 尚未完全安全