使用 https 保护 Rails API 的安全
我正在创建我的第一个 API 并使用 Ruby on Rails。 API 将严格是服务器到服务器。我已经做了很多阅读关于保护此类 API 的方法,并决定使用 https 可能是最简单的方法,而不是 Oauth。
附加信息:
- API 非常简单且只读。
- 我提供的数据并不便宜,我需要向我们的数据提供商和合作伙伴表明 API 是安全的,并且他们的数据将受到保护,不会被盗。这是我需要保护它的唯一原因。
我最初的计划是简单地使用通过 https 发送的私钥。我不需要担心客户端共享此密钥,因为它们是根据使用情况计费的。
我的问题是如何在客户端服务器上强制使用 https?除了要求 API 路由使用 https 协议之外,我还需要做其他事情吗?
I'm creating my first API and am using Ruby on Rails. The API will strictly be server to server. I've done a lot of reading on the methods of securing an API like this and have decided that using https might be the easiest method, rather than Oauth.
Additional Info:
- API is pretty simple and read only
- The data I'm providing is not cheap and I need to show our data provider and partner that the API is secure and that their data will be protected from theft. This is the only reason I need it secured.
My initial plan is to simply use a private key that will be sent over https. I don't need to worry about the client sharing this key because they are billed based on usage.
My question is how do you go about enforcing use of https on the client server? Are there any other things I need to do on my end other than require the API routes use https protocol?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HTTPS 只做两件事:
* 给您一种温暖模糊的感觉,您正在与正确的服务器进行通信
* 使用加密技术防止窃听和篡改。
这不会限制对您的 API 的访问。敏感数据必须使用 HTTPS,因此请使用它。您可以将前端服务器(例如 nginx)设置为专门使用 SSL(例如,不要配置端口 80/HTTP)。在这里阅读更多内容: http://ariejan.net/2011/10/22/automatically-switch- Between-ssl-and-non-ssl-with-nginx-unicorn-rails
然后你就会希望客户端进行自身身份验证,以便您可以检查他们是否是从您那里接收数据的正确方。您可以在这里使用 OAuth,但据我所知,只有一个客户端,这可能有点矫枉过正。
您可以采用的最简单的身份验证形式是需要身份验证令牌。每个请求都必须包含此 api 令牌,您可以在服务器端验证该令牌。您还可以使用它记录有关使用情况的指标。
因此,基本上,每个请求都需要一个 API 密钥,并配置您的服务器,以便您的 API 仅通过 HTTPS 公开。
HTTPS only does two things:
* Give you a warm fuzzy feeling that you're communicating with the right server
* Use encryption to prevent eavesdropping and tampering.
This does not restrict access to your API. Using HTTPS for sensitive data is a must, so use it. You can setup your front-end server (nginx for example) to use SSL exclusively (e.g. don't configure port 80/HTTP). Read more here: http://ariejan.net/2011/10/22/automatically-switch-between-ssl-and-non-ssl-with-nginx-unicorn-rails
Then you will want the client to authenticate itself so you can check they are the right party to receive data from you. You could use OAuth here, but since I gather there will only be one client, this might be overkill.
The simplest form of authentication you can employ is requiring an authentication token. Each request must include this api token which you can validate server-side. You can also use it record metrics about usage.
So, basically, require an API key for every request and configure your server so your API is only exposed over HTTPS.