【问题标题】:Change the ActiveStorage Controller Path更改 ActiveStorage 控制器路径
【发布时间】:2018-08-30 17:41:45
【问题描述】:

有没有办法自定义附件网址而不是

/rails/active_storage/representations/
/rails/active_storage/blobs/

我们可以有这样的东西:

/custom_path/representations/
/custom_path/blobs/

【问题讨论】:

标签: ruby-on-rails rails-activestorage


【解决方案1】:

猴子补丁总是在你身边 ?。

为了对下一个补丁感兴趣,我可以更改 ActiveStorage Controller 路径:

module MapperMonkeypatch
  def map_match(paths, options)
    paths.collect! do |path|
      path.is_a?(String) ? path.sub('/rails/active_storage/', '/custom_path/') : path
    end
    super
  end
end

ActionDispatch::Routing::Mapper.prepend(MapperMonkeypatch)

一切似乎都有效?。

【讨论】:

  • 谢谢。我认为 Rails 默认 ActiveStorage 路径是一个安全问题,因为它们暴露了您的应用程序正在运行的框架类型,提供了黑客计划攻击所需的关键信息。我很惊讶这些路径在初始版本中无法配置。
【解决方案2】:

最近添加了一个可以配置路由前缀的功能:https://github.com/rails/rails/commit/7dd9916c0d5e5d149bdde8cbeec42ca49cf3f6ca

现在在 master 分支,但是 应该被集成到 ~> 5.2.2 已经被集成到 Rails 6.0.0 和更高版本.

那么,就是配置的问题了:

Rails.application.configure do
  config.active_storage.routes_prefix = '/whereever'
end

【讨论】:

  • 对于那些现在在这个答案上磕磕绊绊的人,请注意这个补丁没有进入5.2.2,所以这个解决方案需要 Rails 6.0.0 或更高版本。
【解决方案3】:

测试了@stwienert 的解决方案。 config.active_storage.routes_prefix 选项仅适用于 rails 6.0.0alpha 和更高版本的分支。该补丁不适用于 5.2.2

我用补丁为 5.2.2 制作了一个分支。 https://github.com/StaymanHou/rails/tree/v5.2.2.1

要使用它,只需将行 gem 'rails', '~> 5.2.2' 替换为 gem 'rails', '5.2.2.1', git: 'https://github.com/StaymanHou/rails.git', tag: 'v5.2.2.1'。并运行bundle install --force

coffee-rails 如果您还没有用于 rails edge 安装的 gem - https://github.com/rails/rails/issues/28965

【讨论】:

    猜你喜欢
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多