Requirements describe

Usually, when we log in, in order to prevent multiple attempts or attacks on the login interface, we need to create a verification code function. Only when the verification code is correct, we will do password verification, which reduces the possibility that the password may be tested.

Demand analysis

There are two main points of this feature design:

The first point, the generation of verification code picture

Captcha images are generated using IO streams, and many captcha generation tools on the web, such as awT Graphics2D, generate captcha images. But if you want to adjust the number of verification code, style, and so on that is a bit of trouble. Here is an open source captcha component com.google.code. Kaptcha. You can adjust the style of the verification code flexibly.

Second, the storage method of verification code

If it is a single project, it can be stored in a session. If shiro is integrated, it can also be stored in Shiro’s session.

In distributed system, the sharing function of verification code needs to be considered.

1. It can be stored in session. If spring session needs to be integrated, it can be stored in the session verification code sharing function in storage middleware such as Redis.

Shiro integrated with Redis can be stored in Shiro Session for sharing.

3. Of course, you can also store captcha directly in middleware such as Redis. There is no need for session, but the key must be unique.

Pseudocode logic

1. Introduce poM coordinates for com.google.code.kaptcha

2. Generate the configuration information and configuration style of the verification code

3. Invoke the producer interface to generate verification codes and pictures and save them in the session

4, use the servlet output stream captcha image

5. Upload the verification code to the client

** Suggestion: ** can define a captcha switch, which can be turned off during development to improve efficiency.

Code implementation

Step 1: Import the POM package of the verification code component

Step 2. Generate the verification code configuration

Step 3. Generate a captcha image and store the captcha in session

The fourth step is to verify whether the verification code is correct

At this point, the verification code function is complete

(after)

Recommended reading

IDEA2020.2.3 crack, IDEA2020.2 activation crack, IDEA activation code

Great, this Java site, everything! https://markerhub.com

The UP master of this B station, speaks Java really good!

B station 150 thousand playback, SpringBoot+Vue front and back end separation complete introductory tutorial!

Too great! The latest edition of Java programming ideas can be viewed online!

The most frequently asked business interview questions of 2021 and the answers