Web API access control and data encryption method and device
Through full-process signature and data encryption of Web API requests and responses, Web API security risks and data privacy issues are solved, access control and data encryption of Web APIs are realized, and the risk of data leakage is significantly reduced.
Patent Information
- Application Number
- CN202410986905.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-23
- Publication Date
- 2024-10-01
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
Web APIs have security risks and data privacy issues. Attackers may use APIs without sufficient security verification to obtain sensitive information, and unencrypted data can easily be intercepted by third parties during transmission.
By performing full-process signing and data encryption on Web API requests and responses, using the RSA encryption algorithm to encrypt and decrypt query parameters and request bodies, and using signature keys and public keys for signature verification between the client and the server, data is ensured. Transmission security.
Significantly improves the security of data access and transmission, greatly reduces the risk of data leakage, implements access control and data encryption for Web API, and prevents unauthorized access and data leakage.
Smart Images

Figure CN118740485A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of Internet technology, and in particular to a method and device for Web API access control and data encryption. Background Art
[0002] Web API is a set of APIs based on HTTP(s) protocol, which is usually provided by a web server. Users can use Web API to obtain data or share data with other applications.
[0003] Currently, Web APIs also have some disadvantages, such as security risks and data privacy issues. For example, if the API has not been adequately verified, attackers may use the API to obtain data. If the API response contains sensitive information and there is no proper encryption or desensitization measures, this information may be intercepted by a third party. Summary of the invention
[0004] The technical task of the present invention is to address the above shortcomings and provide a method and device for Web API access control and data encryption, which can not only perform access control on Web API, but also encrypt Web API data. By signing and encrypting the entire process of Web API request and response, the security of data access and transmission can be improved, greatly reducing the risk of data leakage.
[0005] The technical solution adopted by the present invention to solve its technical problem is:
[0006] A method for Web API access control and data encryption, the method comprising:
[0007] The preset client assembles a Web API request, the request URL or header contains at least a unique identity identifier and a signature, and the query parameters or request body of the Web API request are RSA encrypted;
[0008] Send a Web API request to the preset server;
[0009] When the server receives the Web API request from the client, it determines whether the unique identifier of the client exists in the preset local database. If not, it returns an access failure to the client. If so, it caches the request parameters and body in the filter.
[0010] The interceptor queries the signature key, RSA public key, and RSA private key corresponding to the client from the local database according to the unique identity identifier, and uses the signature key to check whether the signature is correct. If the signature is incorrect, it returns authentication failure to the client. If the signature is correct, it uses the RSA private key to decrypt the query parameter or request body data to obtain plaintext request data;
[0011] According to the plaintext request data, the business logic is processed in the server, and the plaintext response data is returned. The interceptor intercepts the response data, encrypts the plaintext response data using the RSA public key, signs the response header using the signature key, and returns the encrypted data and the signature to the client;
[0012] The client receives the data and signature returned by the server, checks the signature, and if the signature is correct, uses the RSA private key to decrypt the response data to obtain the plaintext response data.
[0013] By signing and encrypting the entire process of Web API requests and responses, the security of data access and transmission can be improved, greatly reducing the risk of data leakage.
[0014] Furthermore, the preset client assembles the Web API request, including assembling the request URL, request header, request body, and request method; the signature is a string encrypted with HMAC-SHA1 and encoded with base64, and the specific algorithm is as follows:
[0015] Signature=base64(HMAC-SHA1(AccessKeySecret,VERB+"\n"+IdentityId+"\n"+Date+"\n"+CanonicalizedResource));
[0016] Among them, AccessKeySecret indicates the secret key required for signature; VERB indicates the Method of HTTP request; IdentityId indicates the unique identity identifier; Date is the request timestamp; CanonicalizedResource indicates the resource that the client wants to access.
[0017] Furthermore, if the request method is get, the request timestamp, unique identity identifier and the signature are concatenated to the request URL; if the request method is not get, such as post, put, delete, etc., the request timestamp, unique identity identifier and the signature are placed in the request header;
[0018] RSA encryption of the query parameters or request body of the API request:
[0019] If the request is a get request, sort the query parameters in alphabetical order, encrypt them using the RSA public key, and then concatenate them to the request URL; if the request is not a get request, convert the business parameters into a json string, encrypt them using the RSA public key, and write them into the request body.
[0020] Furthermore, the server is a WEB server that can receive WebAPI requests of http protocol or https protocol;
[0021] After receiving the Web API request, the server parses the unique identity identifier from the request URL or header, and queries the local database based on the unique identity identifier to see whether the unique identity identifier has been registered. If the query does not exist, the client cannot access the Web API and returns access failure; if it does, the request parameters and body are cached in the filter. The filter can intercept the request. After the request is intercepted, the request parameters and body are parsed, and the request parameters and body strings are stored in the memory for reuse.
[0022] Furthermore, after intercepting the Web API request, the interceptor queries the signature key, RSA public key, and RSA private key corresponding to the client from the local database according to the unique identity identifier, obtains the request timestamp and CanonicalizedResource from the cache, and calculates the signature using the same signature algorithm as the client. The obtained signature is compared with the signature calculated by the client. If they are not equal, the signature is incorrect and authentication failure is returned to the client; if the signature is correct, the RSA private key is used to decrypt the query parameters or request body data to obtain the plaintext request data.
[0023] Furthermore, the plain text response data is converted into a JSON string, the JSON string is encrypted using the RSA public key, and the ciphertext is written into the response body of the Web API; the response header is signed using the signature key. The specific signature algorithm used is as follows:
[0024] Signature=base64(HMAC-SHA1(AccessKeySecret,VERB+"\n"+IdentityId+"\n"
[0025] +Date+"\n"+CanonicalizedResource));
[0026] Among them, AccessKeySecret indicates the secret key required for signature; VERB indicates the HTTP request method; IdentityId indicates the unique identity identifier; Date is the response timestamp; CanonicalizedResource indicates the resource that the client wants to access;
[0027] Write the signature into the response header.
[0028] Furthermore, the client receives the response of the Web API, calculates the signature using the same signature algorithm as the signature of the server response header, compares the obtained signature with the signature calculated by the server, and returns authentication failure if the signatures are not equal; if the signature is correct, uses the RSA private key to decrypt the string in the response body, converts the decrypted data into a json string, and obtains the plaintext response data.
[0029] The present invention also claims a device for Web API access control and data encryption, comprising:
[0030] The client application is used to sign and encrypt the request parameters and body, assemble the Web API request URL, request header, request body, and request method, and send the Web API to the web server; it is used to receive the Web API, check the response parameters and body signature, and decrypt the response data;
[0031] The filter unit is used to cache request parameters and request body and is executed before the interceptor unit.
[0032] The interceptor unit can intercept both requests and responses. It is executed after the filter unit and before the business logic unit to intercept request data, verify signature correctness, and decrypt request data. After the business logic unit, it signs and encrypts response data.
[0033] Business logic unit, used to process business logic and return plain text data;
[0034] The database unit is used to store unique identity identifiers, signature keys, RSA public keys, and private keys.
[0035] The present invention also claims a device for Web API access control and data encryption, comprising: at least one memory and at least one processor;
[0036] The at least one memory is used to store a machine-readable program;
[0037] The at least one processor is used to call the machine-readable program to implement the above method.
[0038] The present invention also claims protection for a computer-readable medium having computer instructions stored thereon, which, when executed by a processor, cause the processor to perform the above-mentioned method.
[0039] Compared with the prior art, the method and device for Web API access control and data encryption of the present invention have the following beneficial effects:
[0040] The present invention can solve the problems of Web API security risks and easy data leakage in the prior art, and can not only perform access control on Web API, but also encrypt Web API data. By signing and encrypting the entire process of Web API request and response, the security of data access and transmission can be improved, greatly reducing the risk of data leakage. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] Figure 1 It is a flowchart of a method for Web API access control and data encryption provided by an embodiment of the present invention;
[0042] Figure 2 It is a schematic diagram of the method architecture of Web API access control and data encryption provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0043] The present invention will be further described below in conjunction with the accompanying drawings and specific embodiments.
[0044] An embodiment of the present invention provides a method for Web API access control and data encryption, the method comprising:
[0045] 1. The preset client assembles a Web API request, and the request URL or header contains at least a unique identity identifier and a signature, and the query parameters or request body of the Web API request are RSA encrypted. Specifically, the preset client assembles a Web API request, including assembling a request URL, a request header, a request body, and a request method; the signature is a string encrypted with HMAC-SHA1 and encoded with base64, and the specific algorithm is as follows:
[0046] Signature=base64(HMAC-SHA1(AccessKeySecret,VERB+"\n"+IdentityId+"\n"+Date+"\n"+CanonicalizedResource));
[0047] Among them, AccessKeySecret indicates the secret key required for signature; VERB indicates the Method of HTTP request; IdentityId indicates the unique identity identifier; Date is the request timestamp; CanonicalizedResource indicates the resource that the client wants to access.
[0048] If the request method is get, the request timestamp, unique identity identifier and the signature are concatenated to the request URL; if the request method is not get, such as post, put, delete, etc., the request timestamp, unique identity identifier and the signature are placed in the request header;
[0049] RSA encryption of the query parameters or request body of the API request:
[0050] If the request is a get request, sort the query parameters in alphabetical order, encrypt them using the RSA public key, and then concatenate them to the request URL; if the request is not a get request, convert the business parameters into a json string, encrypt them using the RSA public key, and write them into the request body.
[0051] 2. Send a Web API request to a preset server; the server is a WEB server that can receive Web API requests using the http protocol or the https protocol.
[0052] 3. If the server receives the Web API request from the client, it determines whether the unique identity identifier of the client exists in the preset local database. If not, it returns an access failure to the client. If so, it caches the request parameters and body in the filter. Specifically, after receiving the Web API request, the server parses the unique identity identifier from the request URL or header, and queries the local database based on the unique identity identifier to see whether the unique identity identifier has been registered. If the query does not exist, the client cannot access the Web API and returns an access failure. If so, it caches the request parameters and body in the filter. The filter can intercept the request. After the request is intercepted, it parses the request parameters and body, and stores the request parameters and body strings in the memory for reuse.
[0053] 4. The interceptor queries the signature key, RSA public key, and RSA private key corresponding to the client from the local database according to the unique identity identifier, and uses the signature key to check whether the signature is correct. If the signature is incorrect, an authentication failure is returned to the client. If the signature is correct, the query parameter or request body data is decrypted using the RSA private key to obtain the plaintext request data. Specifically, after the interceptor intercepts the Web API request, it queries the signature key, RSA public key, and RSA private key corresponding to the client from the local database according to the unique identity identifier, obtains the request timestamp and CanonicalizedResource from the cache, calculates the signature using the same signature algorithm as the client, and compares the obtained signature with the signature calculated by the client. If they are not equal, the signature is incorrect and an authentication failure is returned to the client. If the signature is correct, the query parameter or request body data is decrypted using the RSA private key to obtain the plaintext request data.
[0054] 5. According to the plaintext request data, the business logic is processed in the server, and the plaintext response data is returned. The interceptor intercepts the response data, encrypts the plaintext response data using the RSA public key, signs the response header using the signature key, and returns the encrypted data and signature to the client. Specifically, the plaintext response data is converted into a json string, the json string is encrypted using the RSA public key, and the ciphertext is written into the response body of the Web API; the response header is signed using the signature key. The specific signature algorithm used is as follows:
[0055] Signature=base64(HMAC-SHA1(AccessKeySecret,VERB+"\n"+IdentityId+"\n"
[0056] +Date+"\n"+CanonicalizedResource));
[0057] Among them, AccessKeySecret indicates the secret key required for signature; VERB indicates the HTTP request method; IdentityId indicates the unique identity identifier; Date is the response timestamp; CanonicalizedResource indicates the resource that the client wants to access;
[0058] Write the signature into the response header.
[0059] 5. The client receives the data and signature returned by the server, checks the signature, and if the signature is correct, uses the RSA private key to decrypt the response data to obtain the plaintext response data. Specifically, the client receives the response from the WebAPI, calculates the signature using the same signature algorithm as the signature of the server response header, compares the obtained signature with the signature calculated by the server, and returns an authentication failure if the signatures are not equal; if the signature is correct, uses the RSA private key to decrypt the string in the response body, converts the decrypted data into a json string, and obtains the plaintext response data. The flow chart of this method is as follows: Figure 1 shown.
[0060] By signing and encrypting the entire process of Web API requests and responses, the security of data access and transmission can be improved, greatly reducing the risk of data leakage.
[0061] The embodiment of the present invention also requires protection of a device for Web API access control and data encryption, including:
[0062] The client application is used to sign and encrypt request parameters and body, assemble the Web API request URL, request header, request body, and request method, and send the Web API to the web server; it is used to receive the Web API, check the response parameters and body signature, and decrypt the response data.
[0063] The filter unit is used to cache request parameters and request body and is executed before the interceptor unit.
[0064] The interceptor unit can intercept both requests and responses. It is executed after the filter unit and before the business logic unit. It is used to intercept request data, verify the correctness of the signature, and decrypt the request data. After the business logic unit, it signs and encrypts the response data.
[0065] The business logic unit is used to process business logic and return plain text data.
[0066] The database unit is used to store unique identity identifiers, signature keys, RSA public keys, and private keys.
[0067] The device can implement the Web API access control and data encryption methods described in the above embodiments.
[0068] in,
[0069] The client application is specifically used for:
[0070] Include a unique identity identifier and signature in the request URL or header, and perform RSA encryption on the query parameters or request body of the API request;
[0071] Receive the Web API response and check the signature. If the signature is correct, use the RSA private key to decrypt the response data to obtain the plaintext response data.
[0072] The interceptor unit is specifically used for:
[0073] According to the unique identity identifier, the signature key, RSA public key, and RSA private key corresponding to the client are queried from the local database, and the signature key is used to check whether the signature is correct;
[0074] If the signature is incorrect, an authentication failure is returned to the client; if the signature is correct, the query parameter or request body data is decrypted using the RSA private key to obtain the plaintext request data.
[0075] The filter unit is specifically used for:
[0076] Cache request parameters and request body, executed before interceptor unit.
[0077] An embodiment of the present invention also provides a device for Web API access control and data encryption, comprising: at least one memory and at least one processor;
[0078] The at least one memory is used to store a machine-readable program;
[0079] The at least one processor is used to call the machine-readable program to implement the WebAPI access control and data encryption method described in the above embodiment.
[0080] The embodiment of the present invention further provides a computer-readable medium, on which computer instructions are stored, and when the computer instructions are executed by a processor, the processor executes the Web API access control and data encryption method described in the above embodiment. Specifically, a system or device equipped with a storage medium can be provided, on which software program codes implementing the functions of any of the above embodiments are stored, and a computer (or CPU or MPU) of the system or device reads and executes the program code stored in the storage medium.
[0081] In this case, the program code itself read from the storage medium can realize the function of any one of the above-mentioned embodiments, and thus the program code and the storage medium storing the program code constitute a part of the present invention.
[0082] The storage medium embodiments for providing the program code include a floppy disk, a hard disk, a magneto-optical disk, an optical disk (such as CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-RAM, DVD-RW, DVD+RW), a magnetic tape, a non-volatile memory card, and a ROM. Alternatively, the program code can be downloaded from a server computer by a communication network.
[0083] In addition, it should be clear that the functions of any of the above embodiments can be implemented not only by executing the program code read by the computer, but also by enabling an operating system operating on the computer to complete part or all of the actual operations based on instructions from the program code.
[0084] In addition, it can be understood that the program code read from the storage medium is written to a memory provided in an expansion board inserted into the computer or written to a memory provided in an expansion unit connected to the computer, and then based on the instructions of the program code, a CPU installed on the expansion board or the expansion unit is enabled to perform part or all of the actual operations, thereby realizing the functions of any of the above-mentioned embodiments.
[0085] The present invention is shown and described in detail above through the accompanying drawings and preferred embodiments. However, the present invention is not limited to these disclosed embodiments. Based on the above multiple embodiments, those skilled in the art can know that the code review methods in the above different embodiments can be combined to obtain more embodiments of the present invention, and these embodiments are also within the protection scope of the present invention.
Claims
1. A method for Web API access control and data encryption, It is characterized in that The method includes: The preset client assembles a Web API request, the request URL or header contains at least a unique identity identifier and a signature, and the query parameters or request body of the Web API request are RSA encrypted; Send a Web API request to the preset server; When the server receives the Web API request from the client, it determines whether the unique identifier of the client exists in the preset local database. If not, it returns an access failure to the client. If so, it caches the request parameters and body in the filter. The interceptor queries the signature key, RSA public key, and RSA private key corresponding to the client from the local database according to the unique identity identifier, and uses the signature key to check whether the signature is correct. If the signature is incorrect, it returns authentication failure to the client. If the signature is correct, it uses the RSA private key to decrypt the query parameter or request body data to obtain plaintext request data; According to the plaintext request data, the business logic is processed in the server, and the plaintext response data is returned. The interceptor intercepts the response data, encrypts the plaintext response data using the RSA public key, signs the response header using the signature key, and returns the encrypted data and the signature to the client; The client receives the data and signature returned by the server, checks the signature, and if the signature is correct, uses the RSA private key to decrypt the response data to obtain the plaintext response data.
2. A Web API access control and data encryption method according to claim 1, It is characterized in that The preset client assembles the Web API request, including assembling the request URL, request header, request body, and request method; the signature is a string encrypted with HMAC-SHA1 and encoded with base64. The specific algorithm is as follows: Signature=base64(HMAC-SHA1(AccessKeySecret,VERB+"\n"+IdentityId+"\n"+Date+"\n"+CanonicalizedResource)); Among them, AccessKeySecret indicates the secret key required for signature; VERB indicates the Method of HTTP request; IdentityId indicates the unique identity identifier; Date is the request timestamp; CanonicalizedResource indicates the resource that the client wants to access.
3. A Web API access control and data encryption method according to claim 2, It is characterized in that If the request method is get, the request timestamp, unique identity identifier and the signature are concatenated to the request URL; if the request method is not get, the request timestamp, unique identity identifier and the signature are placed in the request header; RSA encryption of the query parameters or request body of the API request: If the request is a get request, sort the query parameters in alphabetical order, encrypt them using the RSA public key, and then concatenate them to the request URL; if the request is not a get request, convert the business parameters into a json string, encrypt them using the RSA public key, and write them into the request body.
4. A Web API access control and data encryption method according to claim 1 or 2, It is characterized in that The server is a WEB server that can receive Web API requests using the http protocol or the https protocol; After receiving the Web API request, the server parses the unique identity identifier from the request URL or header, and queries whether the unique identity identifier has been registered in the local database based on the unique identity identifier. If the query does not exist, the client cannot access the Web API and returns access failure; if it does, the request parameters and body are cached in the filter. The filter can intercept the request. After the request is intercepted, the request parameters and body are parsed, and the request parameters and body strings are stored in the memory.
5. A Web API access control and data encryption method according to claim 2, It is characterized in that After intercepting the Web API request, the interceptor queries the signature key, RSA public key, and RSA private key corresponding to the client from the local database according to the unique identity identifier, obtains the request timestamp and CanonicalizedResource from the cache, calculates the signature using the same signature algorithm as the client, and compares the obtained signature with the signature calculated by the client. If they are not equal, the signature is incorrect and an authentication failure is returned to the client; If the signature is correct, the query parameter or request body data is decrypted using the RSA private key to obtain the plaintext request data.
6. A Web API access control and data encryption method according to claim 2, It is characterized in that Convert the plain text response data into a json string, encrypt the json string using the RSA public key, and write the ciphertext into the response body of the Web API; use the signature key to sign the response header. The specific signature algorithm used is as follows: Signature=base64(HMAC-SHA1(AccessKeySecret,VERB+"\n"+IdentityId+"\n" +Date+"\n"+CanonicalizedResource)); Among them, AccessKeySecret indicates the secret key required for signature; VERB indicates the HTTP request method; IdentityId indicates the unique identity identifier; Date is the response timestamp; CanonicalizedResource indicates the resource that the client wants to access; Write the signature into the response header.
7. A Web API access control and data encryption method according to claim 2, It is characterized in that The client receives the response from the Web API, calculates the signature using the same signature algorithm as the signature of the server response header, compares the obtained signature with the signature calculated by the server, and returns authentication failure if the signatures are not equal; If the signature is correct, use the RSA private key to decrypt the string in the response body, convert the decrypted data into a JSON string, and get the plaintext response data.
8. A device for Web API access control and data encryption, It is characterized in that include: The client application is used to sign and encrypt the request parameters and body, assemble the Web API request URL, request header, request body, request method, and send the Web API to the web server; Used to receive Web API, check response parameters and body signatures, and decrypt response data; The filter unit is used to cache request parameters and request body and is executed before the interceptor unit. The interceptor unit can intercept both requests and responses. It is executed after the filter unit and before the business logic unit to intercept request data, verify signature correctness, and decrypt request data. After the business logic unit, it signs and encrypts response data. Business logic unit, used to process business logic and return plain text data; The database unit is used to store unique identity identifiers, signature keys, RSA public keys, and private keys.
9. A device for Web API access control and data encryption, It is characterized in that include: at least one memory and at least one processor; The at least one memory is used to store a machine-readable program; The at least one processor is used to call the machine-readable program to implement the method described in any one of claims 1 to 7.
10. Computer readable media, It is characterized in that The computer readable medium stores computer instructions, and when the computer instructions are executed by a processor, the processor executes the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Method and system for carrying out encryption and decryption on API service data on transmission layer
CN107222473A
Data security transmission method and device based on HTTP
CN108712388A
Access control method and device, gateway and console
CN112165454A
Data security interface based on asymmetric and symmetric encryption and working method thereof
CN114143026A
Interface access security control method and system based on hybrid encryption
CN116471008A
