JCaptcha图片验证,解决区分大小写问题

本文介绍了一个关于JCaptcha配置的问题及解决方案。通过调整GimpyCopyFactory类,并创建GimpyCopy类,使得验证码服务正常运行。涉及的具体改动包括:自定义验证码验证逻辑和验证码图片生成过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

<!-- ===================================================================== -->
	<!-- ============================ JCaptcha  ============================== -->
    <!-- ===================================================================== -->
<bean id="captchaService"	class="com.octo.captcha.service.multitype.GenericManageableCaptchaService">
		<constructor-arg index="0"><ref bean="imageEngine"/></constructor-arg>
   	 	<constructor-arg index="1"><value>180</value></constructor-arg>
   		<constructor-arg index="2"><value>180000</value></constructor-arg>
	</bean>
	<bean  id="imageEngine"     class="com.octo.captcha.engine.GenericCaptchaEngine">
		<constructor-arg index="0">
			<list>
				<ref bean="CaptchaFactory"/>
			</list>
		</constructor-arg>
	</bean>
	<bean id="CaptchaFactory" class="com.octo.captcha.image.gimpy.GimpyCopyFactory" ><!--已经改为重写的类 -->
		<constructor-arg><ref bean="wordgen"/></constructor-arg>
		<constructor-arg><ref bean="wordtoimage"/></constructor-arg>
	</bean>

	<bean id="wordgen" class= "com.octo.captcha.component.word.wordgenerator.DictionaryWordGenerator" >
		<constructor-arg><ref bean="filedict"/></constructor-arg>
	</bean>

	<bean id="wordtoimage" class="com.octo.captcha.component.image.wordtoimage.ComposedWordToImage" >
		<constructor-arg index="0"><ref bean="fontGenRandom"/></constructor-arg>
		<constructor-arg index="1"><ref bean="backGenUni"/></constructor-arg>
		<constructor-arg index="2"><ref bean="simpleWhitePaster"/></constructor-arg>
	</bean>

	<bean id="filedict" class="com.octo.captcha.component.word.FileDictionary" >
		<constructor-arg index="0"><value>toddlist</value></constructor-arg>
	</bean>

	<bean id="fontGenRandom" class="com.octo.captcha.component.image.fontgenerator.RandomFontGenerator" >
		<constructor-arg index="0"><value>20</value></constructor-arg>
		<constructor-arg index="1"><value>20</value></constructor-arg>
		<constructor-arg index="2">
			<list>
				<ref bean="fontRoman"/>
			</list>
		</constructor-arg>
	</bean>

	<bean id="fontRoman" class="java.awt.Font" >
			<constructor-arg index="0"><value>Tahoma</value></constructor-arg>
			<constructor-arg index="1"><value>1</value></constructor-arg>
			<constructor-arg index="2"><value>1</value></constructor-arg>
	</bean>

	<bean id="backGenUni" class="com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator" >
			<constructor-arg index="0"><value>60</value></constructor-arg>
			<constructor-arg index="1"><value>28</value></constructor-arg>
	</bean>

	<bean id="simpleWhitePaster" class="com.octo.captcha.component.image.textpaster.SimpleTextPaster" >
		<constructor-arg type="java.lang.Integer" index="0"><value>4</value></constructor-arg>
		<constructor-arg type="java.lang.Integer" index="1"><value>4</value></constructor-arg>
		<constructor-arg type="java.awt.Color" index="2">
			<ref bean="colorGreen"/>
		</constructor-arg>
	</bean>

	<bean id="colorGreen" class="java.awt.Color" >
			<constructor-arg index="0" type="int"><value>0</value></constructor-arg>
			<constructor-arg index="1" type="int"><value>51</value></constructor-arg>
			<constructor-arg index="2" type="int"><value>153</value></constructor-arg>
	</bean>
然后我写了两个类, 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;

/**
 * <p>A Gimpy is a ImagCaptcha. It is also the most common captcha.</p> <ul> <li>Challenge type : image</li>
 * <li>Response type : String</li> <li>Description : An image of a distorded word is shown. User have to recognize the
 * word and to submit it.</li> </ul>
 *
 * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
 * @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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值