当前位置:网站首页>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 .
边栏推荐
- Flask framework beginner-03-template
- Basic usage of protobuf
- Formwork (III)
- 3.1 leetcode daily question 6
- Study notes of the first week of sophomore year
- PLC概述
- 【Halcon视觉】极坐标变换
- 30 minutes to thoroughly understand the synchronized lock upgrade process
- Solution of inputting whole line string after inputting integer
- The charm of SQL optimization! From 30248s to 0.001s
猜你喜欢

Flask框架初学-03-模板

Meeting OA project (III) -- my meeting (meeting seating and submission for approval)

Review of database -- 1. Overview

How to write a million reading article

Uniapp "no mobile phone or simulator detected, please try again later" and uniapp custom components and communication

Okaleido ecological core equity Oka, all in fusion mining mode

服务器内存故障预测居然可以这样做!
![Uniapp common error [wxml file compilation error]./pages/home/home Wxml and using MySQL front provided by phpstudy to establish an independent MySQL database and a detailed tutorial for independent da](/img/21/5dceab9815b503f0b62d26a430d7eb.png)
Uniapp common error [wxml file compilation error]./pages/home/home Wxml and using MySQL front provided by phpstudy to establish an independent MySQL database and a detailed tutorial for independent da

Dynamically determine file types through links

Flask框架初学-04-flask蓝图及代码抽离
随机推荐
The practice of OpenCV -- bank card number recognition
数据库的复习--3.SQL语言
Distributed network communication framework: how to publish local services into RPC services
equals与==的区别
Production of a-modal drag function in antui
Uniapp "no mobile phone or simulator detected, please try again later" and uniapp custom components and communication
Cause: could't make a guess for solution
Uniapp error 7 < Map >: marker ID should be a number
Leetcode 504. 七进制数
Transform between tree and array in JS (hide the children field if the child node of the tree is empty)
Vs Code configures go locale and successfully installs go related plug-ins in vscode problem: Tools failed to install
protobuf的基本用法
汉诺塔II|汉诺塔4柱
Map key not configured and uniapp routing configuration and jump are reported by the uniapp < map >< /map > component
The fourth week of summer vacation
The problem of incomplete or partial display of the last recyclerview is solved
How to use Gmail to pick up / send mail on Foxmail
Study on the basis of opencv
Study notes of the third week of sophomore year
About automatic operation on Web pages