当前位置:网站首页>Implement verification code verification
Implement verification code verification
2022-07-03 12:21:00 【Midsummer month 28】
Import dependence
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
Code implementation
@Controller
public class VerificationCodeController {
@Resource
private DefaultKaptcha defaultKaptcha;
@GetMapping("/common/kaptcha")
public void defaultKaptch(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws IOException {
byte[] captchaOutPutStream = null;
// Array byte output stream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
// Save the generated verification code to session in
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;
}
}
design sketch
边栏推荐
- Swagger
- previous permutation lintcode51
- Colleagues wrote a responsibility chain model, with countless bugs
- 手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法
- [MySQL special] read lock and write lock
- PHP导出word方法(一phpword)
- Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
- 2020-10_ Development experience set
- Wechat applet pages always report errors when sending values to the background. It turned out to be this pit!
- Basic knowledge of OpenGL (sort it out according to your own understanding)
猜你喜欢
2.7 overview of livedata knowledge points
PHP export word method (phpword)
【mysql专项】读锁和写锁
Socket TCP for network communication (I)
2.8 overview of ViewModel knowledge
Basic knowledge of OpenGL (sort it out according to your own understanding)
Shutter: overview of shutter architecture (excerpt)
[learning notes] DP status and transfer
ES6 standard
MCDF Experiment 1
随机推荐
Dart: about Libraries
Vulnhub's Nagini
网络通讯之Socket-Tcp(一)
232. Implement queue with stack
How to deploy web pages to Alibaba cloud
DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
repo Manifest Format
Wrong arrangement (lottery, email)
DEJA_VU3D - Cesium功能集 之 054-模拟火箭发射全过程
Php Export word method (One MHT)
OpenGL shader use
242. Effective letter heteronyms
1-1 token
LeetCode 0556.下一个更大元素 III - 4步讲完
手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法
Introduction to concurrent programming (I)
Self made pop-up input box, input text, and click to complete the event.
adb push apk
PHP export word method (one MHT)
(construction notes) grasp learning experience