- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要在java spring中集成kaptcha,官网https://code.google.com/p/kaptcha/wiki/SpringUsage是非常旧的信息。
最佳答案
添加maven依赖:
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
将 kaptcha 配置添加到 application.properties:
#Kaptcha
kaptcha.config.imageWidth=310
kaptcha.config.imageHeight=55
kaptcha.config.textProducerCharString=0123456789aAbBcCdDEeFeGgHhIi
kaptcha.config.textProducerCharLength=6
kaptcha.config.headerName=KAPTCHA_HEADER
kaptcha.config.useBorder=no
kaptcha.config.textColor=48,101,137
使用@ConfigurationProperties获取配置
@Data
@Component
@ConfigurationProperties(prefix="kaptcha.config")
public class KaptchaConfigurations{
private String imageWidth;
private String imageHeight;
private String textProducerCharString;
private String textProducerCharLength;
private String headerName;
private String useBorder;
private String backgroundClass;
private String textColor;
}
*如果您不使用 lombok,请将 @Data 替换为 getter 和 setter。
声明生产者@Bean:
@Bean
public Producer createKaptchaProducer(KaptchaConfigurations
kaptchaConfigurations) {
DefaultKaptcha kaptcha = new DefaultKaptcha();
Properties properties = new Properties();
properties.put(Constants.KAPTCHA_IMAGE_HEIGHT ,
kaptchaConfigurations.getImageHeight());
properties.put(Constants.KAPTCHA_IMAGE_WIDTH,
kaptchaConfigurations.getImageWidth());
properties.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH ,
kaptchaConfigurations.getTextProducerCharLength());
properties.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING,
kaptchaConfigurations.getTextProducerCharString());
properties.put(Constants.KAPTCHA_BORDER,
kaptchaConfigurations.getUseBorder());
properties.put(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR,
kaptchaConfigurations.getTextColor());
properties.put(Constants.KAPTCHA_NOISE_COLOR,
kaptchaConfigurations.getTextColor());
kaptcha.setConfig(new Config(properties));
return kaptcha;
}
创建端点以获取生成的验证码并将验证码文本保存在 session 中:
@GetMapping("/image")
public void handleRequest(
HttpServletRequest request,
HttpServletResponse response) throws Exception {
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control",
"no-store, no-cache, must- revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
String capText = captchaProducer.createText();
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY,
capText);
// create the image with the text
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
// write the data out
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
}
创建一个方面来验证验证码:
@Aspect
@Component
public class KaptchaAspect {
private KaptchaConfigurations kaptchaConfigurations;
public KaptchaAspect(KaptchaConfigurations kaptchaConfigurations) {
this.kaptchaConfigurations = kaptchaConfigurations;
}
@Before("@annotation(ValidateKaptcha)")
public void validateKaptcha() throws Throwable {
String headerName = this.kaptchaConfigurations.getHeaderName();
HttpServletRequest request =
((ServletRequestAttributes)
RequestContextHolder
.currentRequestAttributes())
.getRequest();
String headerValue = request.getHeader(headerName);
String kaptchaSessionValue =
request.getSession()
.getAttribute(Constants.KAPTCHA_SESSION_KEY)
.toString();
if(headerValue == null || kaptchaSessionValue == null) {
throw new BusinessException();
}
if(!headerValue.equals(kaptchaSessionValue)) {
throw new BusinessException();
}
}
}
声明验证注释:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidateKaptcha {
}
在任何端点之上使用@ValidateKaptcha来验证验证码:
@ValidateKaptcha
@PostMapping("/forgot-password-by-user-name")
public ResponseEntity<?> forgotPasswordByUsername(@RequestBody @Valid
ForgotPasswordByUsernameInput forgotPasswordByUsernameInput) {
...
}
从客户端在 header 中传递 kaptcha 值,将其命名为 KAPTCHA_HEADER。
关于java - 如何在java spring中集成验证码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28299434/
我在网上找到了这个很棒的小代码,但它似乎没有在正确删除空格后比较两个字符串?我知道一些js,但这里的任何错误都超出了我的理解范围。希望有人知道这个问题的答案。 注意:它似乎还根据 channel 的数
如何使用 requirejs 导入 recaptcha。我已经尝试了几件事,但没有任何效果。 我需要这样做,以便能够在加载后使用 reCaptcha 的渲染方法自行渲染它。 require.confi
我可以做些什么来尝试解决之前一直有效但现在在尝试访问 javascript 文件时返回 404 的重新验证码问题。 我不认为这是编码问题,因为他们今天下午就起来了。 值得一提的是,我的两个使用 re-
好的,我们在生产中实现了 Recaptcha。我们收到错误是因为它无法到达使用该服务所需的 IP 地址。我们为 IP 地址打开一个端口以到达 Google。没问题。我们这样做并显式配置该 IP 地址以
我正在使用 Robot Framework + Selenium2Library 为 Web 编写验收测试。关键是 web 包含一些我无法自动化的输入字段 (CAPTCHA),并且我无法告诉我的供应商
我正在尝试实现验证码。我正在使用 jquery (ajax) 调用验证脚本 (http://www.google.com/recaptcha/api/verify)。这将数据类型限制为 JSONP,G
我在站点中使用 scrapy 提交表单 https://www.barefootstudent.com/jobs (任何进入页面的链接等http://www.barefootstudent.com/l
我经营一个游戏网站,所以我有很多用户登录,他们可以每两分钟做一次某些事情。 我在某些地方有一个 CAPTCHA 系统,对于某些东西,它总是要求输入代码,而对于其他东西,它会每 10 分钟询问一次。 我
thinkphp中的验证码是可以直接调用的,非常方便,我们看一下 Think 文件夹下 有一个名为verify.class.php的文件 首先 我们要有一个模
我正在实现一个在注册表单上带有验证码的网站;我的第一次。我已经阅读了数十篇关于支持和反对论点以及所有各种实现的帖子。我对这一切很满意,但对我来说这是必要的邪恶。 我不明白的是为什么人们会在整个网络上的
我正在使用 Sitecore 8 update 3,目前我向 WFFM 表单添加了验证码并按下音频,但显示错误如下: [ArgumentNullException: Value cannot be n
我正在对我已经完成的网络系统部分进行一小部分升级,其中之一是确保我的 Google reCaptcha 的安全性正确。 目前,我使用此代码: //reCaptcha $Url = "https://w
我正在对我已经完成的网络系统部分进行一小部分升级,其中之一是确保我的 Google reCaptcha 的安全性正确。 目前,我使用此代码: //reCaptcha $Url = "https://w
我对制作 3D 验证码很感兴趣,我让它使用一种字体,如下所示: import string from matplotlib.font_manager import findSystemFonts im
大家。我是jquery初学者,想请教几个问题。 我正在为表单提交测试编写一个简单的数学验证码,我想每次按下“重置按钮”时生成一组新的随机数。 但是当我用谷歌搜索解决方案时,大多数人都在尝试重新加载页面
我的网站上有一个验证码,我认为样式被其他一些 css 覆盖了,正如您在下面的验证码底部看到的那样,它有点偏离.. 在 firebug 中发现 CSS 覆盖的最佳方法是什么?已经看了一段时间了,似乎无法
我在 Google Play 上有一个 PNR 查询应用程序。它工作得很好。但最近 Indian Railwys 在他们的 PNR 查询部分添加了验证码,因此我无法将正确的数据传递到服务器以获得正确的
我被指派为 joomla 中的自定义组件创建验证码验证,但我不知道如何正确地完成它。 我知道有许多可用的验证码插件,例如 recaptcha,但我需要使用公司创建的自定义验证码。 它在 session
本文实例讲述了php/JS实现的生成随机密码(验证码)功能。分享给大家供大家参考,具体如下: PHP写法: ?
我正在关注关于电话授权的 React Native firebase 文档 ( https://rnfirebase.io/docs/v5.x.x/auth/phone-auth ),并且对是否需要(
我是一名优秀的程序员,十分优秀!