首页 > 其他分享 >springboot生成图形验证码

springboot生成图形验证码

时间:2023-02-09 17:11:33浏览次数:35  
标签:springboot kaptcha 验证码 setProperty import 图形 com properties

pom.xml

        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
        </dependency>

 

KaptchaConfig.java
package com.example.demo.config;

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class KaptchaConfig {
    // 实例化这个接口
    @Bean
    public Producer kaptchaProducer() {
        Properties properties = new Properties();
        properties.setProperty("kaptcha.image.width", "100");
        properties.setProperty("kaptcha.image.height", "40");
        properties.setProperty("kaptcha.textproducer.font.size", "32");
        properties.setProperty("kaptcha.textproducer.font.color", "0,0,255");
        // 验证码的字符在哪个范围里随机产生
        properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        // 验证码的长度
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
        DefaultKaptcha kaptcha = new DefaultKaptcha();
        Config config = new Config(properties);
        kaptcha.setConfig(config);
        return  kaptcha;
    }
}

 

控制器:


@Autowired
private Producer kaptchaProducer;

@RequestMapping(path = "/kaptcha", method = RequestMethod.GET) public void getKaptcha(HttpServletResponse httpServletResponse, HttpSession httpSession) throws BizException { // 生成验证码 String text = kaptchaProducer.createText(); BufferedImage image = kaptchaProducer.createImage(text); // 将验证码存入session httpSession.setAttribute("kaptcha", text); // 将图片输出给浏览器 httpServletResponse.setContentType("image/png"); try { OutputStream os = httpServletResponse.getOutputStream(); ImageIO.write(image, "png", os); } catch (IOException e) { throw new BizException(1, "响应验证码失败"); } }

 



前端:
<li valign="top">验证码: <img src="/kaptcha" /></li>

 

效果:

 

标签:springboot,kaptcha,验证码,setProperty,import,图形,com,properties
From: https://www.cnblogs.com/xuxiaobo/p/17106272.html

相关文章

  • springboot开发日记(9)——YAML配置文件
    YAML——适合用来做以数据为中心的配置文件基本语法使用缩进表示层级关系。缩进时不允许使用Tab键,只允许使用空格。(实际使用idea开发中,Tab也有效)缩进的空格数不重......
  • 生成图片验证码
    packagecn.tx.demo1;importcom.sun.tools.javac.Main;importjavax.imageio.ImageIO;importjava.awt.*;importjava.awt.image.BufferedImage;importjava.io.Fi......
  • SpringBoot 项目实战 | 瑞吉外卖 Day02
    该系列将记录一份完整的实战项目的完成过程,该篇属于第二天案例来自B站黑马程序员Java项目实战《瑞吉外卖》,请结合课程资料阅读以下内容该篇我们将完成以下内容:完善登陆......
  • SpringBoot 项目实战 | 瑞吉外卖 Day03
    该系列将记录一份完整的实战项目的完成过程,该篇属于第三天案例来自B站黑马程序员Java项目实战《瑞吉外卖》,请结合课程资料阅读以下内容该篇我们将完成以下内容:公共字段......
  • SpringBoot自动配置原理
    传统的Spring项目,需要我们对每个引入的组件进行手动配置。这需要开发者对组件有深入的了解,否则很容易遗漏某些细节。对于业务开发人员/公司来说,他们只需要知道如何使用组......
  • springboot开发日记(8)——插件
    1.lombok——简化JavaBean的开发普通的bean类需要写getter、setter、重写toString方法,较为麻烦。我们可以通过查找springboot的dependencies得知springboot的依赖里已经......
  • Response案例验证码-代码实现、点击切换
    Response验证码-代码实现packagecom.example.day_14_response;importjavax.imageio.ImageIO;importjavax.servlet.ServletException;importjavax.servlet.Servle......
  • 一篇文章带了解如何用SpringBoot在RequestBody中优雅的使用枚举参数
    目录确认需求定义枚举和对象实现转换逻辑方案一:精准攻击方案二:全范围攻击测试总结 确认需求需求与前文类似,只不过这里需要是在RequestBody中使用。与前文......
  • SpringBoot/SpringCloudAlibaba(ruoyi)中cron表达式(配置每天指定整点执行)读取配置文
    场景若依微服务版手把手教你本地搭建环境并运行前后端项目:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/109363303在上面的基础上某业务需要配置cron表......
  • 【SpringBoot】条件装配 @profile
    profile使用说明:@profile注解的作用是指定类或方法在特定的Profile环境生效,任何@Component或@Configuration注解的类都可以使用@Profile注解。在使用DI来依赖注入的......