关于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 , 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验证码长度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.keysession keyKAPTCHA_SESSION_KEY
kaptcha.session.datesession dateKAPTCHA_SESSION_DATE
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值