博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JCaptcha图片验证,解决区分大小写问题
阅读量:6422 次
发布时间:2019-06-23

本文共 5918 字,大约阅读时间需要 19 分钟。

hot3.png

这是一个老项目留下的问题,首先他是这么配置的

180
180000
toddlist
20
20
Tahoma
1
1
60
28
4
4
0
51
153
然后我写了两个类, GimpyCopy.java
/* * jcaptcha, the open source java framework for captcha definition and integration * Copyright (c) 2005 jcaptcha.net. All Rights Reserved. * See the LICENSE.txt file distributed with this package. */
package com.octo.captcha.image.gimpy;import com.octo.captcha.image.ImageCaptcha;import java.awt.image.BufferedImage;import java.io.Serializable;/** * 

A Gimpy is a ImagCaptcha. It is also the most common captcha.

  • Challenge type : image
  • *
  • Response type : String
  • Description : An image of a distorded word is shown. User have to recognize the * word and to submit it.
* * @author Marc-Antoine Garrigue * @version 1.0 */public class GimpyCopy extends ImageCaptcha implements Serializable { private String response; GimpyCopy(String question, BufferedImage challenge, String response) { super(question, challenge); this.response = response; } /** * Validation routine from the CAPTCHA interface. this methods verify if the response is not null and a String and * then compares the given response to the internal string. * * @return true if the given response equals the internal response, false otherwise. */ public final Boolean validateResponse(final Object response) { return (null != response && response instanceof String) ? validateResponse((String) response) : Boolean.FALSE; } /** * Very simple validation routine that compares the given response to the internal string. * * @return true if the given response equals the internal response, false otherwise. */ private final Boolean validateResponse(final String response) {        // 主要改的这里 return new Boolean(response.toLowerCase().equals(this.response.toLowerCase())); } ;}
GimpyCopyFactory.java
/* * jcaptcha, the open source java framework for captcha definition and integration * Copyright (c) 2005 jcaptcha.net. All Rights Reserved. * See the LICENSE.txt file distributed with this package. */
package com.octo.captcha.image.gimpy;import com.octo.captcha.CaptchaException;import com.octo.captcha.CaptchaQuestionHelper;import com.octo.captcha.component.image.wordtoimage.WordToImage;import com.octo.captcha.component.word.wordgenerator.WordGenerator;import com.octo.captcha.image.ImageCaptcha;import java.awt.image.BufferedImage;import java.security.SecureRandom;import java.util.Locale;import java.util.Random;/** * Factories for Gimpies. Built on top of WordGenerator and WordToImage. It uses thoses interfaces to build an * ImageCaptha answered by a String and for which the question is : Spell the word. */public class GimpyCopyFactory extends com.octo.captcha.image.ImageCaptchaFactory {    private Random myRandom = new SecureRandom();    private WordToImage wordToImage;    private WordGenerator wordGenerator;    public static final String BUNDLE_QUESTION_KEY = Gimpy.class.getName(); // 这个还是用原来的Gimpy    public GimpyCopyFactory(WordGenerator generator, WordToImage word2image) {        if (word2image == null) {            throw new CaptchaException("Invalid configuration" +                    " for a GimpyFactory : WordToImage can't be null");        }        if (generator == null) {            throw new CaptchaException("Invalid configuration" +                    " for a GimpyFactory : WordGenerator can't be null");        }        wordToImage = word2image;        wordGenerator = generator;    }    /**     * gimpies are ImageCaptcha     *     * @return the image captcha with default locale     */    public ImageCaptcha getImageCaptcha() {        return getImageCaptcha(Locale.getDefault());    }    public WordToImage getWordToImage() {        return wordToImage;    }    public WordGenerator getWordGenerator() {        return wordGenerator;    }    /**     * gimpies are ImageCaptcha     *     * @return a pixCaptcha with the question :"spell the word"     */    public ImageCaptcha getImageCaptcha(Locale locale) {        //length        Integer wordLength = getRandomLength();        String word = getWordGenerator().getWord(wordLength, locale);        BufferedImage image = null;        try {            image = getWordToImage().getImage(word);        } catch (Throwable e) {            throw new CaptchaException(e);        }        // 这里用我们自己写的GimpyCopy        ImageCaptcha captcha = new GimpyCopy(CaptchaQuestionHelper.getQuestion(locale, BUNDLE_QUESTION_KEY),                image, word);        return captcha;    }    protected Integer getRandomLength() {        Integer wordLength;        int range = getWordToImage().getMaxAcceptedWordLength() -                getWordToImage().getMinAcceptedWordLength();        int randomRange = range != 0 ? myRandom.nextInt(range + 1) : 0;        wordLength = new Integer(randomRange +                getWordToImage().getMinAcceptedWordLength());        return wordLength;    }}
这下好了,问题解决了

转载于:https://my.oschina.net/webinteligent/blog/79139

你可能感兴趣的文章
android 5.1 WIFI图标上的感叹号及其解决办法
查看>>
地址 寻址 存储
查看>>
Receive Windows Messages In NonWindowed Control
查看>>
linux之SQL语句简明教程---SUBSTRING
查看>>
【JS】Beginner7:Functions
查看>>
hdu 3955 March
查看>>
数据库性能优化之冗余字段的作用
查看>>
C语言程序设计第五次作业
查看>>
基础搜索算法的常见题型
查看>>
Go语言的接口
查看>>
DBA_实践指南系列9_Oracle Erp R12应用补丁AutoPatch/AutoControl/AutoConfig(案例)
查看>>
Python语言学习 (六)1.2
查看>>
数据库设计三大范式
查看>>
ionic 字体的导入方法
查看>>
3-【python13_day2】
查看>>
Java多线程基础(二)
查看>>
c#调用java
查看>>
js操作cookie
查看>>
【AC自动机】AC自动机
查看>>
AI佳作解读系列(五) - 目标检测二十年技术综述
查看>>