Content

Rails实现字段加密存储(rail是什么意思)干货满满

文章摘要

这篇文章讨论了一个名为`EncryptedStringSerializer`的类及其在`UserAddress`模型中的实现。`EncryptedStringSerializer`提供加密和解密功能,用于对字符串进行加密和解密操作,其中`load`方法用于解密或返回空字符串,`dump`方法用于加密。此外,文章提到`UserAddress`模型继承自`ActiveRecord::Base`,并在多个字段中使用了该序列化器进行加密,以提高数据的安全性。

 class EncryptedStringSerializer def self.load(value) value.to_s.blank? ? ” : decrypt(value) end def self.dump(value) encrypt(value || ”) end private def self.encrypt(value) encryptor.encrypt_and_sign(value) end def self.decrypt(value) encryptor.decrypt_and_verify(value) end def self.encryptor @encryptor ||=ActiveSupport::MessageEncryptor.new(Settings.message_encryptor_key) end end class UserAddress < ActiveRecord::Base serialize :phone, EncryptedStringSerializer serialize :first_name, EncryptedStringSerializer serialize :last_name, EncryptedStringSerializer serialize :country, EncryptedStringSerializer serialize :state, EncryptedStringSerializer serialize :city, EncryptedStringSerializer serialize :address1, EncryptedStringSerializer serialize :address2, EncryptedStringSerializer serialize :zipcode, EncryptedStringSerializer end