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

边栏推荐
- vulnhub之Ripper
- (构造笔记)GRASP学习心得
- 【mysql官方文档】死锁
- Introduction to the implementation principle of rxjs observable filter operator
- Socket TCP for network communication (I)
- win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
- Wechat applet - basic content
- Niuniu's team competition
- Download address and installation tutorial of vs2015
- 【mysql专项】读锁和写锁
猜你喜欢

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

vulnhub之Ripper

【mysql官方文档】死锁

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

Vulnhub's Tomato (tomato)

vulnhub之presidential

Socket TCP for network communication (I)

Shardingsphere sub database and sub table < 3 >

Duplicate numbers in the array of sword finger offer 03

Xiaopeng P7 hit the guardrail and the airbag did not pop up. The official responded that the impact strength did not meet the ejection requirements
随机推荐
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
836. Merge sets (day 63) and search sets
vulnhub之momentum
Shardingsphere sub database and sub table < 3 >
安装electron失败的解决办法
Keepalived中Master和Backup角色选举策略
Groovy测试类 和 Junit测试
Flutter Widget : Flow
laravel 时区问题timezone
previous permutation lintcode51
Redis 笔记 01:入门篇
PHP export word method (phpword)
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
OpenGL shader use
PHP get the file list and folder list under the folder
Ripper of vulnhub
Vulnhub's Nagini
vulnhub之GeminiInc v2
Flutter: about monitoring on flutter applications
Niuniu's team competition