使用 Kaptcha 生成验证码

1.pom.xml 配置文件如下


    
      junit
      junit
      4.11
      test
    
    
      com.google.code.kaptcha
      kaptcha
      2.3
    
    
      org.springframework
      spring-context
      4.3.17.RELEASE
    
    
      org.springframework
      spring-web
      4.3.17.RELEASE
    
    
      org.springframework
      spring-webmvc
      4.3.17.RELEASE
    
    
      javax.servlet
      servlet-api
      2.5
    
  

2.web.xml配置如下



    
        contextConfigLocation
        classpath*:spring-context-kaptcha.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
        springmvc
        
            org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath*:springmvc.xml
        
        1
    
    
    
        springmvc
        /
    
    
    
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        characterEncodingFilter
        /*
    

3.springmvc.xml的配置



    
    

    
    
        
        
            /WEB-INF/content/
        
        
        
            .jsp
        
    

4.创建一个名为 spring-context-kaptcha.xml Spring 配置文件,配置如下 



    
        
            
                
                    
                        yes
                        105,179,90
                        blue
                        150
                        50
                        45
                        code
                        4
                        宋体,楷体,微软雅黑
                    
                
            
        
    

5.控制器关键代码

Controller 层的关键代码如下,主要作用为将生成的验证码放入 Session 并输出到页面

package com.funtl.my.shop.web.ui.controller;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Controller
public class KaptchaController {

    @Autowired
    private Producer captchaProducer;

    @RequestMapping(value = "verification", method = RequestMethod.GET)
    public ModelAndView verification(HttpServletRequest request, HttpServletResponse response) throws IOException {
        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;
    }
}

6.JSP 关键代码

JSP 使用 标签去请求验证码图片

7.运行效果

使用 Kaptcha 生成验证码_第1张图片

备注:

如果pom.xml文件中kaptcha.2.3.jar下载失败,可先在https://mvnrepository.com/中下载其jar,然后在jar所在的目录执行mvn install:install-file -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=2.3 -Dpackaging=jar -Dfile=kaptcha-2.3.jar命令即可。

你可能感兴趣的:(使用 Kaptcha 生成验证码)