如何在没有控制器的情况下保护rspec_api_documentation apitome路由

Cur*_*son 2 rspec ruby-on-rails devise

我有一组API文档页面,我想使用设计密码保护.使用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"
Run Code Online (Sandbox Code Playgroud)

https://github.com/zipmark/rspec_api_documentation

https://github.com/modeset/apitome

我发现这个How-to应该会使网站上的每个页面都需要进行身份验证,但我仍然可以在不登录的情况下点击文档URL.

#https://github.com/plataformatec/devise/wiki/How-To:-Require-authentication-for-all-pages
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

jog*_*aco 5

您可以控制Apitome控制器并请求身份验证.

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

控制器实现如下:

class ApidocsController < Apitome::DocsController
    before_filter :authenticate_user!
end
Run Code Online (Sandbox Code Playgroud)