当前位置:网站首页>实现验证码验证
实现验证码验证
2022-07-03 11:32:00 【仲夏月二十八】
导入依赖
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
代码实现
@Controller
public class VerificationCodeController {
@Resource
private DefaultKaptcha defaultKaptcha;
@GetMapping("/common/kaptcha")
public void defaultKaptch(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws IOException {
byte[] captchaOutPutStream = null;
// 数组字节输出流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
//将生成的验证码保存到session中
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;
}
}
效果图
边栏推荐
- Qt OpenGL 纹理贴图
- DNS multi-point deployment IP anycast+bgp actual combat analysis
- DEJA_VU3D - Cesium功能集 之 053-地下模式效果
- Concurrent programming - singleton
- QT OpenGL texture map
- (構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
- Dart: self study system
- [combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
- Redis notes 01: Introduction
- C language improvement article (wchar_t) character type
猜你喜欢
Php Export word method (One MHT)
vulnhub之narak
Wechat applet - basic content
vulnhub之cereal
Shutter widget: centerslice attribute
小鹏 P7 撞护栏安全气囊未弹出,官方回应称撞击力度未达到弹出要求
During FTP login, the error "530 login incorrect.login failed" is reported
STL Tutorial 9 deep copy and shallow copy of container elements
Vulnhub narak
vulnhub之raven2
随机推荐
MySQL time zone solution
Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
PHP導出word方法(一mht)
Ripper of vulnhub
(构造笔记)ADT与OOP
Apprendre à concevoir des entités logicielles réutilisables à partir de la classe, de l'API et du cadre
Qt OpenGL相机的使用
Flutter Widget : Flow
Dart: about Libraries
Groovy测试类 和 Junit测试
vulnhub之Ripper
Introduction to the implementation principle of rxjs observable filter operator
OpenGL draws colored triangles
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
Oracle advanced (I) realize DMP by expdp impdp command
vulnhub之narak
Deploying WordPress instance tutorial under coreos
[official MySQL document] deadlock
Talk about the state management mechanism in Flink framework
PHP导出word方法(一mht)