【问题标题】:Is it possible to use sessionauth of martini with Datastore on google app engine?是否可以在谷歌应用引擎上将马提尼的 sessionauth 与 Datastore 一起使用?
【发布时间】:2014-06-03 09:18:18
【问题描述】:

我尝试在google app engine上使用martinisessionauth的例子,想在Datastore中保存登录列表,但不知道如何处理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)
}

【问题讨论】:

    标签: google-app-engine session go google-cloud-datastore martini


    【解决方案1】:

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

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

    【讨论】:

    • 好像没有你说的那么简单。 gaesessions 不能替代github.com/martini-contrib/sessions。我想我需要自己为 gae 数据存储编写 sessionauth 并分享,对吗?希望我能成功。
    • sessionauth 和 contrib/sessions 都接受 *sessions.Store。您能否提供您收到的代码和/或错误(更新您的问题)以便我们对其进行调试?您似乎没有任何理由不能使用替代会话存储(最坏的情况下,CookieStore 会起作用)。
    • 请看帖子更新。该示例的运行方式与原始示例不同。我不能再登录了。好吧,我想我对会话有一些误解,因为这是我第一次编写 Web 服务器。我在 user.go 中看到 GetById 函数,注释提醒我们有一个登录列表并从该列表中检查用户 ID,所以我想我需要将登录列表保存在 Datastore 中。但是要访问Datastore,我需要将http.request传递给GetById来运行appengine.NewContext,我卡住了,所以我在这里发布。我想我只是不需要在数据存储中写登录列表。还是谢谢。
    猜你喜欢
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 2012-04-04
    • 2013-01-13
    • 1970-01-01
    • 2015-08-02
    相关资源
    最近更新 更多