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

边栏推荐
- 2020-10_ Development experience set
- Quantitative calculation research
- How to deploy web pages to Alibaba cloud
- Download address and installation tutorial of vs2015
- Adult adult adult
- Symlink(): solution to protocol error in PHP artisan storage:link on win10
- typeScript
- 4000 word super detailed pointer
- 2.6 preliminary cognition of synergetic couroutines
- OpenGL 着色器使用
猜你喜欢

Quantitative calculation research

QT OpenGL texture map

Download address and installation tutorial of vs2015

During FTP login, the error "530 login incorrect.login failed" is reported

4000 word super detailed pointer

Summary of development issues

Is BigDecimal safe to calculate the amount? Look at these five pits~~

4000字超详解指针

Wechat applet - basic content
![[MySQL special] read lock and write lock](/img/ac/e01c26882cc664ea2e5e731c5a8bab.png)
[MySQL special] read lock and write lock
随机推荐
OpenGL index cache object EBO and lineweight mode
Adult adult adult
QT OpenGL texture map
ES6 standard
How to deploy web pages to Alibaba cloud
Wechat applet pages always report errors when sending values to the background. It turned out to be this pit!
(构造笔记)GRASP学习心得
typeScript
OPenGL 基本知识(根据自己理解整理)
PHP导出word方法(一mht)
[learning notes] DP status and transfer
Pki/ca and digital certificate
4000 word super detailed pointer
在网上炒股开户可以吗?资金安全吗?
2020-11_ Technical experience set
Fluent: Engine Architecture
Use of QT OpenGL camera
实现验证码验证
242. Effective letter heteronyms
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)