Java QR code login process implementation code (including short address generation, including some codes)
In recent years, the use of two-dimensional code has become more and more popular. Recently, the author also encountered a job that needs to use two-dimensional code to scan the code to log in to the website, so I studied this set of mechanism and realized the whole process with code. Next, I'll talk about two-dimensional code login and.
Two dimensional code principle
The QR code was created by wechat. When wechat scanned the QR code and logged into wechat, it felt very magical. However, when we understood its principle, it was not so magical. The QR code actually contains a URL request information through a black-and-white dot matrix. Scan the code on the end, request the URL and do the corresponding operation.
Principle of general code scanning operation
WeChat login, Alipay scan code payment is the principle:
1. Request QR code
The desktop sends a QR code request to the server.
2. Generate a QR code with a unique ID
The desktop side will randomly generate an ID, which uniquely identifies the QR code for subsequent operations.
3. Code scanning on the end
The mobile terminal scans the QR code to solve Chu the URL request in the QR code.
4. The mobile terminal sends a request to the server
The mobile terminal sends a URL request to the server, which contains two information. The unique ID identifies which code is scanned, and the specific cookie or header parameter in the terminal browser will identify which user is scanning the code.
5. The server notifies that the code scanning is successful
When the server receives the URL request for the information in the QR code, it notifies the client that the code has been scanned successfully, and adds necessary login cookies and other information. There are generally several notification methods here: websocket, rotation hold request until timeout, and rotation training every few seconds.
The art of URL in QR code
How to realize the different performance of self owned clients and other clients (such as wechat)
For example, in business, you may want to do this. If your company's QR code is scanned by other apps (such as wechat), you want to jump to a prompt page, on which you can have an app download link; when it is scanned by your own app, you can directly make the corresponding request.
In this case, you can do this. All links in the QR code are encrypted in one layer, and then processed with another link.
For example: www.test.com com/qr? P = XXXXXX, the P parameter contains the encryption and decryption algorithm agreed between the server and the client (it can be symmetric or asymmetric). When the end scans the code to this specific path, directly use the decryption algorithm to solve the P parameter to obtain www.testqr com/qrcode? Key = s1arv, so you can make a request to the server. Because other clients do not know this rule, they can only directly request www.test com/qr? P = XXXXXX, this request returns to the prompt page.
How to make QR code easier
Most of the time, the horses have to run and not eat grass. You want the QR code with many parameters, but you don't want the QR code to be too complex to be scanned out. At this time, we need to consider how to simplify the QR code without affecting the business.
Sample code
Generate QR code (remove the white edge and add the logo in the middle)
You need to import the jar package: zxing's core-2.0 jar
Generate short links
Basic idea:
Theory of short URL mapping algorithm:
1. Add the long web address and random number, and use MD5 algorithm to generate a 32-bit signature string, which is divided into 4 segments with 8 characters each
2. Cycle these four segments, take 8 characters of each segment, treat them as hexadecimal string and 0x3ffffff (30 bit 1) bit and operation, and ignore those exceeding 30 bits
3. Divide the 30 bits obtained from each paragraph into 6 segments, use every 5 digits as the index of the alphabet to obtain specific characters, and then obtain a 6-bit string in turn;
4. Four 6-bit strings can be obtained from such an MD5 string, and any one of them can be used as the short URL address of the long URL.
5. It is better to use a key value database for storage. In case of collision, change one. If all four collide, regenerate MD5 (because there are random numbers, different MD5 will be generated)
Note: the core code is not original. It draws on the code of others. Thank you!
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.
