Try It
Okay, bosses, here it is, the configuration is completed, and then the effect experience can be carried out. The demo of the front and back ends will be provided when there is time later.
WARNING
This article is just to experience the function first, please be sure to be the same as my code, otherwise you may encounter some problems. The annotations used in the code will be introduced in subsequent pages to solve your doubts.
Launch project
When the enable
of SecureApi is set to true
, the console will print the following information, indicating that the interface plus decipher function is enabled. I did not specify the key here, so the component automatically generates it for me. Then you can set the key to the front end or negotiate a key with the front end to pursue a more secure transmission.
It is recommended to use CipherUtils
to manually set the key during testing, and you can specify seed
to ensure that the generated key is the same every time, which is more convenient.
![Launch log](/secure-api-doc/assets/%E5%90%8E%E7%AB%AFDemo%E5%90%AF%E5%8A%A8%E6%89%93%E5%8D%B0%E4%BF%A1%E6%81%AF-CoPip-2N.png)
Return value encryption
We did not match the configuration url before. We need to add the @EncryptApi
annotation on the interface or the class where the interface is located to implement the return value encryption.
![Return value encryption interface](/secure-api-doc/assets/%E5%90%8E%E7%AB%AF%E6%B5%8B%E8%AF%95%E8%BF%94%E5%9B%9E%E5%80%BC%E5%8A%A0%E5%AF%86%E6%8E%A5%E5%8F%A3-CSyM-bhc.png)
You can see that since I turned on the log printing function, the console prints out some information.
![Return value encryption log](/secure-api-doc/assets/%E5%90%8E%E7%AB%AF%E6%B5%8B%E8%AF%95%E8%BF%94%E5%9B%9E%E5%80%BC%E5%8A%A0%E5%AF%86-dra7yjl1.png)
The interface returns a json character string, and then the front-end uses the corresponding key to decipher the character string (note that this is a json character string, the front-end processing should remove the quotes at the front and rear ends) to get {"code":200,"message":"哈哈哈","data":null}
object.
![Interface return value ciphertext](/secure-api-doc/assets/%E5%90%8E%E7%AB%AF%E6%B5%8B%E8%AF%95%E8%BF%94%E5%9B%9E%E5%80%BC%E5%8A%A0%E5%AF%86%E6%8E%A5%E5%8F%A3%E8%BF%94%E5%9B%9E%E5%80%BC-IexkCdvD.png)
Parameter decrypt
Body parameter decrypt
Component can decipher the json parameter body. This time we pass in the encrypted return value in the previous step to take a look at the decipher result. The interface needs to add the @DecryptApi
annotation, so that this interface will not only decipher the parameter, but also encrypt the return value.
![Parameter decryption interface](/secure-api-doc/assets/%E5%90%8E%E7%AB%AF%E6%B5%8B%E8%AF%95Body%E5%8F%82%E6%95%B0%E8%A7%A3%E5%AF%86%E6%8E%A5%E5%8F%A3-CWyaGGat.png)
![Send body ciphertext](/secure-api-doc/assets/%E5%90%8E%E7%AB%AF%E6%B5%8B%E8%AF%95Body%E5%8F%82%E6%95%B0%E8%A7%A3%E5%AF%86%E6%8E%A5%E5%8F%A3%E8%BF%94%E5%9B%9E%E5%80%BC-DoWXQv-n.png)
You can see that the ciphertext parameter is normally deciphered to {"code":200,"message":"哈哈哈","data":null}
, the return value was also successfully encrypted.
![Body decryption](/secure-api-doc/assets/%E5%90%8E%E7%AB%AF%E6%B5%8B%E8%AF%95Body%E5%8F%82%E6%95%B0%E8%A7%A3%E5%AF%86-BaGs2woR.png)
Param and form-data parameter decrypt
This time we are a little more complicated. All kinds of parameters are integrated, and there is no URL matching enabled. We need to add the @DecryptParam
annotation to the field. Note that @DecryptParam
cannot be used at the same time as @RequestParam
. @DecryptParam
has replaced the latter function.
![param and formData decryption interface](/secure-api-doc/assets/%E6%B5%8B%E8%AF%95param%E5%92%8Cform-data%E5%8F%82%E6%95%B0%E8%A7%A3%E5%AF%86%E6%8E%A5%E5%8F%A3-BbaKf33u.png)
The entity class itself does not need to be annotated, but should be added to the fields inside. Note that fields without annotations will not be decrypted (if the configuration url matches, all fields will be decrypted without annotations)
![Fields in Entity Classes](/secure-api-doc/assets/%E6%B5%8B%E8%AF%95param%E5%92%8Cform-data%E5%8F%82%E6%95%B0%E8%A7%A3%E5%AF%86%E5%AE%9E%E4%BD%93%E7%B1%BB-BHMY5uN3.png)
Send the request. The ciphertext in the request is generated by me in advance with code(Attention,The ciphertext in the param should be url safe). It is possible to send these parameters in param
or form-data
.
![send request](/secure-api-doc/assets/%E6%B5%8B%E8%AF%95param%E5%92%8Cform-data%E5%8F%82%E6%95%B0%E8%A7%A3%E5%AF%86postman-D4DBN2Kx.png)
Successfully decipher the annotated parameters, no annotated and empty parameters are not decrypted
![param and formData decryption log](/secure-api-doc/assets/%E6%B5%8B%E8%AF%95param%E5%92%8Cform-data%E5%8F%82%E6%95%B0%E8%A7%A3%E5%AF%86%E7%BB%93%E6%9E%9C-B1sTGoCr.png)
Summary
Regarding the use of the @DecryptApi
@EncryptApi
@DecryptParam
annotation, please see the next article Annotation