Kaptcha图片验证码工具

阅读文本大概需要3分钟。

验证码的作用

图片验证码自从诞生以来从未被抛弃,依然发出属于它所应有的光。验证码经常验证如下一些场景。

1、用户登录,防止机器人登录

2、论坛留言,防止恶意灌水

3、短信验证码发送,防止盗刷短信

Kaptcha 简介

Kaptcha 是一个可高度配置的实用验证码生成工具,可自由配置的选项如:

  • 验证码的字体

  • 验证码字体的大小

  • 验证码字体的字体颜色

  • 验证码内容的范围(数字,字母,中文汉字!)

  • 验证码图片的大小,边框,边框粗细,边框颜色

  • 验证码的干扰线

  • 验证码的样式(鱼眼样式、3D、普通模糊)

Kaptcha详细配置表

配置项:kaptcha.border
描述:图片边框,合法值:yes , no
默认值:yes

配置项:kaptcha.border.color
描述:边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue.
默认值:black

配置项: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
描述:验证码长度
默认值:5

配置项: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.ShadowGimpy
默认值:com.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.key
描述:session key
默认值:KAPTCHA_SESSION_KEY

配置项:kaptcha.session.date
描述:session date
默认值:KAPTCHA_SESSION_DATE

SpringBoot整合 Kaptcha

1、pom.xml文件中引入

<!-- https://mvnrepository.com/artifact/com.oopsguy.kaptcha/kaptcha-spring-boot-autoconfigure -->
<dependency>
    <groupId>com.oopsguy.kaptcha</groupId>
    <artifactId>kaptcha-spring-boot-autoconfigure</artifactId>
    <version>1.0.0-beta-2</version>
</dependency>

2、配置DefaultKaptcha

package com.piano;

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;

@Configuration
public class ConfigBean {

    @Bean
    public DefaultKaptcha getDefaultKaptcha(){
        DefaultKaptcha dk = new DefaultKaptcha();
        Properties properties = new Properties();
        properties.put("kaptcha.border", "yes");
        properties.put("kaptcha.border.color","105,179,90");
        properties.put("kaptcha.textproducer.font.color","blue");
        properties.put("kaptcha.image.width","125");
        properties.put("kaptcha.image.height","45");
        properties.put("kaptcha.textproducer.font.size","45");
        properties.put("kaptcha.session.key","code");
        properties.put("kaptcha.textproducer.char.length","4");
        properties.put("kaptcha.textproducer.font.names","宋体,楷体,微软雅黑");
        Config config = new Config(properties );
        dk.setConfig(config);
        return dk;
    }

}

3、编写controller

package com.piano.student.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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 com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;

@Controller
public class KaptchaController {

    @Autowired
    private DefaultKaptcha 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;
    }
}

4、访问http://127.0.0.1:8083/verification

往期精彩

01 漫谈发版哪些事,好课程推荐

02 Linux的常用最危险的命令

03 精讲Spring&nbsp;Boot—入门+进阶+实例

04 优秀的Java程序员必须了解的GC哪些

05 互联网支付系统整体架构详解

关注我

每天进步一点点

很干!在看吗?☟

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BUG弄潮儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值