1.什么是验证码?
验证码是一种区别用户是计算机仍是人的公共全自动程序。短时间是无法退出人类舞台的,现在仅仅尽量提升用户体验。
效果
- 账号安全
- 反作弊
- 反爬虫
- 防论坛灌水
- 防歹意注册
分类
- 图形验证码
- Gif动画验证码
- 手机短信验证码
- 手机语音验证码
- 视频验证码
- web2.0验证码
2.kaptcha验证码组件
kaptcha 是一个非常有用的验证码生成东西;有了它,你能够生成各种款式的验证码,由于它是可装备的。
常见装备
- 验证码的字体
- 验证码字体的巨细
- 验证码字体的字体色彩
- 验证码内容的规模(数字,字母,中文汉字)
- 验证码图片的巨细,边框,边框粗细,边框色彩
- 验证码的搅扰线(能够自己承继com.google.code.kaptcha.NoiseProducer写一个自定义的搅扰线)
- 验证码的款式(鱼眼款式、3D、一般含糊……当然也能够承继com.google.code.kaptcha.GimpyEngine自定义款式)
引进maven装备
- com.github.axet
- kaptcha
- 0.0.9
kaptcha.properties
- kaptcha.textproducer.font.color=red
- kaptcha.image.width=130
- kaptcha.image.height=44
- kaptcha.textproducer.font.size=35
- kaptcha.textproducer.char.length=4
- kaptcha.textproducer.font.names=\\u5B8B\\u4F53,\\u6977\\u4F53,\\u5FAE\\u8F6F\\u96C5\\u9ED1
- kaptcha.noise.color=gray
- kaptcha.obscurificator.impl=com.google.code.kaptcha.impl.WaterRipple
用filter过滤器来生成验证码,具体步骤如下:
1、web.xml
- KaptchaFilter
- com.xxoo.admin.ui.filter.KaptchaFilter
- KaptchaFilter
- /kaptcha.jpg
阐明:验证码过滤器需求放到Shiro之后,由于Shiro将包装HttpSession.如果不,可能形成两次的sesisonid不一样。
2、图片验证码类
- public class CaptchaService {
- private static ImageCaptchaService instance = null;
- static {
- instance = new KaptchaImageCaptchaService();
- }
- public synchronized static ImageCaptchaService getInstance() {
- return instance;
- }
- public synchronized static boolean validate(HttpServletRequest httpServletRequest, String input) throws Exception {
- String text = instance.getText(httpServletRequest);
- boolean result = text.equalsIgnoreCase(input);
- instance.removeKaptcha(httpServletRequest);
- return result;
- }
- }
3、依据Kaptcha的验证码图片实现
- public class KaptchaImageCaptchaService implements ImageCaptchaService {
- private Logger logger = LoggerFactory.getLogger(getClass());
- public KaptchaImageCaptchaService() {
- }
- public static Config getConfig() throws IOException {
- Properties p = new Properties();
- p.load(new DefaultResourceLoader().getResource("kaptcha.properties").getInputStream());
- Config config = new Config(p);
- return config;
- }
- @Override
- public void create(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
- httpServletResponse.setDateHeader("Expires", 0L);
- httpServletResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
- httpServletResponse.addHeader("Cache-Control", "post-check=0, pre-check=0");
- httpServletResponse.setHeader("Pragma", "no-cache");
- httpServletResponse.setContentType("image/jpeg");
- Config config = getConfig();
- Producer producer = config.getProducerImpl();
- String capText = producer.createText();
- if(logger.isDebugEnabled()){
- logger.info("create captcha:" + capText + ":" + config.getSessionKey() );
- }
- httpServletRequest.getSession().setAttribute(config.getSessionKey(), capText);
- httpServletRequest.getSession().setAttribute(config.getSessionDate(), new Date());
- BufferedImage bi = producer.createImage(capText);
- ServletOutputStream out = httpServletResponse.getOutputStream();
- ImageIO.write(bi, "jpg", out);
- out.flush();
- out.close();
- }
- @Override
- public String getText(HttpServletRequest httpServletRequest) throws Exception {
- return (String)httpServletRequest.getSession().getAttribute(getConfig().getSessionKey());
- }
- @Override
- public void removeKaptcha(HttpServletRequest httpServletRequest) throws Exception {
- httpServletRequest.getSession().removeAttribute(getConfig().getSessionKey());
- httpServletRequest.getSession().removeAttribute(getConfig().getSessionDate());
- }
- }
4、验证码东西类
- public class CaptchaService {
- private static ImageCaptchaService instance = null;
- static {
- instance = new KaptchaImageCaptchaService();
- }
- public synchronized static ImageCaptchaService getInstance() {
- return instance;
- }
- public synchronized static boolean validate(HttpServletRequest httpServletRequest, String input) throws Exception {
- String text = instance.getText(httpServletRequest);
- boolean result = text.equalsIgnoreCase(input);
- instance.removeKaptcha(httpServletRequest);
- return result;
- }
- }
5、生成验证码过滤器
- public class KaptchaFilter extends OncePerRequestFilter {
- private Logger logger = LoggerFactory.getLogger(getClass());
- @Override
- protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
- try {
- CaptchaService.getInstance().create(httpServletRequest,httpServletResponse);
- } catch (Exception e) {
- logger.info("create captcha error.",e);
- }
- }
- }
6、验证码校验
- private boolean doCaptchaValidate(HttpServletRequest request, String code) {
- //比对
- try {
- if (code == null || !CaptchaService.validate(request, code)) {
- return false;
- } else {
- return true;
- }
- } catch (Exception e) {
- logger.warn("captcha check error!");
- return false;
- }
- }
总结
本文首要讲述了kaptcha图形化验证码的运用和介绍,小伙伴能够依据自己的需求进行引进。
注:文章内容和图片均来源于网络,只起到信息的传递,不是用于商业,如有侵权请联系删除!