当前位置:网站首页>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

边栏推荐
- 232. Implement queue with stack
- Introduction to concurrent programming (I)
- [combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
- regular expression
- 安裝electron失敗的解决辦法
- Redis
- typeScript
- Colleagues wrote a responsibility chain model, with countless bugs
- 4000字超详解指针
- Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022
猜你喜欢

OpenGL 索引缓存对象EBO和线宽模式

PHP导出word方法(一phpword)

Integer int compare size

4000 word super detailed pointer

2.8 overview of ViewModel knowledge

Itext7 uses iexternalsignature container for signature and signature verification

【附下载】密码获取工具LaZagne安装及使用

Shutter: overview of shutter architecture (excerpt)

win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法

1-2 project technology selection and structure
随机推荐
lambda与匿名内部类的区别
Shutter widget: centerslice attribute
Introduction to the implementation principle of rxjs observable filter operator
【嵌入式】---- 内存四区介绍
在网上炒股开户可以吗?资金安全吗?
OpenGL index cache object EBO and lineweight mode
If you can't learn, you have to learn. Jetpack compose writes an im app (II)
2020-10_ Development experience set
安裝electron失敗的解决辦法
(构造笔记)ADT与OOP
Flutter: self study system
OpenGL draws colored triangles
Dart: about Libraries
Talk about the state management mechanism in Flink framework
(数据库提权——Redis)Redis未授权访问漏洞总结
20. Valid brackets
102. Sequence traversal of binary tree
Shutter: about inheritedwidget
(construction notes) learning experience of MIT reading
DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch