gpt4 book ai didi

google-app-engine - 是否可以在谷歌应用引擎上将马提尼的 sessionauth 与数据存储一起使用?

转载 作者:IT王子 更新时间:2023-10-29 02:17:18 24 4
gpt4 key购买 nike

我尝试使用 sessionauth 的例子的 martini在谷歌应用引擎上,想在数据存储中保存登录列表,但不知道如何处理 appengine.Context。有没有人有经验?

谢谢。

更新:

// Auth example is an example application which requires a login
// to view a private link. The username is "testuser" and the password
// is "password". This will require GORP and an SQLite3 database.
package ahl

import (
//"fmt"
"github.com/go-martini/martini"
"github.com/hnakamur/gaesessions"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/render"
"github.com/martini-contrib/sessionauth"
"github.com/martini-contrib/sessions"
"net/http"
)

//var namespace string = "ahl"

func init() {
//store := sessions.NewCookieStore([]byte("secret123"))
store := gaesessions.NewDatastoreStore("", gaesessions.DefaultNonPersistentSessionDuration)

m := martini.Classic()
m.Use(render.Renderer())

// Default our store to use Session cookies, so we don't leave logged in
// users roaming around
//store.Options(sessions.Options{
// MaxAge: 0,
//})
m.Use(sessions.Sessions("my_session", store))
m.Use(sessionauth.SessionUser(GenerateAnonymousUser))
sessionauth.RedirectUrl = "/new-login"
sessionauth.RedirectParam = "new-next"

m.Get("/", func(r render.Render) {
r.HTML(200, "index", nil)
})

m.Get("/new-login", func(r render.Render) {
r.HTML(200, "login", nil)
})

m.Post("/new-login", binding.Bind(MyUserModel{}), func(session sessions.Session, postedUser MyUserModel, r render.Render, req *http.Request) {
// You should verify credentials against a database or some other mechanism at this point.
// Then you can authenticate this session.
//user := MyUserModel{}
user := MyUserModel{1, "testuser", "password", false}
//err := dbmap.SelectOne(&user, "SELECT * FROM users WHERE username = $1 and password = $2", postedUser.Username, postedUser.Password)
//if err != nil {
// r.Redirect(sessionauth.RedirectUrl)
// return
//} else {
err := sessionauth.AuthenticateSession(session, &user)
if err != nil {
r.JSON(500, err)
}

params := req.URL.Query()
redirect := params.Get(sessionauth.RedirectParam)
r.Redirect(redirect)
return
//}
})

m.Get("/private", sessionauth.LoginRequired, func(r render.Render, user sessionauth.User) {
r.HTML(200, "private", user.(*MyUserModel))
})

m.Get("/logout", sessionauth.LoginRequired, func(session sessions.Session, user sessionauth.User, r render.Render) {
sessionauth.Logout(session, user)
r.Redirect("/")
})

http.Handle("/", m)
}

最佳答案

是的,这应该是可能的。 sessionauth 包要求您向它传递一个*sessions.Store,并且有一个 gaesessions 包可以替换默认的 cookie/文件存储:https://github.com/hnakamur/gaesessions

sessionauth 包有一个完整的示例 (https://github.com/martini-contrib/sessionauth/blob/master/example/auth_example.go) - 只需将 sessions.NewCookieStore 替换为 gaesessions.NewDatastoreStore

关于google-app-engine - 是否可以在谷歌应用引擎上将马提尼的 sessionauth 与数据存储一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24011381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com