关于SSM(Spring+SpringMVC+Mybatis)三大框架集成验证码插件Kaptcha简单方法

关于SSM(Spring+SpringMVC+Mybatis)三大框架集成验证码插件Kaptcha简单方法


Kaptcha是一个基于SimpleCaptcha的验证码开源项目。

官网地址:http://code.google.com/p/kaptcha/

<!-- kaptcha 验证码开源组件 -->
        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
        </dependency>
  • 配置Kaptcha.xml文件(要生成验证码的样式参数)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-3.1.xsd 
 http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
 <!-- Kaptcha组件配置 -->
 <bean id="kaptchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">  
        <property name="config">  
            <bean class="com.google.code.kaptcha.util.Config">  
                <constructor-arg>  
                    <props>  
                        <!-- 验证码宽度 -->  
                        <prop key="kaptcha.image.width">138</prop>  
                        <!-- 验证码高度 -->  
                        <prop key="kaptcha.image.height">28</prop>  
                        <!-- 生成验证码内容范围 -->  
                        <prop key="kaptcha.textproducer.char.string">0123456789AKWUEHPMRX</prop>  
                        <!-- 验证码个数 -->  
                        <prop key="kaptcha.textproducer.char.length">5</prop>  
                        <!-- 是否有边框 -->  
                        <prop key="kaptcha.border">no</prop>  
                        <!-- 边框颜色 -->  
                        <prop key="kaptcha.border.color">105,179,90</prop>  
                        <!-- 边框厚度 -->  
                        <prop key="kaptcha.border.thickness">1</prop>  
                        <!-- 验证码字体颜色 -->  
                        <prop key="kaptcha.textproducer.font.color">black</prop>  
                        <!-- 验证码字体大小 -->  
                        <prop key="kaptcha.textproducer.font.size">25</prop>  
                        <!-- 验证码所属字体样式 -->  
                        <prop key="kaptcha.textproducer.font.names">楷体</prop>  
                        <!-- 干扰线颜色 -->  
                        <prop key="kaptcha.noise.color">black</prop>  
                        <!-- 验证码文本字符间距 -->  
                        <prop key="kaptcha.textproducer.char.space">8</prop>  
                        <!-- 图片样式 :阴影-->  
                        <prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.ShadowGimpy</prop>  
                    </props>  
                </constructor-arg>  
            </bean>  
        </property>  
    </bean>
 </beans>
  • 引入Spring_Mybatis.xml文件中
<!-- 引入Kaptcha组件配置文件 -->
<import resource="spring-Kaptcha.xml"/>
  • 代码实现绘制验证码图片
@Controller
public class CaptchaController {
    private Producer kaptchaProducer=null;  

    @Autowired  
    public void setCaptchaProducer(Producer kaptchaProducer) {  
        this.kaptchaProducer = kaptchaProducer;  
    }  

    @RequestMapping("/kaptcha")  
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response){  
        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 capText = kaptchaProducer.createText();
        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
        BufferedImage bi = kaptchaProducer.createImage(capText);  
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            ImageIO.write(bi, "jpg", out);
        } catch (IOException e) {
            e.printStackTrace();
        }  

        try {  
            out.flush();  
        } catch (IOException e) {
            e.printStackTrace();
        } finally {  
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }  
        }  
        return null;  
    }  
}  
  • 生成结果样式
    这里写图片描述

  • 基本参数

Constant 描述 默认值
kaptcha.border 图片边框,合法值:yes , no yes
kaptcha.border.color 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue. black
kaptcha.border.thickness 边框厚度,合法值:>0 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
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

猜你喜欢

转载自blog.csdn.net/qq_33624284/article/details/79000767