当前位置:网站首页>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
边栏推荐
- DEJA_VU3D - Cesium功能集 之 053-地下模式效果
- PHP導出word方法(一mht)
- 实现验证码验证
- 347. Top k high frequency elements
- How to convert a numeric string to an integer
- 4000字超详解指针
- 242. Effective letter heteronyms
- 【嵌入式】---- 内存四区介绍
- Kubectl_ Command experience set
- Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022
猜你喜欢
Is BigDecimal safe to calculate the amount? Look at these five pits~~
ArcGIS application (XXI) ArcMap method of deleting layer specified features
ES6 standard
2.8 overview of ViewModel knowledge
Solve msvcp120d DLL and msvcr120d DLL missing
[official MySQL document] deadlock
Socket TCP for network communication (I)
Wechat applet pages always report errors when sending values to the background. It turned out to be this pit!
QT OpenGL rotate, pan, zoom
Introduction to the implementation principle of rxjs observable filter operator
随机推荐
Kubernetes three dozen probes and probe mode
257. All paths of binary tree
Differences between MySQL Union and union all
OpenGL 索引缓存对象EBO和线宽模式
2.8 overview of ViewModel knowledge
Interview experience in summer camp of Central South University in 2022
previous permutation lintcode51
[MySQL special] read lock and write lock
Socket TCP for network communication (I)
Download address and installation tutorial of vs2015
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
PHP export word method (one MHT)
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
Use of QT OpenGL camera
Dart: about grpc (I)
init. RC service failed to start
Dart: About zone
2.7 overview of livedata knowledge points
239. Sliding window maximum
LeetCode 0556.下一个更大元素 III - 4步讲完