当前位置:网站首页>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 .
边栏推荐
- Use of selectors
- All codes of Tetris
- Time series anomaly detection
- Use spiel expressions in custom annotations to dynamically obtain method parameters or execute methods
- 【Halcon视觉】编程逻辑
- Basics of data communication - basic knowledge of network
- Wechat applet learning notes 2
- PTA class a 1002
- Learning about opencv (2)
- PTA class a 1001
猜你喜欢
【Halcon视觉】数组
新建福厦铁路全线贯通 这将给福建沿海带来什么?
Applet record
Like, "new programmer" e-book is free for a limited time!
Production of a-modal drag function in antui
Employee information management system based on Web
Vs Code configures go locale and successfully installs go related plug-ins in vscode problem: Tools failed to install
Learning about tensorflow (I)
Meeting OA project (III) -- my meeting (meeting seating and submission for approval)
【Halcon视觉】编程逻辑
随机推荐
利用原生js实现自定义滚动条(可点击到达,拖动到达)
Data communication foundation TCPIP reference model
Like, "new programmer" e-book is free for a limited time!
MySQL function
在.NET 6.0中配置WebHostBuilder
【Halcon视觉】形态学膨胀
Study notes of the first week of sophomore year
Study notes of the fifth week of sophomore year
Yarn 'TSC' is not an internal or external command, nor is it a runnable program or batch file. The problem that the command cannot be found after installing the global package
Data communication foundation STP principle
Data communication foundation - layer 2 switching principle
数通基础-TCPIP参考模型
On the compilation of student management system of C language course (simple version)
Replay the snake game with C language (II) end
Error in render: "typeerror: cannot read properties of undefined (reading 'length')" --- error when calling interface
Use of tabbarcontroller
Use spiel expressions in custom annotations to dynamically obtain method parameters or execute methods
PTA class a 1001
Rowselection emptying in a-table
【杂谈】Error loading psycopg2 module :No module named psycopg2