SpringBoot项目实战-请求验证码

136 阅读2分钟

在系统登录的时候,往往需要验证码,为了防止有好事者,频繁的做重复的不友好的操作,现在我要在项目中实现图文验证码的获取。我在config包下,创建一个CaptchaConfig,大家可以根据自己的需要找不同的验证码模版,我这里直接贴图文验证码模板:


/**
 * 验证码配置类
 */
@Configuration
public class CaptchaConfig {

    @Bean
    public DefaultKaptcha defaultKaptcha(){
//        验证码生成器
        DefaultKaptcha defaultKaptcha=new DefaultKaptcha();
//      配置
        Properties properties=new Properties();
//        是否有边框
        properties.setProperty("kaptcha.border","yes");
//        设置边框颜色
        properties.setProperty("kaptcha.border.color","105,179,90");
//        边框粗细度,默认为1
//        properties.setProperty("kaptcha.border.thickness","1");
//        验证码
        properties.setProperty("kaptcha.session.key","code");
//        验证码文本字符颜色,默认黑色
        properties.setProperty("kaptcha.textproducer.font.color","blue");
//        设置字体样式
        properties.setProperty("kaptcha.textproducer.font.names","宋体,楷体,微软雅黑");
//        设置字体大小
        properties.setProperty("kaptcha.textproducer.font.size","30");
//        验证码文本字符内容范围,默认abced2345678fynmnpwx
//        properties.setProperty("kaptcha.textproducer.char.string","");
//        字符长度,默认为5
        properties.setProperty("kaptcha.textproducer.char.length","4");
//        字符间距,默认为2
        properties.setProperty("kaptcha.textproducer.char.space","4");
//        验证码图片宽度,默认200
        properties.setProperty("kaptcha.image.width","100");
//        验证码图片宽度,默认40
        properties.setProperty("kaptcha.image.height","40");
        Config config=new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}

里面主要是设置我们生成的验证码类型,以及大小等配置参数,配置文件写好之后,我们需要写一个接口,以生成验证码(CaptchaController):

/**
 * 验证码
 */
@RestController
public class CaptchaController {
    @Autowired
    private DefaultKaptcha defaultKaptcha;
    @ApiOperation(value="验证码")
    @GetMapping(value = "/captcha",produces = "image/jpeg")
    public void captcha(HttpServletRequest request, HttpServletResponse response){
        // 定义 response 输出类型为 image/jpeg 类型
        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 text = defaultKaptcha.createText(); // 获取验证码文本内容
        System.out.println("验证码文本内容:" + text);
        request.getSession().setAttribute("captcha", text);
        BufferedImage image = defaultKaptcha.createImage(text); // 根据文本内容创建图形验证码
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            ImageIO.write(image,"jpg",outputStream); // 输出流输出图片,格式为jpg
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null!=outputStream) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

简单说明下response.的设置部分是固定的,接着,这里会把生成的验证码保存在session中,到我们检验用户输入的验证码时,可以拿用户输入的跟session中的对比,是否成功!以上就是图文验证码部分的笔记!!!