当前位置:网站首页>实现验证码验证
实现验证码验证
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;
}
}
效果图
边栏推荐
- Raven2 of vulnhub
- Deploying WordPress instance tutorial under coreos
- Shutter: add gradient stroke to font
- AOSP ~ NTP (Network Time Protocol)
- MySQL time zone solution
- Vulnhub's presidential
- (construction notes) learn the specific technology of how to design reusable software entities from three levels: class, API and framework
- 为什么我的mysql容器启动不了呢
- Groovy test class and JUnit test
- Flutter: about monitoring on flutter applications
猜你喜欢
OpenGL 绘制彩色的三角形
Quantitative calculation research
OPenGL 基本知识(根据自己理解整理)
小鹏 P7 撞护栏安全气囊未弹出,官方回应称撞击力度未达到弹出要求
Unity3d learning notes 5 - create sub mesh
Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022
MCDF Experiment 1
Groovy test class and JUnit test
vulnhub之GeminiInc
Colleagues wrote a responsibility chain model, with countless bugs
随机推荐
Shutter widget: centerslice attribute
QT OpenGL texture map
Redis 笔记 01:入门篇
vulnhub之pyexp
Experience container in libvirt
Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
(构造笔记)ADT与OOP
Momentum of vulnhub
[MySQL special] read lock and write lock
Dynamically monitor disk i/o with ZABBIX
(构造笔记)GRASP学习心得
Shardingsphere sub database and sub table < 3 >
"Jianzhi offer 04" two-dimensional array search
Socket TCP for network communication (I)
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
抓包整理外篇fiddler———— 会话栏与过滤器[二]
(construction notes) ADT and OOP
MySQL time zone solution
023 ([template] minimum spanning tree) (minimum spanning tree)
vulnhub之narak