我有一组 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/