maven项目 图片验证码

———-pom.xml配置

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

———-web.xml配置

<servlet>  
    <servlet-name>Kaptcha</servlet-name>  
    <servlet-class>
     com.google.code.kaptcha.servlet.KaptchaServlet
    </servlet-class>  
/**属性配置可不写,试用默认即可start**/
<init-param>  
  <param-name>kaptcha.image.width</param-name>  
  <param-value>200</param-value>  
  <description>
  Width in pixels of the kaptcha image.
 </description>  
 </init-param>  
 <init-param>  
  <param-name>kaptcha.image.height</param-name>  
  <param-value>50</param-value>  
 <description>
     Height in pixels of the kaptcha image.、
 </description>  
</init-param>  
<init-param>  
  <param-name>kaptcha.textproducer.char.length</param-name>  
  <param-value>4</param-value>  
  <description>
  The number of characters to display.
  </description>  
</init-param>  
 <init-param>  
 <param-name>kaptcha.noise.impl</param-name>  
 <param-value>
     com.google.code.kaptcha.impl.NoNoise
 </param-value>  
        <description>The noise producer.</description>  
 </init-param>  
  /**属性配置可不写,试用默认即可end**/  
 </servlet>  
  <servlet-mapping>  
        <servlet-name>Kaptcha</servlet-name>  
        <url-pattern>/kaptcha.jpg</url-pattern>  
  </servlet-mapping>  

———-html使用注意图片id

 <div class="control-form">
    <input type="text" name="picNo" id="picNo" tabindex="2"     class="ui-input valid" placeholder="请输入图片识别码">
   <img  id="kaptchaImage" src="../sign/kaptcha.jpg" /> 
</div>

———-调用方法

@Autowired
    public void setCaptchaProducer(Producer captchaProducer) {
        this.captchaProducer = captchaProducer;
    }

    /**
     * 
     * @Description 获取图片验证码
     * @author pm
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/kaptcha.jpg")
    @NoLogin
    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        // Set to expire far in the past.
        response.setDateHeader("Expires", 0);
        // Set standard HTTP/1.1 no-cache headers.
        response.setHeader("Cache-Control",
                "no-store, no-cache, must-revalidate");
        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        // Set standard HTTP/1.0 no-cache header.
        response.setHeader("Pragma", "no-cache");

        // return a jpeg
        response.setContentType("image/jpeg");

        // create the text for the image
        String capText = captchaProducer.createText();

        // store the text in the session
        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY,
                capText);

        // create the image with the text
        BufferedImage bi = captchaProducer.createImage(capText);

        ServletOutputStream out = response.getOutputStream();

        // write the data out
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
        return null;
    }

———-验证图片验证码是否正确

@RequestMapping("/checkPic.do")
    @NoLogin
    public @ResponseBody JsonResp checkPic(SignVo signVo,
            HttpServletRequest request) throws Exception {
        JsonResp resp = null;
        // 验证图片验证码
        String kaptchaExpected = (String) request.getSession().getAttribute(
                com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
        if (!signVo.getPicNo().equals("")) {
            if (!signVo.getPicNo().equals(kaptchaExpected)) {
                resp = new JsonResp(JsonResp.STATE_ERR);
                return resp;
            }
        }
        resp = new JsonResp(JsonResp.STATE_OK);
        return resp;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值