- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个使用 Apache Shiro 保护的 Web 应用程序,并且一切正常。现在我想将 Goolge 的 reCaptcha 添加到我的登录页面,但不知道如何做到这一点。
验证码本身已添加并且工作正常,我现在需要帮助的部分是如何将其与 Shiro 集成以在继续对用户进行身份验证之前进行验证。
根据我从 docs 收集到的信息这可以通过过滤器链来完成。
所以我将其放入我的 shiro.ini
中:
[main]
authc.failureKeyAttribute = shiroLoginFailure
authc.loginUrl = /login.jsp
authc.successUrl = /LogIn
logout.redirectUrl = /login.jsp
captcha = path.to.my.class.VerifyUserFilter
...
[urls]
/login.jsp = captcha,authc
/* = authc
我知道如何实现验证验证码的逻辑。我不知道除了实际的验证逻辑之外,VerifyUserFilter 类需要是什么样子才能完成这项工作。本质上,它需要做的就是发布到 Google API,获取响应,解析它,并根据结果将请求传递到下一个过滤器,或者在验证失败时停止。
在我发布这篇文章之前,我决定最后一次尝试,并花了几个小时尝试不同的事情,最终让它发挥作用!无论如何,我仍然决定发布这个问题,并在下面附上我自己的答案,因为我在研究这个问题时找不到太多帮助,也看看我所做的是否正确。如果有更好或更合适的方法来做到这一点,我想知道。
最佳答案
这是我解决这个问题的方法。
首先,我按如下方式设置我的 shiro.ini
(相关部分):
[main]
authc.failureKeyAttribute = shiroLoginFailure
authc.loginUrl = /login.jsp
authc.successUrl = /LogIn
logout.redirectUrl = /login.jsp
captcha = path.to.my.class.VerifyUserFilter
...
[urls]
/login.jsp = captcha,authc
/* = authc
然后,我通过扩展 FormAuthenticationFilter
类并重写读取参数的 doFilterInternal
方法创建了我的 VerifyUserFilter
类,调用 Google API验证响应,然后根据结果,如果验证失败,则重定向到登录页面,或者转到过滤器链中的下一项 - 在本例中为 authc
。
这是我的 VerifyUserFilter
类的完整实现:
import org.apache.shiro.web.util.WebUtils;
public class VerifyUserFilter extends org.apache.shiro.web.filter.authc.FormAuthenticationFilter {
@Override
public void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain)
throws ServletException, IOException {
org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(VerifyUserFilter.class);
if (isLoginSubmission(request, response)){
String user = WebUtils.getCleanParam(request, "username");
String verifId = WebUtils.getCleanParam(request, "g-recaptcha-response");
log.debug("Verif ID: " + verifId);
if (verifId == null || "".equals(verifId)) {
log.warn("User " + user + " missed the captcha challenge and is returned to login page.");
saveRequestAndRedirectToLogin(request, response);
} else {
// Now do the verification of the captcha
final String url = "https://www.google.com/recaptcha/api/siteverify";
final String secret = "6Lxxxxxxxxxxxxxx";
// Send the POST request to the Google API
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection)obj.openConnection();
con.setRequestMethod("POST");
String params = "secret=" + secret + "&response=" + verifId;
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
log.debug("Sending POST to " + url);
wr.writeBytes(params);
wr.flush();
wr.close();
// Get the response code
int respCode = con.getResponseCode();
log.debug("Response code: " + respCode);
// Read in the response data
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
StringBuilder input = new StringBuilder();
log.debug("Reading response data...");
while ((line = in.readLine()) != null){
input.append(line);
}
in.close();
log.debug("Reponse data: " + input);
// Parse the json received to determine if the verif was successful
JsonReader jr = Json.createReader(new StringReader(input.toString()));
JsonObject jo = jr.readObject();
jr.close();
if (jo.getBoolean("success")){
log.debug("User " + user + " is good to go, not a robot!");
// Move on to the next filter
chain.doFilter(request, response);
} else {
// User did not solve the captcha, return to login page
log.warn("User " + user + " failed the captcha challenge and is returned to login page.");
saveRequestAndRedirectToLogin(request, response);
}
}
} else {
log.debug("Not a login attempt, carry on.");
chain.doFilter(request, response);
}
}
}
关于java - 使用 Apache Shiro 实现 reCaptcha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30301277/
我在我的一种表单中集成了 reCAPTCHA v3。在 onload 中,右下角有一个生成的 token 和谷歌验证码标志。但是当我提交表单时,在控制台中会显示一个错误,“错误:不存在 reCAPTC
是我还是 recaptcha 图像无法翻译成除 EN 以外的其他语言? 我有 它仍然是英文的。这是故意的吗? 最佳答案 对于 reCAPTCHA 2. 一段时间过去了 这是你的cdn链接看最后,hl参
我正在尝试按照 https://developers.google.com/recaptcha/docs/faq#can-i-use-recaptcha-globally 中的说明在全局范围内使用 r
我们在我们的网站之一中集成了隐形 recaptcha。每当我们提交表单时,它就会自动提交。 我在一些谷歌小组中读到,在边缘浏览器上访问时我们会遇到挑战。但对我们来说,它是自动提交的。 测试隐形reca
我目前使用的是假的开发域而不是 localhost在我的本地机器上,因为我有多个要在本地管理的网站。 Google reCAPTCHA 无法识别我的域,并给我以下错误:ERROR: Invalid d
reCAPTCHA 需要公钥和私钥才能在网站中实现。它还需要 reCAPTCHA key ,具体取决于网站。这背后的原因是什么?公钥和私钥是否会影响 reCAPTCHA 中显示的文字?我知道我可以将公
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭13 年前。 Improve th
我目前使用隐形recaptcha,如果它认为用户是机器人,它会自动显示一个验证码。 现在有了 Recaptcha v3,我很难理解它应该如何使用。 它返回一个分数,但是如果分数很低,这可能表明用户是机
我希望,如果用户(或漫游器)重复做同样的事情,那么Recaptcha v3的得分会下降,但是事实并非如此。 这是从我的日志中摘录的内容,当我在正在构建的网站上登录时尝试使用不同的密码时,该日志是其中的
我有新的隐形 recaptcha 工作正常,但它把徽章放在左下角或右下角。您可以使用“data-badge='inline'”覆盖它,并将其拉入表单中。谷歌对如何实际移动它非常含糊。您无法隐藏它,因为
我的网站正在使用 Google reCAPTCHA 控件,但我听说它被阻止了 中国,反正我看到有人报告说将 API 更改为 https://www.recaptcha.net在中国工作? Anyone
我面临以下情况:当用户点击提交按钮时,应用会禁用该按钮。然后,当 ReCaptcha 的回调函数被调用时,我可以再次启用该按钮。但是如果用户关闭验证窗口我怎么知道然后再次启用我的按钮? 最佳答案 我在
在 reCAPTCHA v3 文档中,它说 reCAPTCHA works best when it has the most context about interactions with your
我正在尝试在我的网站上呈现 google reCaptcha。我去了google reCaptcha admin ,设置标签,并添加两个域:一个是localhost,另一个是mydomain.com。
我已登录我的 Google 帐户,并导航到 reCaptcha 设置页面。但它显示的只是创建新 reCaptcha 帐户的表单。 我想我曾经能够为我的不同域找到我以前的帐户设置,但现在似乎没有任何地方
ReCaptcha v3 将不再提供验证码,而是依靠浏览器指纹识别和谷歌可以获得的有关您的其他信息。 这是一个测试:https://recaptcha-demo.appspot.com/recaptc
google recaptcha 创建了一个没有可访问性属性的 textarea,例如 aria-label。这导致 recaptcha 无法通过 Siteimprove 进行可访问性扫描。 我已经尝
我正在我的一个网站上实现 reCAPTCHA,并且我已经成功实现了它。它工作正常。有时仅通过单击“我不是机器人”才能完成 reCAPTCHA,但有时它会显示图像网格以选择正确的图像并完成验证码。 我只
我已经在我的网站上实现了 reCaptcha,但每当我测试它时,我都会尝试 3-5 次才能成功。我认为我的用户不会喜欢这个。我注意到有些网站有诸如“2 + 2 是什么?”之类的问题。类似的东西同样有效
我在 Angular+Spring MVC 应用程序中使用 Google reCaptcha 进行用户登录。我想知道谷歌是否对其每天(或周/月/年)支持的点击次数有任何限制。 最佳答案 首先,我建议您
我是一名优秀的程序员,十分优秀!