Springboot2.0.6+DefaultKaptcha2.3.2实现生成验证码

1、在pom文件中引入Kaptcha依赖:

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

2、创建kaptchad的配置文件:kaptcha.properties

##### Kaptcha Information
kaptcha.width=150
kaptcha.height=42
kaptcha.border=no
kaptcha.textproducer.font.size=40
kaptcha.textproducer.char.space=10
kaptcha.textproducer.font.names=\u4EFF\u5B8B,\u5FAE\u8F6F\u96C5\u9ED1
kaptcha.textproducer.char.string=1234567890
kaptcha.textproducer.char.length=4
kaptcha.background.clear.from=92,189,170
kaptcha.background.clear.to=255,255,255
常量描述默认值
kaptcha.border设置是否有边框yes
kaptcha.border.color设置边框颜色

black

(105,179,90)

kaptcha.border.thickness设置边框的厚度1
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

(org.cric.util.ChineseText)

kaptcha.textproducer.char.stringThe characters that will create the kaptcha.abcde2345678gfynmnpwx
kaptcha.textproducer.char.length设置字体个数5
kaptcha.textproducer.font.names设置字体样式Arial, Courier(宋体,楷体,微软雅黑)
kaptcha.textproducer.font.size 设置字体大小40px.
kaptcha.textproducer.font.color设置字体颜色black
kaptcha.noise.implThe noise producer.com.google.code.kaptcha.impl.DefaultNoise
kaptcha.noise.colorThe noise color. Legal values are r,g,b.black
kaptcha.obscurificator.implThe obscurificator implementation.com.google.code.kaptcha.impl.WaterRipple
kaptcha.background.implThe background implementation.com.google.code.kaptcha.impl.DefaultBackground
kaptcha.background.clear.fromStarting background color. Legal values are r,g,b.light grey
kaptcha.background.clear.toEnding background color. Legal values are r,g,b.white
kaptcha.word.implThe word renderer implementation.com.google.code.kaptcha.text.impl.DefaultWordRenderer
kaptcha.session.keyThe value for the kaptcha is generated and is put into the HttpSession. This is the key value for that item in the session.KAPTCHA_SESSION_KEY
kaptcha.session.dateThe date the kaptcha is generated is put into the HttpSession. This is the key value for that item in the session.KAPTCHA_SESSION_DATE
   

3、创建生成defaultKaptcha的工具类:

package com.hhf.utils;
 
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
 
@Component("numCodeUtil")
public class NumCodeUtil {
	
	private static Properties props = new Properties();
	
	@Bean
	public DefaultKaptcha defaultKaptcha() throws Exception {
		// 创建DefaultKaptcha对象
		DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
		
		// 读取配置文件
		try {
			props.load(NumCodeUtil.class.getClassLoader()
                .getResourceAsStream("kaptcha.properties"));
		}catch (Exception e) {
			e.printStackTrace();
		}
 
		// 将Properties文件设到DefaultKaptcha对象中
		defaultKaptcha.setConfig(new Config(props));
		return defaultKaptcha;
	} 
}

4、controller层实现:

package com.hhf.house.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

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.ByteArrayOutputStream;
import java.io.IOException;

/***
 * 验证码controller
 */
@Controller
public class CodeController{

    @Autowired
    private DefaultKaptcha defaultKaptcha;
    @GetMapping("/numCode")
    public void getNumCode(HttpServletResponse response, HttpServletRequest request) throws IOException {
        // 创建字节数组用户存放图片信息
        byte[] numCodeImgByte = null;
        // 获得二进制输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // 通过DefaultKaptcha获得随机验证码
        String code = defaultKaptcha.createText();
        // 将验证码存放在session中
        request.getSession().setAttribute("code",code);
        // 生成图片
        BufferedImage image = defaultKaptcha.createImage(code);
        // 将图片写入到流中
        ImageIO.write(image, "jpg", baos);
        numCodeImgByte = baos.toByteArray();
        // 通过response设定响应请求类型
        // no-store用于防止重要的信息被无意的发布。在请求消息中发送将使得请求和响应消息都不使用     缓存。
        response.setHeader("Cache-Control","no-store");
        // no-cache指示请求或响应消息不能缓存
        response.setHeader("Pragma","no-cache");
        /* expires是response的一个属性,它可以设置页面在浏览器的缓存里保存的时间 ,超过设定的时        间后就过期 。过期后再次
         * 浏览该页面就需要重新请求服务器发送页面数据,如果在规定的时间内再次访问次页面 就不需从服务器传送 直接从缓存中读取。
         * */
        response.setDateHeader("Expires", 0);
        // servlet接受request请求后接受图片形式的响应
        response.setContentType("image/jpeg");
        // 通过response获得输出流
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(numCodeImgByte);
        outputStream.close();
    }
}

5、测试获取验证码:localhost:8097/numCode

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值