当前位置:网站首页>实现验证码验证
实现验证码验证
2022-07-03 11:32:00 【仲夏月二十八】
导入依赖
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
代码实现
@Controller
public class VerificationCodeController {
@Resource
private DefaultKaptcha defaultKaptcha;
@GetMapping("/common/kaptcha")
public void defaultKaptch(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws IOException {
byte[] captchaOutPutStream = null;
// 数组字节输出流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
//将生成的验证码保存到session中
String kaptchaText = defaultKaptcha.createText();
httpServletRequest.getSession().setAttribute("verifyCode",kaptchaText);
BufferedImage image = defaultKaptcha.createImage(kaptchaText);
ImageIO.write(image,"jpg",byteArrayOutputStream);
} catch (IOException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
captchaOutPutStream = byteArrayOutputStream.toByteArray();
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
responseOutputStream.write(captchaOutPutStream);
responseOutputStream.flush();
responseOutputStream.close();
}
}
package com.cheng.controller.common;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import sun.security.krb5.KrbException;
import java.util.Properties;
@Component
public class KaptchaConfig {
@Bean
public DefaultKaptcha getDefaultKaptcha() {
com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
Properties properties = new Properties();
properties.put("kaptcha.border","no");
properties.put("kaptcha.textproducer.font.color", "black");
properties.put("kaptcha.image.width", "150");
properties.put("kaptcha.image.height", "40");
properties.put("kaptcha.textproducer.font.size", "30");
properties.put("kaptcha.session.key", "verifyCode");
properties.put("kaptcha.textproducer.char.space", "5");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
效果图

边栏推荐
猜你喜欢

Php Export word method (One MHT)

Quantitative calculation research

ES6新特性

网络通讯之Socket-Tcp(一)

laravel 时区问题timezone

Momentum of vulnhub

ArcGIS application (XXI) ArcMap method of deleting layer specified features

Basic knowledge of OpenGL (sort it out according to your own understanding)

《剑指offer 03》数组中重复的数字

Wechat applet - basic content
随机推荐
[official MySQL document] deadlock
Experience container in libvirt
MCDF Experiment 1
ES6新特性
Kubernetes three dozen probes and probe mode
(构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
网络通讯之Socket-Tcp(一)
Differences between MySQL Union and union all
【mysql专项】读锁和写锁
4000字超详解指针
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
Qt OpenGL相机的使用
(construction notes) learning experience of MIT reading
利用Zabbix动态监控磁盘I/O
Vulnhub narak
Fluent: Engine Architecture
C language improvement article (wchar_t) character type
laravel 时区问题timezone
ArcGIS application (XXI) ArcMap method of deleting layer specified features
Qt OpenGL 纹理贴图