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

边栏推荐
- laravel 时区问题timezone
- Visual studio 2022 downloading and configuring opencv4.5.5
- AOSP ~ NTP (Network Time Protocol)
- Display time with message interval of more than 1 minute in wechat applet discussion area
- (construction notes) learning experience of MIT reading
- 225. Implement stack with queue
- Redis 笔记 01:入门篇
- PHP export word method (one MHT)
- C language improvement article (wchar_t) character type
- Adult adult adult
猜你喜欢
随机推荐
网络通讯之Socket-Tcp(一)
PHP导出word方法(一phpword)
Swagger
Shell: basic learning
Laravel time zone timezone
242. Effective letter heteronyms
previous permutation lintcode51
4000字超详解指针
Integer int compare size
C language improvement article (wchar_t) character type
PHP導出word方法(一mht)
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
Itext7 uses iexternalsignature container for signature and signature verification
Prompt unread messages and quantity before opening chat group
Dart: about grpc (I)
抓包整理外篇fiddler———— 会话栏与过滤器[二]
Redis 笔记 01:入门篇
repo Manifest Format
【附下载】密码获取工具LaZagne安装及使用
Pki/ca and digital certificate









