SpringBoot 集成 kaptcha 验证码插件

kaptcha : 验证码插件,可以使用它生成自定义的验证码

1. kaptcha 依赖



   com.github.penggle
   kaptcha
   2.3.2

2. kaptcha 配置

创建 kaptcha.xml 配置文件,配置文件信息如下?:



    
    
        
            
                
                    
                        
                        120
                        
                        50
                        
                        0123456789AKWUEHPMRX
                        
                        4
                        
                        no
                        
                        105,179,90
                        
                        1
                        
                        yellow
                        
                        30
                        
                        楷体
                        
                        black
                        
                        8
                        
                        com.google.code.kaptcha.impl.ShadowGimpy
                    
                
            
        
    

 3. kaptcha 在 springboot 中配置

打开spring boot 的启动类,将上面所写的 kaptcha.xml 配置文件引入?:

SpringBoot 集成 kaptcha 验证码插件_第1张图片

4. 编写 kaptcha 使用controller

创建 CaptchaController ?:

CaptchaController 内容?:

package guet.hj.travel.controller;

import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
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 CaptchaController {
    private Producer kaptchaProducer=null;

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

    @GetMapping(value = "/getVerifyCode")
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response){
        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 = kaptchaProducer.createText();
        request.getSession().setAttribute("verifyCode", capText);
        BufferedImage bi = kaptchaProducer.createImage(capText);
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            ImageIO.write(bi, "jpg", out);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

5. 前端引用

SpringBoot 集成 kaptcha 验证码插件_第2张图片

温馨附上 οnclick="change()" 用来点击更换验证码图片 

function change() {
        $('#kaptchaImage').click(function () {
            $(this).attr('src', '[[@{/getVerifyCode?}]]' + Math.floor(Math.random() * 100));
        })
    }

展示:

6. 总结:

对于上述的配置,通过 CaptchaController 中的 getVerifyCode 获取验证码图片,并同时将验证码图片的内容信息保存在 httpSession 中

 谢谢大家观看!

你可能感兴趣的:(springboot)