前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Kaptcha图片验证码工具

Kaptcha图片验证码工具

作者头像
BUG弄潮儿
发布2020-06-12 16:02:08
3.8K0
发布2020-06-12 16:02:08
举报
文章被收录于专栏:JAVA乐园JAVA乐园

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

验证码的作用

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

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

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

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

Kaptcha 简介

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

  • 验证码的字体
  • 验证码字体的大小
  • 验证码字体的字体颜色
  • 验证码内容的范围(数字,字母,中文汉字!)
  • 验证码图片的大小,边框,边框粗细,边框颜色
  • 验证码的干扰线
  • 验证码的样式(鱼眼样式、3D、普通模糊)

Kaptcha详细配置表

代码语言:javascript
复制
配置项: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文件中引入

代码语言:javascript
复制
<!-- 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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 BUG弄潮儿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Kaptcha 简介
  • Kaptcha详细配置表
  • SpringBoot整合 Kaptcha
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档