当前位置: 首页 > 工具软件 > JCaptcha > 使用案例 >

生成验证码工具jar->Jcaptcha

何楷
2023-12-01

Jar包名

Jcaptcha-all-1.0-RC6.jar

bean 配置

    <bean id="captchaService" class="com.octo.captcha.service.multitype.GenericManageableCaptchaService">
        <constructor-arg index="0" ref="imageEngine" />
        <constructor-arg type="int" index="1" value="180" />
        <constructor-arg type="int" index="2" value="100000" />
    </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.GimpyFactory">
        <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.RandomWordGenerator">
        <!--可选字符-->
        <constructor-arg>
            <value>
                0123456789abcdefghijklmnopqrstuvwxyz
            </value>
        </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="decoratedPaster" />
        </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>
                <bean class="java.awt.Font">
                    <constructor-arg index="0">
                        <value>Arial</value>
                    </constructor-arg>
                    <constructor-arg index="1">
                        <value>0</value>
                    </constructor-arg>
                    <constructor-arg index="2">
                        <value>20</value>
                    </constructor-arg>
                </bean>
            </list>
        </constructor-arg>
    </bean>

    <bean id="backGenUni" class="com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator">
        <!--背景宽度-->
        <constructor-arg index="0">
            <value>80</value>
        </constructor-arg>
        <!--背景高度-->
        <constructor-arg index="1">
            <value>32</value>
        </constructor-arg>
    </bean>

<!--textpaster有多种paster方式,如LineRandomTextPaster-->
    <bean id="decoratedPaster" class="com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster">
        <!--最大字符长度-->
        <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 index="2">
            <ref bean="colorGen" />
        </constructor-arg>
        <!--文本混淆-->
        <constructor-arg index="3">
            <list>
                <!--<ref bean="baffleDecorator"/>-->
            </list>
        </constructor-arg>
    </bean>

    <bean id="baffleDecorator" class="com.octo.captcha.component.image.textpaster.textdecorator.BaffleTextDecorator">
        <constructor-arg type="java.lang.Integer" index="0">
            <value>1</value>
        </constructor-arg>
        <constructor-arg type="java.awt.Color" index="1">
            <ref bean="colorWrite" />
        </constructor-arg>
    </bean>

    <bean id="colorGen" class="com.octo.captcha.component.image.color.SingleColorGenerator">
        <constructor-arg type="java.awt.Color" index="0">
            <ref bean="colorDimGrey" />
        </constructor-arg>
    </bean>

    <bean id="colorWrite" class="java.awt.Color">
        <constructor-arg type="int" index="0">
            <value>255</value>
        </constructor-arg>
        <constructor-arg type="int" index="1">
            <value>255</value>
        </constructor-arg>
        <constructor-arg type="int" index="2">
            <value>255</value>
        </constructor-arg>
    </bean>

    <bean id="colorDimGrey" class="java.awt.Color">
        <constructor-arg type="int" index="0">
            <value>105</value>
        </constructor-arg>
        <constructor-arg type="int" index="1">
            <value>105</value>
        </constructor-arg>
        <constructor-arg type="int" index="2">
            <value>105</value>
        </constructor-arg>
    </bean>
</beans>

方法调用-生成验证码

String captchaId = request.getSession().getId(); 

BufferedImage challenge = imageCaptchaService.getImageChallengeForID(captchaId, request.getLocale());

ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();

ImageIO.write(challenge, "jpeg", jpegOutputStream);

byte[] captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

os.flush();

os.close();

String captchaImage = new String(Base64.encodeBase64(captchaChallengeAsJpeg), "UTF-8");

方法调用-验证验证码

Boolean isResponseCorrect = imageCaptchaService.validateResponseForID(request.getSession().getId(), captcha);

 

 类似资料: