Kaptcha 驗證碼在springMVC中的使用

項目中配置 Kaptcha 驗證碼分一下幾步:

        1,添加Maven依賴

          <dependency>
            <groupId>com.github.axet</groupId>
            <artifactId>kaptcha</artifactId>
            <version>0.0.9</version>
        </dependency>

       2,spring容器中注入kaptcha,自定義其相關屬性

          <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
        <property name="config">
            <bean class="com.google.code.kaptcha.util.Config">
                <!--通過構造函數注入屬性值 -->
                <constructor-arg type="java.util.Properties">
                    <props>
                        <!-- 驗證碼寬度 -->
                        <prop key="kaptcha.image.width">150</prop>
                        <!-- 驗證碼高度 -->
                        <prop key="kaptcha.image.height">60</prop>
                        <!-- 生成驗證碼內容範圍 -->
                        <prop key="kaptcha.textproducer.char.string">abcde2345678gfynmnpwx</prop>
                        <!-- 驗證碼個數 -->
                        <prop key="kaptcha.textproducer.char.length">5</prop>
                        <!-- 是否有邊框 -->
                        <prop key="kaptcha.border">no</prop>
                        <!-- 驗證碼字體顏色 -->
                        <prop key="kaptcha.textproducer.font.color">red</prop>
                        <!-- 驗證碼字體大小 -->
                        <prop key="kaptcha.textproducer.font.size">35</prop>
                        <!-- 驗證碼所屬字體樣式 -->
                        <prop key="kaptcha.textproducer.font.names">Arial, Courier</prop>
                        <prop key="kaptcha.background.clear.from">white</prop>
                        <prop key="kaptcha.background.clear.to">white</prop>
                        <prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.ShadowGimpy</prop>
                        <prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop>
                        <!-- 干擾線顏色 -->
                        <prop key="kaptcha.noise.color">red</prop>
                        <!-- 驗證碼文本字符間距 -->
                        <prop key="kaptcha.textproducer.char.space">4</prop>
                    </props>
                </constructor-arg>
            </bean>
        </property>
    </bean>

     3 在控制器中寫獲取驗證圖片的方法

   /**
     * Kaptcha 驗證碼
     */
    @Autowired
    private DefaultKaptcha captchaProducer;

    @RequestMapping(value = "captcha.htm", method = RequestMethod.GET)
    public void captcha(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control",
                "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        String capText = captchaProducer.createText();
        request.getSession().setAttribute(
                com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY, capText);
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }

   4,前臺調用

      <img   id="captcha" style="cursor: pointer; cursor: hand; margin-top: -13px;"
                  onclick="this.src='${BASE_PATH}/admin/captcha.htm?'+Math.random();"
                  src="${BASE_PATH}/captcha.htm">

  5,後臺驗證時從Session中獲取驗證碼

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章