首页 > 其他分享 >SpringBoot中使用Kaptcha实现验证码

SpringBoot中使用Kaptcha实现验证码

时间:2023-03-03 21:55:52浏览次数:80  
标签:SpringBoot Kaptcha image 验证码 kaptcha setProperty import com properties

1. 首先,我们在pom.xml文件中引入kaptcha的maven依赖。

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

 2. 然后,我们编写kaptcha的配置类:KaptchaConfig.java。

 1 package com.rqfreefly.community.config;
 2 
 3 import com.google.code.kaptcha.Producer;
 4 import com.google.code.kaptcha.impl.DefaultKaptcha;
 5 import com.google.code.kaptcha.util.Config;
 6 import org.springframework.context.annotation.Bean;
 7 import org.springframework.context.annotation.Configuration;
 8 
 9 import java.util.Properties;
10 
11 @Configuration
12 public class KaptchaConfig {
13 
14     @Bean
15     public Producer kaptchaProducer() {
16         Properties properties = new Properties();
17         properties.setProperty("kaptcha.image.width", "100");
18         properties.setProperty("kaptcha.image.height", "40");
19         properties.setProperty("kaptcha.textproducer.font.size", "32");
20         properties.setProperty("kaptcha.textproducer.font.color", "0,0,0");
21         properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYAZ");
22         properties.setProperty("kaptcha.textproducer.char.length", "4");
23         properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
24 
25         DefaultKaptcha kaptcha = new DefaultKaptcha();
26         Config config = new Config(properties);
27         kaptcha.setConfig(config);
28         return kaptcha;
29     }
30 
31 }

 3. 接下来,我们编写kaptcha的控制层。

 1     @RequestMapping(path = "/kaptcha", method = RequestMethod.GET)
 2     public void getKaptcha(HttpServletResponse response, HttpSession session) {
 3         String text = kaptchaProducer.createText();
 4         BufferedImage image = kaptchaProducer.createImage(text);
 5         session.setAttribute("kaptcha", text);
 6         response.setContentType("image/png");
 7         try {
 8             ServletOutputStream os = response.getOutputStream();
 9             ImageIO.write(image, "png", os);
10         } catch (IOException e) {
11             logger.error("响应验证码失败:" + e.getMessage());
12         }
13     }

 4. 然后,我们就可以在前端调用katpcha的接口生成验证码。

 

标签:SpringBoot,Kaptcha,image,验证码,kaptcha,setProperty,import,com,properties
From: https://www.cnblogs.com/RQfreefly/p/17177095.html

相关文章

  • 出报关,海关,验证码本地识别
             本地识别出售 联系v  备注购买验证码  ......
  • springboot 过滤器FilterRegistrationBean详解
    一:基础知识1.通过FilterRegistrationBean实例注册,该方法能够设置过滤器之间的优先级2.为了演示优先级,这里创建2个测试过滤器类:Test1Filter、Test2Filter通过实现javax.serv......
  • Springboot Controller接口默认自动填充 业务实体参数值
    前言今天看有小伙伴求救: 我还是一贯如此,有人不明白,没玩过HandlerMethodArgumentResolver。那么很可能不止他一个人,那么我就有必要出手。不多说,开搞。 正文快速模拟出......
  • springboot(三)
    1.热部署可以在不重启服务器的情况下重新加载更新后的程序.1.1实现原理①非springboot项目热部署实现原理开发非springboot项目时,我们要制作一个web工程并通过tomca......
  • SpringBoot接口返回统一格式
    packagecom.example.springboot.common;importorg.springframework.core.MethodParameter;importorg.springframework.http.MediaType;importorg.springframework......
  • springboot整合mybatis
    1,引入依赖<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version><scope>runtime</scope></depend......
  • SpringBoot集成Knife4j
    转载自: SpringBoot集成Knife4j-hviger-博客园(cnblogs.com)Knife4j简介Knife4j官网地址:https://doc.xiaominfo.com/knife4j是为JavaMVC框架集成Swagger生成Ap......
  • springboot启动报错 Failed to scan *****/derbyLocale_ja_JP.jar from classloader h
    springboot启动报错Failedtoscan*****/derbyLocale_ja_JP.jarfromclassloaderhierarchy 这两天自己在玩虚拟机,想把线上的平台复制一份到虚拟机上,jdk,tomcat服务,防......
  • springboot jpa hibernate mysql clickhouse 多数据源
    ClickhouseConfig.java@Configuration@EntityScan(basePackages="test.entity.clickhouse")@EnableJpaRepositories(basePackages="test.repository.clic......
  • 视频上传及压缩SpringBoot
    在SpringBoot项目中实现视频的上传和下载,地址都是存放至阿里云的,但是由于视频数量越来越多占用的内存也越来越大,导致服务器内存不足,公司为了减少服务器开支,要求我们研......