2015-04-07 26 views
2

我使用golang web框架作爲我的web框架。但會議不會使用。我的代碼編寫如下:如何使用會話與golang狂歡框架

package controllers 

import (
    "fmt" 
    "net/http" 
    "strconv" 

    "GoRBAC/app/base" 
    "GoRBAC/app/models" 

    r "github.com/revel/revel" 
) 

type App struct { 
    *r.Controller 
} 

// 首頁 
func (this App) Index() r.Result { 
    userDao := models.NewUserDao() 
    user, err := userDao.QueryById(1) 
    if err != nil { 
     fmt.Println(err) 
    } 
    fmt.Println(user) 
    return this.Render() 
} 

// 跳轉至登錄頁面 
func (this App) ToLogin() r.Result { 
    return this.RenderTemplate("App/login.html") 
} 

func (this App) Login() r.Result { 

    username := this.Params.Get("username") 
    password := this.Params.Get("password") 
    securityCode := this.Params.Get("securityCode") 

    fmt.Println(this.Session["securityCode"]) 
    fmt.Println(username) 
    fmt.Println(password) 
    fmt.Println(securityCode) 

    if securityCode != this.Session["securityCode"] { 
     return this.RenderText("error securityCode") 
    } else { 
     userDao := models.NewUserDao() 
     user, err := userDao.QueryByUsername(username) 
     if err != nil || user == nil { 
      return this.RenderText("username not exsit") 
     } else if user.Password != password { 
      return this.RenderText("error password") 
     } else { 
      delete(this.Session, "securityCode") 
      this.RenderText("ok") 
     } 
    } 
    return this.RenderText("ok") 
} 

// 獲取驗證碼圖片 
func (this App) GetSecurityCode(timestamp int64) r.Result { 
    // 時間戳參數,第一次加載爲1,後續加載爲當前的時間戳,可以用來驗證客戶端刷新頻率 
    // 如:刷新頻率過高,直接限制當前客戶端等 
    fmt.Println("GetSecurityCode", timestamp) 

    d := make([]byte, 4) 
    s := base.NewLen(4) 
    ss := "" 
    d = []byte(s) 

    for v := range d { 
     d[v] %= 10 
     ss += strconv.FormatInt(int64(d[v]), 32) 
    } 

    // 將驗證碼字符串存入到session 
    this.Session["securityCode"] = ss 
    this.Session.SetNoExpiration() 

    fmt.Println(this.Session["securityCode"]) 
    fmt.Println(ss) 

    this.Response.Status = http.StatusOK 
    this.Response.ContentType = "image/png" 

    base.NewImage(d, 100, 40).WriteTo(this.Response.Out) 

    return this.Render() 
} 

我的方法GetSecurityCode()設置securityCode值在會話中,這種方法的使用,以產生用於登錄的安全碼驗證

我使用的securityCode值Login()方法。但我沒有得到任何價值

任何人都可以幫助我,我該如何解決這個問題?

謝謝!!

回答

1

我想知道爲什麼你的絕對有效的問題downvoted。你所描述的問題是已知的。這裏有一個相關的GH問題:https://github.com/revel/revel/issues/792

臨時解決方法是避免直接寫入c.Response.Out。而是使用一個緩衝區,然後使用RenderText返回它:

this.Response.Status = http.StatusOK 
this.Response.ContentType = "image/png" 

var buffer bytes.Buffer 
base.NewImage(d, 100, 40).WriteTo(&buffer) 

return this.RenderText(buffer.String()) 

或可選擇地修改您的GetSecurityCode所以你不需要Session那裏。

1

@anonx: 感謝 正如你所說,我解決這個問題,如:

// 獲取驗證碼圖片 
func (this App) GetSecurityCode(timestamp int64) r.Result { 
// 時間戳參數,第一次加載爲1,後續加載爲當前的時間戳,可以用來驗證客戶端刷新頻率 
// 如:刷新頻率過高,直接限制當前客戶端等 
//fmt.Println("GetSecurityCode", timestamp) 

d := make([]byte, 4) 
s := base.NewLen(4) 
ss := "" 
d = []byte(s) 

for v := range d { 
    d[v] %= 10 
    ss += strconv.FormatInt(int64(d[v]), 32) 
} 

this.Session["securityCode"] = ss 
fmt.Println(this.Session["securityCode"]) 

//this.Response.Status = http.StatusOK 
//this.Response.ContentType = "image/png" 

buff := &bytes.Buffer{} 
base.NewImage(d, 100, 40).WriteTo(buff) 

return this.RenderBinary(buff, "img", r.Attachment, time.Now()) 
}