亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 golang revel 框架中使用 session

如何在 golang revel 框架中使用 session

Go
慕標琳琳 2021-09-27 17:32:15
我使用 golang web 框架作為我的 web 框架。但是會話不使用。我寫的代碼如下: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)    }}我在方法 GetSecurityCode() 中設置了 session 中的 securityCode 值,該方法用于生成登錄驗證的安全代碼我在方法 Login() 中使用了 securityCode 值。但我一無所獲誰能幫助我,我該如何解決這個問題?
查看完整描述

2 回答

?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

謝謝正如你所說,我解決了這個問題:


// 獲取驗證碼圖片

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())

}


查看完整回答
反對 回復 2021-09-27
  • 2 回答
  • 0 關注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號