zoukankan      html  css  js  c++  java
  • spring mvc 使用kaptcha配置生成验证码实例

    SpringMVC整合kaptcha(验证码功能)

    一、依赖

    <dependency>
        <groupId>com.github.penggle</groupId>
        <artifactId>kaptcha</artifactId>
        <version>2.3.2</version>
    </dependency>

    二、springmvc配置文件

    <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
            <property name="config">
                <bean class="com.google.code.kaptcha.util.Config">
                    <constructor-arg>
                        <props>
                            <prop key="kaptcha.border">yes</prop>
                            <prop key="kaptcha.border.color">105,179,90</prop>
                            <prop key="kaptcha.textproducer.font.color">blue</prop>
                            <prop key="kaptcha.image.width">125</prop>
                            <prop key="kaptcha.image.height">45</prop>
                            <prop key="kaptcha.textproducer.font.size">45</prop>
                            <prop key="kaptcha.session.key">code</prop>
                            <prop key="kaptcha.textproducer.char.length">4</prop>
                            <prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>
                        </props>
                    </constructor-arg>
                </bean>
            </property>
        </bean>

    三、编码

    1.后端

    controler类:
        package com.neusoft.controller;

    import java.awt.image.BufferedImage;

    import javax.imageio.ImageIO;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;

    import com.google.code.kaptcha.Constants;
    import com.google.code.kaptcha.Producer;
        
    @Controller
    @RequestMapping(value="/code")
    public class codeController {

         @Autowired
         private Producer captchaProducer = null;

            @RequestMapping(value = "/getcode")
            public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
                HttpSession session = request.getSession();
                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();
                session.setAttribute(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();
                }
               return null;
            }
    }

    2.前端

    <div class="form-group">
        <div class="row">
            <div class="col-sm-8">
                <input type="text" id="code" class="form-control" placeholder="验证码" data-toggle="tooltip" data-placement="top">
            </div>
            <div class="col-sm-4">
                <img id="codeImg" src="http://127.0.0.1:8666/sys/captcha" class="img-responsive" style="display: block;100%;height: 32px">
            </div>
        </div>
    </div>

    3.效果图

  • 相关阅读:
    uva 12169 Disgruntled Judge
    uva 11582 Colossal Fibonacci Numbers!
    ACdream 1073
    ACdream 1061 郭式树
    《构建之法》读书笔记
    南阳oj 素数求和问题
    hdoj Problem-1007 A hard puzzle
    程序员不得不知的 Navicat 实用工具
    Beyond Compare怎么对比图片差异
    教您用Beyond Compare比较十六进制文件
  • 原文地址:https://www.cnblogs.com/liubin1988/p/8721956.html
Copyright © 2011-2022 走看看