• kaptcha验证码组件用法


    1.添加依赖

    <dependency>
        <groupId>com.github.penggle</groupId>
        <artifactId>kaptcha</artifactId>
        <version>2.3.2</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.在applicationContext.xml配置Kaptcha

        <!-- 配置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.border">no</prop>
                            <!-- 验证码图片宽度为120像素 -->
                            <prop key="kaptcha.image.width">120</prop>
                            <!-- 验证码图片字体颜色为蓝色 -->
                            <prop key="kaptcha.textproducer.font.color">blue</prop>
                            <!-- 每个字符最大占用40像素 -->
                            <prop key="kaptcha.textproducer.font.size">40</prop>
                            <!-- 验证码包含4个字符 -->
                            <prop key="kaptcha.textproducer.char.length">4</prop>
                        </props>
                    </constructor-arg>
                </bean>
            </property>
        </bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3.在KaptchaController中实现逻辑功能

    @Controller
    public class KaptchaController {
        @Resource
        private Producer kaptchaProducer;
    
        @GetMapping("/verify_code")
        public void createVerifyCode(HttpServletRequest request , HttpServletResponse response) throws IOException {
            //响应立即过期
            response.setDateHeader("Expires",0);
            //不缓存任何图片数据
            response.setHeader("Cache-Control" , "no-store,no-cache,must-revalidate");
            response.setHeader("Cache-Control" , "post-check=0,pre-check=0");
            response.setHeader("Pragma" , "no-cache");
            response.setContentType("image/png");
            //生成验证码字符文本
            String verifyCode = kaptchaProducer.createText();
            request.getSession().setAttribute("kaptchaVerifyCode",verifyCode);
            System.out.println(request.getSession().getAttribute("kaptchaVerifyCode"));
            BufferedImage image = kaptchaProducer.createImage(verifyCode);//创建验证码图片
            ServletOutputStream out = response.getOutputStream();
            ImageIO.write(image, "png", out);//输出图片流
            out.flush();
            out.close();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    树莓派(一)树莓派的4种登陆方式
    Three.js(7):局部纹理刷新
    Codeforces Round 731 (Div 3)(A - F)
    单卡成功验证RLHF DPO效果
    MYSQL下载及安装完整教程
    基于C语言的图论汇编
    Worthington胶原蛋白酶的多类型研究
    云计算-Linux-软链接与硬链接,获取命令帮助,系统运行级别,关机和重启
    项目进展(十)-解决ADS1285在调试时出现的问题
    el-table 合集行合并
  • 原文地址:https://blog.csdn.net/qq_41787812/article/details/133104940