[英]How to delete ==%0A from url in order to obfuscate the ids of my records in rails?
using the built-in OpenSSL library to encrypt and decrypt the id 使用内置的OpenSSL库对ID进行加密和解密
the url be like this: http://localhost:3000/requests/Ym7c8FQQlCe5FvotzKu4yw==%0A and i want to delete ==%0A from it ? 网址是这样的: http:// localhost:3000 / requests / Ym7c8FQQlCe5FvotzKu4yw ==%0A ,我想从中删除==%0A吗? how ? 怎么样 ?
require 'openssl'
require 'base64'
module Obfuscate
def self.included(base)
base.extend self
end
def cipher
OpenSSL::Cipher::Cipher.new('aes-256-cbc')
end
def cipher_key
'blah!'
end
def decrypt(value)
c = cipher.decrypt
c.key = Digest::SHA256.digest(cipher_key)
c.update(Base64.decode64(value.to_s)) + c.final
end
def encrypt(value)
c = cipher.encrypt
c.key = Digest::SHA256.digest(cipher_key)
Base64.encode64(c.update(value.to_s) + c.final)
end
end
and in my model: 在我的模型中:
class MyModel < ActiveRecord::Base
include Obfuscate
def to_param
encrypt id
end
end
Take a look at activeuuid 看看activeuuid
When you create a Create a Migration this is how it should look like 当您创建创建迁移时,它应该是这样
class CreateEmails < ActiveRecord::Migration
def change
create_table :users, :id => false do |t|
t.uuid :id, :primary_key => true
t.uuid :something_id # belongs_to :something
t.timestamps
end
add_index :emails, :id
end
end
then in your models all you have to do is include include ActiveUUID::UUID
那么在您的模型中,您要做的就是include ActiveUUID::UUID
and this is a example of a uuid 1dd74dd0-d116-11e0-99c7-5ac5d975667e
这是一个uuid 1dd74dd0-d116-11e0-99c7-5ac5d975667e
I hope that this helps 我希望这个对你有用
Another solution would be to use Friendly ID . 另一种解决方案是使用Friendly ID 。 Basically, it replaces the id
in your routes with a name
or other field. 基本上,它用name
或其他字段替换路由中的id
。 It changes 它改变
/requests/Ym7c8FQQlCe5FvotzKu4yw==%0A
to 至
/requests/some_cool_name_in_your_database
That way, your users don't see the id
, but instead see something a little more pretty. 这样,您的用户不会看到id
,而是会看到更漂亮的内容。 And if your users were to return to that page, it would still work because you have a valid "id". 并且,如果您的用户要返回该页面,则该页面仍将起作用,因为您具有有效的“ id”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.