当前位置:网站首页>Kaptcha image verification code integration
Kaptcha image verification code integration
2022-07-26 10:18:00 【CS beat you】
technological process : Generate pictures ----> Put the verification code data into session in ----> Login interface for comparison .
Here to session Explain : When the server is getSession(true) Created on Session At the same time , The server will be Session Generate unique Session id, And this Session id It will be used in subsequent requests to retrieve the created Session; stay Session After being created , You can call Session Related methods Session Added to , And that content will only be stored on the server , Only Session id; When the client sends the request again , Will take this. Session id close , After the server receives the request, it will base on Session id Find the appropriate Session, So that it can be used again . So it's safe .
1, Import dependence
<!-- Verification Code -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
</dependency>2, You can put bean Inject Spring in , When you want to use it Autowired Use .
/**
* Verification code generation related
*/
@Bean
public DefaultKaptcha kaptcha() {
Properties properties = new Properties();
properties.put("kaptcha.border", "no");
properties.put("kaptcha.border.color", "105,179,90");
properties.put("kaptcha.textproducer.font.color", "blue");
properties.put("kaptcha.image.width", "125");
properties.put("kaptcha.image.height", "45");
properties.put("kaptcha.textproducer.font.size", "45");
properties.put("kaptcha.session.key", "code");
properties.put("kaptcha.textproducer.char.length", "4");
properties.put("kaptcha.textproducer.font.names", " Song style , Regular script , Microsoft YaHei ");
Config config = new Config(properties);
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}3, Provide an interface for generating pictures , And save the verification code into session in , Then compare the login interface .
@Autowired
private Producer producer;
/**
* Generate verification code
*/
@RequestMapping("/verificationCode")
public void index(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
response.setDateHeader("Expires", 0);
// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");
// return a jpeg
response.setContentType("image/jpeg");
// create the text for the image
String capText = producer.createText();
// store the text in the session
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
// create the image with the text
BufferedImage bi = producer.createImage(capText);
ServletOutputStream out = null;
try {
out = response.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
// write the data out
try {
ImageIO.write(bi, "jpg", out);
} catch (IOException e) {
e.printStackTrace();
}
try {
try {
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}4, Just compare .
This article has references to other frameworks , any similarity , It was an accident .
边栏推荐
- PTA class a 1001
- Session based recommendations with recurrent neural networks
- protobuf的基本用法
- modelsim 安装教程(应用未安装)
- [qualcomm][network] QTI service analysis
- INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误解决方式
- Nacos custom service change subscription
- The CLOB field cannot be converted when querying Damon database
- Force deduction DFS
- Like, "new programmer" e-book is free for a limited time!
猜你喜欢
随机推荐
The fourth week of summer vacation
Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills
Nacos custom service change subscription
Cause: couldn‘t make a guess for 解决方法
Flask framework beginner-03-template
服务器内存故障预测居然可以这样做!
3.1 leetcode daily question 6
Flask框架初学-04-flask蓝图及代码抽离
Uniapp error 7 < Map >: marker ID should be a number
Dynamically determine file types through links
C language course design Tetris (Part 1)
What will the new Fuzhou Xiamen railway bring to Fujian coastal areas?
时间序列异常检测
Wechat applet learning notes 2
面试第二家公司的面试题及答案(二)
面试第一家公司的面试题及答案(一)
Vs2019 configuring opencv
The reason why go language is particularly slow to develop run and build commands
Tableviewcell highly adaptive
IEEE conference upload font problem









