英文:
Web application backend authentication and interaction
问题
我目前正在使用Go编写Web应用程序的后端。如何最好地为标准的jQuery AJAX前端提供一种API与我的后端进行交互?
目前,我有一些函数接受一些数据,执行操作,并返回其他数据,但我应该从哪里开始呢?我有一个模糊的想法,即监听某个端口以接收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?
答案1
得分: 1
Goajax 是一个用于Go语言的JSON-RPC包。它的风格是通过JSON传递函数名和参数,并返回JSON作为答案。
个人而言,我更喜欢REST服务。REST使用标准的Web技术,特别是HTTP和URI来传递资源和对其进行操作。JSON在这方面也非常高效。对于REST服务,有rest.go库(还有一个fork)。
对于身份验证,你可能想看一下authcookie。它“实现了创建和验证签名身份验证cookie”。
通过cookie(或参数作为替代)使用身份验证哈希是一种常见的方式。但请确保你意识到要使它们安全。使用HTTPS来防止窃听(无线局域网、公共网络、中间人攻击)。如何首次验证它们取决于你实际在做什么。还要确保考虑会话的生命周期。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论