当前位置:网站首页>实现验证码验证
实现验证码验证
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;
}
}
效果图

边栏推荐
猜你喜欢
随机推荐
Unicode encoding table download
php 获取文件夹下面的文件列表和文件夹列表
vulnhub之tomato(西红柿)
typeScript
Flutter: self study system
023 ([template] minimum spanning tree) (minimum spanning tree)
4000 word super detailed pointer
Talk about the state management mechanism in Flink framework
Dart: self study system
Visual studio 2022 downloading and configuring opencv4.5.5
Test classification in openstack
(construction notes) learning experience of MIT reading
OpenGL 索引缓存对象EBO和线宽模式
Basic knowledge of OpenGL (sort it out according to your own understanding)
ES6新特性
New features of ES6
OPenGL 基本知识(根据自己理解整理)
Dart: about Libraries
Raven2 of vulnhub
(构造笔记)MIT reading部分学习心得









