美好的一天,我已经实现了一个 REST 服务。在资源端点的 URL 中,我使用 ID,它们是数据库表的主键。例如http://host/myapp/items/item/4
. 我了解到在 URL 中使用数据库 ID 是一种不好的做法,我应该改用 UUID。另一方面,我了解到,如果数据库中有很多记录,那么在索引中使用 UUID 是一个性能问题,因为它们不是连续的(1、2、3、...)。所以我有一个想法来加密数据库ID。这就是它的工作方式:
1) Client POSTs an item to `http://host/myapp/items`.
2) The back-end creates a new item in the database.
3) Autoincremented ID '4' is generated by the database.
4) The back-end encrypts the ID '4' to 'fa4ce3178a045b2a' using a cipher key and returns encrypted ID of a created resource.
接着:
5) Client sends a request to GET `http://myapp/items/item/fa4ce3178a045b2a`.
6) The back-end decrypts 'fa4ce3178a045b2a' to '4' using an cipher key.
7) The back-end fetches item with primary key '4' and sends it to the client.
这种解决方案有什么缺点?加密/解密是否会足够快,以至于不会比使用 UUID 更糟?我应该使用什么加密算法才能快速且不消耗太多资源?更有经验的人可以建议或推荐更好的解决方案吗?先感谢您。沃杰科技