kaptcha实现验证码生成

        之前编写验证码时,会通过写JSP代码控制验证码的类型(数字范围、文字类型等)、干扰点等,现在使用kaptcha直接就可以做到对这些的控制。在这里做为记录,以便日后回顾使用。

        kaptcha生成验证码时,需要在web.xml中进行配置,通过配置,省略了Servlet类的编写,在引入的jar包中已实现Servlet。

需要引入的jar包

jcaptcha-1.0.jar

jcaptcha-api-1.0.jar

kaptcha-2.3.2.jar

web.xml

<!-- jcaptcha图片生成 -->
   <servlet>  
        <servlet-name>kaptcha</servlet-name>  
        <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class> 
       <init-param>
            <param-name>kaptcha.image.width</param-name>
            <param-value>70</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.image.height</param-name>
            <param-value>28</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.textproducer.font.size</param-name>
            <param-value>25</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.textproducer.char.length</param-name>
            <param-value>4</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.textproducer.char.space</param-name>
            <param-value>3</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.textproducer.font.color</param-name>
            <param-value>black</param-value>
        </init-param>
        <init-param> 
            <description>The characters that will create the kaptcha. </description> 
            <param-name>kaptcha.textproducer.char.string</param-name> 
            <param-value>1234567890</param-value> 
        </init-param> 
        <init-param>
            <param-name>kaptcha.noise.color</param-name>
            <param-value>white</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.obscurificator.impl</param-name>
            <param-value>com.google.code.kaptcha.impl.ShadowGimpy</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.noise.impl</param-name>
            <param-value>com.google.code.kaptcha.impl.NoNoise</param-value>
        </init-param>
        
        <init-param>
            <param-name>kaptcha.background.clear.from</param-name>
            <param-value>white</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.background.clear.to</param-name>
            <param-value>white</param-value>
        </init-param>
        <init-param>
            <param-name>kaptcha.border.color</param-name>
            <param-value>181,184,200</param-value>
        </init-param>
    
    </servlet>  
    <servlet-mapping>  
        <servlet-name>kaptcha</servlet-name>  
        <url-pattern>/rest/kaptcha.jpg</url-pattern>  
    </servlet-mapping>

html

<input type="text" value="" id="j_captcha" class="input-verification" name="randCode" placeholder=""/>

Java

String kaptchaExpected = (String)((HttpServletRequest) request).getSession()
                .getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);


你可能感兴趣的:(验证码,kaptcha)