ruby-on-rails - 如何在没有 Controller 的情况下保护 rspec_api_documentation apitome 路由

标签 ruby-on-rails rspec devise

我有一组 API 文档页面,我想使用 devise 进行密码保护。文档是使用 rspec_api_documentation 生成的。 gem 使用 rspec 来执行 API 方法并创建 html 或 json doc 文件。我正在生成 json 并使用另一个 gem,apitome,作为查看器。

这一切都很好地工作,我的 api 文档可以在/api/docs 找到,但我不知道如何要求身份验证才能查看文档。

文档是通过 apitome 查看的,因此它使用的是 rails。 routes.rb 中没有路由,但 apitom 初始化程序挂载了文档。

来自 apitom 初始化程序:

# This determines where the Apitome routes will be mounted. Changing this to "/api/documentation" for instance would
# allow you to browse to http://localhost:3000/api/documentation to see your api documentation. Set to nil and mount
# it yourself if you need to.
config.mount_at = "/api/docs"

https://github.com/zipmark/rspec_api_documentation

https://github.com/modeset/apitome

我发现这个操作方法应该使网站上的每个页面都需要身份验证,但我仍然可以在不登录的情况下访问文档 url。

#https://github.com/plataformatec/devise/wiki/How-To:-Require-authentication-for-all-pages

感谢您的帮助。

最佳答案

您可以控制 Apitome Controller 并请求身份验证。

  • 通过将 mount_at 设置为 nil,将 apitome 配置为不自行挂载
  • 编写扩展 Apitome::DocsController 的 Controller ApidocsController
  • 添加 before_filter :authenticate_user!在你的 Controller 中
  • 添加一个路由到你的 Controller :get '/api/docs', to: 'apidocs#index'

Controller 实现会像这样:

class ApidocsController < Apitome::DocsController
    before_filter :authenticate_user!
end

关于ruby-on-rails - 如何在没有 Controller 的情况下保护 rspec_api_documentation apitome 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22993608/

相关文章:

ruby-on-rails - 升级到 OSX Lion 后 RSpec 停止工作

ruby-on-rails - "before :each do" block 未在 rspec 中运行

ruby-on-rails - 设计如何更改 reset_password_token 错误

mysql - 自动创建 `belongs_to`记录

ruby-on-rails - 将 Rails 和 React 应用程序拆分到单独的 Docker 容器中?

ruby - 使用 rspec 测试 after_create

ruby-on-rails - 将密码迁移到 Devise

ruby-on-rails - 目录 : Forgot Password & Sign In - in same page

javascript - 客户端验证和 Rails4

css - 如何使用 Watir 在 div.style 标签中定位文本?