英文:
How can I manage accounts for my google app engine (with golang)?
问题
我阅读了一个名为“使用用户服务”的文档,它很有用。
但是我想要只允许几个用户访问我的GAE,并限制其他人。
那么,我该如何管理我的Google应用引擎(使用golang)的用户账户?
我将使用“Google账户”系统。
我需要你的帮助。
谢谢!祝你有美好的一天~
英文:
I read a document "Using the Users Service" and it works.
But I want to allow only several users to access my GAE, and restrict others.
So, How can I manage user accounts for my google app engine (with golang)?
I will use "Google account" system.
I need your help.
Thank you! Have a nice day~
答案1
得分: 0
我认为你有两个选择:
- 你可以限制只允许你的 Google App 域的用户访问,方法是进入 Administration >> Application Settings >> Authentication Type。
- "appengine/user" 包只提供了基本功能。你可以使用它来检查当前用户的电子邮件是否在允许列表中。
var allowed = []string{"tom@example.com", "jack@example.com"}
func handler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
url, err := user.LoginURL(c, r.URL.String())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Location", url)
w.WriteHeader(http.StatusFound)
return
}
var granted bool
for _, email := range allowed {
if u.Email == email {
granted = true
break;
}
}
if !granted {
http.Error(w, "你不在允许列表中", 400)
return
}
fmt.Fprintf(w, "你好,%v!", u)
}
英文:
I think you have 2 options:
-
You can limit only the user of your Google App domain, go to Administration >> Application Settings >> Authentication Type.
-
The "appengine/user" pakage just give you the basic function. You can use it to check if the current user email is in the allowed-list or not.
var allowed = []string{"tom@example.com", "jack@example.com"}
func handler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
url, err := user.LoginURL(c, r.URL.String())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Location", url)
w.WriteHeader(http.StatusFound)
return
}var granted bool for _, email := range allowed { if u.Email == email { granted = true break; } } if !granted { http.Error(w, "you're not in the allowed list", 400) return } fmt.Fprintf(w, "Hello, %v!", u)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论