Google Kaptcha验证码生成的使用实例说明

网友投稿 1339 2022-10-23

Google Kaptcha验证码生成的使用实例说明

Google Kaptcha验证码生成的使用实例说明

Kaptcha是什么?

kaptcha 是谷歌开源的非常实用的验证码生成工具,基于SimpleCaptcha的开源项目。使用Kaptcha 生成验证码十分简单并且参数可以进行自定义。只需添加jar包配置下就可以使用,通过配置,可以自己定义验证码大小、颜色、显示的字符等等。下面就来讲一下如何使用kaptcha生成验证码以及在服务器端取出验证码进行校验。

怎么使用Kaptcha?

1.首先-jar包

要项目使用Kaptcha肯定要依赖kaptcha的jar,maven项目的话直接加入如下依赖,非maven项目就自己-jar包吧

com.github.axet

kaptcha

0.0.9

官方-链接:https://code.google.com/archive/p/kaptcha/downloads,当然你得翻墙哈

2.配置kaptcha

以项目使用spring为例,配置一个默认的Kaptcha的bean,如下

kaptcha.code

no

black

com.google.code.kaptcha.impl.WaterRipple

com.google.code.kaptcha.impl.NoNoise

90

33

25

4

vqNkcUnKPm 5

247,247,247

247,247,247

http://

3.将验证码保存进session中

将生成的验证码保存进session中,并输出由验证码生成的图片流到页面

@Autowired

private Producer captchaProducer;

@RequestMapping(value = "/kaptchaImage", method = RequestMethod.GET)

public void kaptcha(HttpServletRequest req, HttpServletResponse rsp) {

ServletOutputStream out = null;

try {

HttpSession session = req.http://getSession();

rsp.setDateHeader("Expires", 0);

rsp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");

rsp.addHeader("Cache-Control", "post-check=0, pre-check=0");

rsp.setHeader("Pragma", "no-cache");

rsp.setContentType("image/jpeg");

String capText = captchaProducer.createText();

session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);

BufferedImage image = captchaProducer.createImage(capText);

out = rsp.getOutputStream();

ImageIO.write(image, "jpg", out);

out.flush();

}catch(IOException e)

{

throw new SystemException(e);

} finally {

try {

out.close();

} catch (IOException e) {

throw new SystemException(e);

}

}

}

4.验证码校验

校验用户输入的验证码和保存进session的是否一直,达到验证目的

@RequestMapping(value = "/login", method = RequestMethod.POST, produces = "text/html; charset=utf-8")

public String userLogin(String accountName, String password, String captcha, Boolean rememberMe, HttpServletRequest request) {

//从session中取出kaptcha生成的验证码text值

String expected = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);

//获取用户页面输入的验证码

if(!captcha.equalsIgnoreCase(expected))

{

request.setAttribute("error", "验证码错误!");

return "/login";

}else

{

xxxx验证码后的后续逻辑

}

}

更详细的Kaptcha验证码配置?

kaptcha.border  是否有边框  默认为true  我们可以自己设置yes,no  

kaptcha.border.color   边框颜色   默认为Color.BLACK  

kaptcha.border.thickness  边框粗细度  默认为1  

kaptcha.producer.impl   验证码生成器  默认为DefaultKaptcha  

kaptcha.textproducer.impl   验证码文本生成器  默认为DefaultTextCreator  

kaptcha.textproducer.char.string   验证码文本字符内容范围  默认为abcde2345678gfynmnpwx  

kaptcha.textproducer.char.length   验证码文本字符长度  默认为5  

kaptcha.textproducer.font.names    验证码文本字体样式  默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)  

kaptcha.textproducer.font.size   验证码文本字符大小  默认为40  

kaptcha.textproducer.font.color  验证码文本字符颜色  默认为Color.BLACK  

kaptcha.textproducer.char.space  验证码文本字符间距  默认为2  

kaptcha.noise.impl    验证码噪点生成对象  默认为DefaultNoise  

kaptcha.noise.color   验证码噪点颜色   默认为Color.BLACK  

kaptcha.obscurificator.impl   验证码样式引擎  默认为WaterRipple  

kaptcha.word.impl   验证码文本字符渲染   默认为DefaultWordRenderer  

kaptcha.background.impl   验证码背景生成器   默认为DefaultBackground  

kaptcha.background.clear.from   验证码背景颜色渐进   默认为Color.LIGHT_GRAY  

kaptcha.background.clear.to   验证码背景颜色渐进   默认为Color.WHITE  

kaptcha.image.width   验证码图片宽度  默认为200  

kaptcha.image.height  验证码图片高度  默认为50

效果图如下

以上就是Google Kaptcha验证码的使用实例说明的详细内容,更多关于Google Kaptcha验证码使用的资料请关注我们其它相关文章!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Light DHCPd -- 拥有小型功能集和一个远程可编程控制面板的DHCP服务器
下一篇:最小而美的Egg + React + SSR 服务端渲染应用骨架
相关文章

 发表评论

暂时没有评论,来抢沙发吧~