【问题标题】:How to encrypt full routes in ruby on rails at Application level如何在应用程序级别加密 ruby​​ on rails 中的完整路由
【发布时间】:2023-04-10 11:32:01
【问题描述】:

我想通过路由文件加密 ruby​​ on rails 应用程序中的路由。

Sample URL: http://localhost:3000/users or http://localhost:3000/users/1/edit

Encrypted URL: http://localhost:3000/dshfkhjkbkbjkdfjkfdhk or http://localhost:3000/dshfkhjkbkbjkdfjkfdhk/dskjfs/hdfkjdf

可以使用任何 gem 进行加密。 如何在应用级别管理所有路由加密

提前谢谢你

【问题讨论】:

    标签: ruby-on-rails ruby encryption routes rubygems


    【解决方案1】:

    你不能。您所能做的就是实现自己的路由器,并将所有加密路径映射到单个控制器,然后让该控制器调用您的路由器。

    # config/routes.rb
    match '*path', to: 'encrypted#index', via: :all
    
    class EncryptedController < ApplicationController
      def index
        path = params[:path]
        decrypted_path = path.split('/').map{|part| decrypt(part)}.join('/')
        MyRouter.dispatch(request.method, decrypted_path, request)
      end
    end
    
    module MyRouter
      def self.dispatch(request_method, path, request)
        # Your own dispatching logic here.
        # Note that this method should return a Rack-compatible response.
      end
    end
    

    【讨论】:

    • 你能分享一个例子吗?我不知道如何返回与机架兼容的响应。
    • 最简单的格式是[status_code, response_headers, response_body_parts],比如[200, {'content-type' =&gt; 'text/plain'}, ['hello world']]
    • 能否动态生成加密路径?喜欢使用加密方法
    • 没有。此外,当您采用这种方法时,您无法访问 Rails 提供的任何 URL 帮助程序。顺便问一下,为什么要加密路径?
    • 如果您需要身份验证才能访问路由,“加密”它们并不会真正添加任何内容——无论路由是什么,未经身份验证的访问都会出现 403 响应。如果您想混淆您的路线,请考虑将 uuid 用于模型而不是整数。
    猜你喜欢
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 2014-09-07
    • 2012-06-30
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多