Web front end
- A random token is generated every time an AJAX Web request (including POST and GET) is sent.
- The token is put into the Header of each AJAX request with the name of HALO_TOKEN and sent to the server as part of the HTTP packet.
- CSRF risk warning for special error code (0008) of server response.
Token creation
-
Read the SEED value. SEED can be either a sessionId or a fixed value. SEED suggests 24 bits, not enough to fill up after 0.
-
Before each server request is sent, a random reversible plaintext is generated based on the SEED and the number of milliseconds of the current client system time.
- Randomly select 5 digits within the length of the SEED text.
- Combine the character contents of each number in the SEED with the number to form a key-value pair. Key-value pairs are separated by ‘:’.
- Concatenate all key-value pairs by ‘,’. Form long strings of text.
- Gets the number of milliseconds of the current client system time, adding it to the end of the text with ‘_’ to form clear text.
-
Use the 3DES algorithm to encrypt the plaintext generated in Step 2 using the SEED as the key to form ciphertext.
-
Use Base64 to encode the ciphertext to form a token.
SEED gets and sets
Front-end application developers can get a valid SEED in two ways:
SessionID as a SEED
- After the user logs in successfully, the front-end program accesses “/web/ sessionID “and obtains the current user sessionID from the data field of the response packet.
- Note: Put the sessionId in the localStorage of the browser.
localStorage.setItem("_HALOSESSIONID", sessionId);
Copy the code
Fixed the SEED setting
- After the user visits the website for the first time, JS will put the fixed SEED value agreed with the server developer into localStorage.
Note: Fixing the SEED value requires coordination with the server side. The configurations of the two parties must be consistent.
The service side
- Intercepts all SpringMVC requests.
- Get the token value from header’s ‘HALO_TOKEN’, or raise (0008) if not.
- Decrypt and verify token content.
- Depending on the decryption validation, the corresponding business logic is executed or a (0008) error is returned.
Token decryption and authentication
- Gets the SEED for the current request.
- Perform Base64 decoding for the token to restore the ciphertext.
- The 3DES algorithm is used to decrypt the ciphertext using SEED as the key.
- Verify the decrypted token content
- If the decrypted content is separated by ‘_’, the Hash content in the first half is authenticated. Requirements must be consistent with the character distribution of the SEED.
- The difference between the second half of ‘_’ and the current server time in milliseconds must not be greater than N x 60000. N indicates minutes.
SEED reading strategy
The backend framework tries to get the SEED value in one of two ways. If the first method is unsuccessful, the second method is tried, and if neither method is valid, an error is reported.
- First try accessing the configuration item web.csrf.token.seed, and if it reads a non-null value, treat it as seed.
- If the first approach fails, the sessionId corresponding to the current request is directly fetched as the SEED.