Web应用程序后端认证和交互
我目前正在用 Go 编写一个 Web 应用程序后端。使某种 API 可用于标准 jQuery AJAX 前端与我的后端交互的最佳方法是什么?
现在,我有一些函数可以接受一些数据、执行操作并返回其他数据,但是我到底应该从哪里开始呢?我有一个模糊的想法,即在某个端口上侦听 JSON 编码的函数调用并返回该函数的 JSON 编码的输出,但是(如果这是实现此目的的好方法)实现此目的的最佳方法是什么?
此外,我应该如何使用 Go/AJAX 处理登录系统和/或身份验证?为该用户返回一些唯一的哈希密钥是否有意义(如果选择持久登录,则将其保存到 cookie),将该密钥存储在内存中,并将该密钥作为发送到的每个 JSON 编码函数调用的参数发送服务器?或者,是否有更好的方法来实现此目的(我对登录系统不了解)或者可能是已经为 Go 开发的解决方案?
I'm currently writing a Web application backend in Go. What is the best way to make some kind of API available for a standard jQuery AJAX frontend to interact with my backend?
Right now, I have some functions that accept some data, perform operations, and return other data, but where exactly should I go from there? I have a vague idea of listening in on some port for a JSON-encoded function call and returning the JSON-encoded output of that function, but (if this is a good way of accomplishing this) what is the best way of accomplishing this?
Furthermore, how exactly should I handle a login system and/or authentication with Go/AJAX? Would it make sense to return some unique hash key for that user, (save it to a cookie if persistent login is selected,) store that key in memory, and send that key as a parameter of every JSON-encoded function call sent to the server? Or, is there a better way of accomplishing this (I'm not knowledgeable on login systems) or possibly a solution already developed for Go?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Goajax 是 go 的 JSON-RPC 包。这种风格有点像你通过 JSON 传递函数名称和参数,然后它返回 JSON 作为答案。
但就我个人而言,我更喜欢 REST 服务。 REST 使用标准 Web 技术,尤其是 HTTP 和 URI 来传递资源以及对它们执行的操作。 JSON 对此也非常有效。对于 REST 服务,有 rest.go 库(还有 分叉)。
对于身份验证,您可能需要查看 authcookie。 “实现签名身份验证 cookie 的创建和验证。”
通过 cookie(或参数作为替代)使用 auth-hash 是一种常见的方法。但请确保您知道,您确保它们的安全。使用 HTTPS 防止窃听(WLAN、酒吧网络、中间人)。您首先如何验证它们取决于您实际在做什么。还要确保考虑会话生命周期。
Goajax is a JSON-RPC package for go. The style is somewhat you pass it function names and parameters via JSON, and it returns JSON as answer.
Personally though, I prefer REST-services. REST uses standard web technologies, especially HTTP and URI for passing resources and what to do on them. JSON is very efficient for this as well. For a REST-service, there is the rest.go library (also, a fork).
For authentification, you may want to look at authcookie. "implements creation and verification of signed authentication cookies."
Using an auth-hash via cookie (or param as alternative) is a common way. Make sure you are aware though, that you make them secure. Use HTTPS to prevent eavesdropping (WLANs, pub-nets, man-in-the-middles). How you first validate them depends on what you’re actually doing. Also make sure to think about session lifetimes.