springboot中使用Kaptcha验证码

Kaptcha是一个比较好用的验证码组件,也是比较常用的,比其他验证码组件生成简便多了。
Kaptcha是谷歌放在github上的一个验证码jar包,我们可以简单配置属性实现验证码的验证功能

作用

  • 验证码取值范围:数字,字符,汉字
  • 自动生成随机验证码数字,数字个数
  • 验证码图片背景,大小,边框粗细,边框颜色
  • 验证码干扰线
  • 验证码样式

pom.xml

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

配置

配置kaptcha有两种方式,一个是基于xml形式,一种是java代码配置。
我使用的是java代码形式,本文也是介绍的这种(xml太麻烦了,看着就不舒服)。

package com.zjg.smsrt.util;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
 * @author zjg
 * @date 2018/7/24 21:27
 * @Description 验证码工具类
 */
@Slf4j
@Component
public class KaptchaConfig {

    @Bean
    public DefaultKaptcha getDefaultKaptche () {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        properties.setProperty("kaptcha.border","yes");
        properties.setProperty("kaptcha.border.color","105,179,90");
        properties.setProperty("kaptcha.textproducer.font.color","127,96,0");
        properties.setProperty("kaptcha.textproducer.font.size","30");
        properties.setProperty("kaptcha.image.width","110");
        properties.setProperty("kaptcha.image.height","40");
        properties.setProperty("kaptcha.session.key","code");
        properties.setProperty("kaptcha.textproducer.char.length","4");
        properties.setProperty("kaptcha.textproducer.font.names","宋体,楷体,微软雅黑");
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);

        return defaultKaptcha;
    }
}

然后就可以调用它了

package com.zjg.smsrt.controller;

import lombok.extern.slf4j.Slf4j;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author zjg
 * @date 2018/7/8 20:14
 * @Description
 */
@Slf4j
@RestController
public class LoginController {

    @Autowired
    private final DefaultKaptcha kaptcha;

    @GetMapping("/kaptcha")
    public void kaptcha ( HttpServletResponse servletResponse) {
        log.info("tologin");
        byte [] kaptchaBytes = null;
        ByteArrayOutputStream byteArrOUT = new ByteArrayOutputStream();
        String kaptchaText =  kaptcha.createText();
        BufferedImage bufferedImage = kaptcha.createImage(kaptchaText);
        try {
            ImageIO.write(bufferedImage,"jpg",byteArrOUT);
        } catch (IOException e) {
            log.info("输出验证码图片发生异常");
            return;
        }

        //定义输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
        kaptchaBytes = byteArrOUT.toByteArray();
        servletResponse.setHeader("Cache-Control","no-store");
        servletResponse.setHeader("Pragma","no-cache");
        servletResponse.setDateHeader("Expires",0);
        servletResponse.setContentType("image/jpeg");
        try {
            ServletOutputStream servletOutputStream = servletResponse.getOutputStream();
            servletOutputStream.write(kaptchaBytes);
            servletOutputStream.flush();
            servletOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

然后访问http://localhost:8080/kaptcha
看下运行效果截图
这里写图片描述

看具体的样式参数

Constant描述默认值
kaptcha.border图片边框,合法值:yes , noyes
kaptcha.border.color 边框颜色,合法值: r,g,b (and optional alpha) 或者white,black,blue.black
kaptcha.border.thickness边框厚度,合法值:>01
kaptcha.image.width图片宽200
kaptcha.image.height图片高50
kaptcha.producer.impl图片实现类com.google.code.kaptcha.impl.DefaultKaptcha
kaptcha.textproducer.impl文本实现类com.google.code.kaptcha.text.impl.DefaultTextCreator
kaptcha.textproducer.char.string文本集合,验证码值从此集合中获取abcde2345678gfynmnpwx
kaptcha.textproducer.char.length验证码长度4
kaptcha.textproducer.font.names字体Arial, Courier
kaptcha.textproducer.font.size字体大小40px.
kaptcha.textproducer.font.color字体颜色,合法值: r,g,b 或者 white,black,blue.black
kaptcha.textproducer.char.space文字间隔2
kaptcha.noise.impl干扰实现类com.google.code.kaptcha.impl.DefaultNoise
kaptcha.noise.color干扰 颜色,合法值: r,g,b 或者 white,black,blue. black
kaptcha.obscurificator.impl图片样式: 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy阴影com.google.code.kaptcha.impl.ShadowGimpycom.google.code.kaptcha.impl.WaterRipple
kaptcha.background.impl背景实现类com.google.code.kaptcha.impl.DefaultBackground
kaptcha.background.clear.from背景颜色渐变,开始颜色light grey
kaptcha.background.clear.to背景颜色渐变, 结束颜色white
kaptcha.word.impl文字渲染器com.google.code.kaptcha.text.impl.DefaultWordRenderer
kaptcha.session.keysession keyKAPTCHA_SESSION_KEY
kaptcha.session.datesession dateKAPTCHA_SESSION_DATE
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值